Click to See Complete Forum and Search --> : WebServices I don't get it
ireland
January 11th, 2008, 10:11 AM
I've been playing around with webservices and I don't get it.
First of all how does the client know how to deal with XML? as all the client can handle is html and script, isn't that correct?
PeejAvery
January 11th, 2008, 11:03 AM
as all the client can handle is html and script, isn't that correct?
No. XML is also readable by the client because it is a widely used standard. It has been readable on the client-side for years.
ireland
January 11th, 2008, 12:02 PM
Ok so the client (browser) can read XML returned from the server. What then, how does the client know how to handle this XML and make use of it? wouldn't the client need some script to deal with the XML? if so where does this script come from?
PeejAvery
January 11th, 2008, 12:15 PM
XML returned directly to the client itself shows a standard XML tree. However, most web services have a handler. An example of this would be AJAX. After sending the information, it is returned in a response. That response can then be accessed and a variable set as its return value.
ireland
January 11th, 2008, 12:35 PM
So a request is made to a .asmx file, this is handled on the server side and returns XML in a response, the client recieves the XML............then what?
Yuo can see the xml using the ?wsdl, but what use is this to the client?
Yeorwned
January 11th, 2008, 01:05 PM
It does whatever you make it do generally.
If I just type in yourwebservice.asmx?whatever, your server will spit back XML which would just be displayed on the screen in most browsers.
If I was on a web page that used javascript to call your web service, then the javascript could be programmed to take the xml response and use it in the page to say Hello $xmlvariable.
Arjay
January 11th, 2008, 01:09 PM
On the client, typically you will generate a proxy for the web service.
This uses XmlSerialization and turns the soap xml into class instances so you can program with a class instead of dealing with raw xml and the dom.
For example in C#, say I have a web service that operates on employees. To retrieve the list of employess, I create an instance of the service proxy and then call a method to retrieve the employees. It may look something like:
using( EmployeeServiceClient esClient = new EmployeeServiceClient( ) )
{
foreach( Employee employee in esClient.GetEmployees( ) )
{
// Access employee properties and methods here
}
}
Btw, in a .Net C# or VB project, when you choose 'add a web service' to the project, this proxy gets generated for you. In addition there are tools available to generate proxys in Java as well.
ireland
January 14th, 2008, 04:42 AM
Eh hold on now a C# Proxy class on the Client, how's that, isn't all .NET code run on the server and only HTML and Script sent upto the client?? :confused:
I've been playing with the Proxy class but I thought it lived on the Server side of course!!!
Here's what I tried, Service4 is the Proxy class that extends System.Web.Services.Protocols.SoapHttpClientProtocol and is compiled into an assembly and added to the website as a reference
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Service4 service4 = new Service4();//Proxy Class on server side lives in assembly
Context.Response.Write(service4.Url);
service4.HelloWorld();
}
}
Shuja Ali
January 14th, 2008, 04:55 AM
In simple words, the client that you are talking about would be an application of some sort(ASP.NET, Windows, Console Application, etc). Now when you request services from a webservice and it returns XML, this client will know what to do with the XML returned by the webservice.
For example I have a webservice that returns me all employees from a database. I can use the return value to create a dataset and then bind it to a datagrid. It all actually depends on what this CLIENT wants to do with the XML returned by the Web Service.
ireland
January 14th, 2008, 05:13 AM
In simple words, the client that you are talking about would be an application of some sort(ASP.NET, Windows, Console Application, etc). Now when you request services from a webservice and it returns XML, this client will know what to do with the XML returned by the webservice.
Yes so raw XML is returned, excellent.
What's the deal with this Proxy class? it is a Serializable class meaning it can create a .NET instance from the XML, great, but how can it run on the Client side?
JohnIdol
January 14th, 2008, 05:26 AM
Yes so raw XML is returned, excellent.
It ain't RAW xml, it is SOAP xml, which means if you try to output your object in your web methods and you want to use xml output you won't be able to cast it to XMLDocument type, you have to output an XMLDocument from your webmethod, which - btw - sucks.
At least when I tried it worked this way - framework 2.0 -, I'd be gald to hear it ain't right.
ireland
January 14th, 2008, 05:35 AM
I'll rephrase:
Yes so Soap XML is returned, excellent.
What's the deal with this Proxy class? it is a Serializable class meaning it can create a .NET instance from the Soap XML, great, but how can it run on the Client side?
What is the use of the Proxy Class o the Client? as it runs on the Server, the Client still only has access to Soap XML
JohnIdol
January 14th, 2008, 05:48 AM
What is the use of the Proxy Class o the Client? as it runs on the Server, the Client still only has access to Soap XML
The client has a web reference which gives information about the type returned from the web service. You can't use webservices without a web reference indeed.
ireland
January 14th, 2008, 06:46 AM
Please confirm my understanding layed out below.
You add a web reference to your webservice to your client script. A proxy class is created e.g. localhost.Service1.
You can now create an instance of the Proxy in your client script.
e.g. Default.aspx like so
protected void Page_Load(object sender, EventArgs e)
{
localhost.Service1 service1 = new localhost.Service1();
service1.HelloWorld();
}
At runtime the Default.aspx is loaded, the Page_Load method is called on the Server side, it creates the instance of localhost.Service1 on the Servcer side and then calls HelloWorld(), this returns something embedded in SOAP XML to the client.
So this is what you talk about when you say accessing the WebService through a Proxy from the client, but in actual fact none of these actually run on the Client, just called from the Client script shown above.
Arjay
January 14th, 2008, 01:05 PM
protected void Page_Load(object sender, EventArgs e)
{
localhost.Service1 service1 = new localhost.Service1();
service1.HelloWorld();
}In this case the 'client' is your aspx server code.
Just ask yourself - at this point in the code would you rather work with a class instance with properties and methods or would you like to work with xml and the DOM (and your answer may be the DOM if you have never worked with XmlSerialization before)?
Btw, the code you posted invokes a method on the web service. The way you have it code will not do anything on the 'client' (again which is your aspx server code). If you need it to interact, perhaps something like the following would be more appropriate:
using( localhost.Service1 service1 = new localhost.Service1() )
{
string myString = service1.GetHelloWorld();
// use the myString here
}
ireland
January 14th, 2008, 04:17 PM
I'm aware of how to use the resiltant xml, the confusion was with the term 'client'. Of course when the client is something like a console app then you need to add some way to communicate with the server containing the webservice i.e. a Proxy class.
So this Proxy class will take care of the HTTP and Soap envelope creation, SOAP De-/serialization.
When working with web applications then there's no need for the Proxy class (and VS.NET does not create a proxy, Reference.cs, for you in this case).
What method in the Proxy class actually creates the SOAP envelope?
Arjay
January 14th, 2008, 05:23 PM
I'm aware of how to use the resiltant xml, the confusion was with the term 'client'. Of course when the client is something like a console app then you need to add some way to communicate with the server containing the webservice i.e. a Proxy class.
So this Proxy class will take care of the HTTP and Soap envelope creation, SOAP De-/serialization.
When working with web applications then there's no need for the Proxy class (and VS.NET does not create a proxy, Reference.cs, for you in this case).
What method in the Proxy class actually creates the SOAP envelope?I'm not sure if you are asking questions or making statements. If you choose the 'add Web service' option a proxy class will get generated for you regardless of whether your client is a web application, a console app or another web service. The class that gets generated is derived from SoapHttpClientProtocal. The public methods mirror your web service method and internally call protected base class methods such as Invoke and InvokeAsync. It is within these method where the soap generation happens.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.