Simplifying Soap Web Services calls for client code

T

Tim Menninger

Hi,

I am trying to figure out how to create an architechture for client code to
make calls to a web service but do some common operations in the proxy code
so each client call to the web service does not need to do these things.

Things I would like to do:
1) Set the url from a client config file at runtime
2) Each [WebMethod] takes a standard SOAP header. For each web service call
fill in the header data from the user's session.

I have tried to do this by inheriting from the Proxy classes generated by
"Web Reference" but I have run into problems doing this. Using the sample
code below I would like to derive a single base class that all generated
proxy classes can inherit from that will do these common operation since the
code would be identical for each proxy class.

The code below will work but that means that EVERY web service method needs
me to cut and paste this logic into a new Proxy class. The problem with
creating a base class and changing the generated classes to inherit from is
that the Soap header is defined in each generated proxy class so in a base
class I do not have access to the Soap header class.

Is there a way that I can override a method in
System.Web.Services.Protocols.SoapHttpClientProtocol to do these operations?



Ex. [C#]
//The generated proxy class
public class MyWebService : SoapHttpClientProtocol
{
public MyWebService
{
this.Url = "http://...";
}

public int Add(int a, int b)
{
object[] results = this.Invoke("Login", new object[] {a,b});
return ((int)(results[0]));
}
}

//My override class
public class MyWebServiceProxy : MyWebService
{
public MyWebServiceProxy()
{
this.Url = Application["ServiceUrl"].Value;
}

public int Add(int a, int b)
{
//fill in the soap header
this.MyHeader.Data = Session["XXXXXX"];;
int result base.Add(a,b);
DoSomethingWithSoapHeader();
}
}


Thanks,

Tim
 
P

Paolo

You don't have to add the logic to all your methods, but you will have
to add the logic to each individual set of web services. Here is
example code:

public class MyWebServiceProxy : MyWebService {
public MyWebServiceProxy() : base() {
//example
RequestEncoding = System.Text.Encoding.UTF8;
}

protected override WebRequest(Uri uri) {
WebRequest request = base.GetWebRequest(uri);
//add headers etc to your request object
request.Headers.Add("Header", "Value");
return request;
}
}


you could also override the WebResponse and manipulate the data coming
back if you want.

hope this helps,

-paolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,282
Latest member
RoseannaBa

Latest Threads

Top