frictionlesspulley
frictionlesspulley

Reputation: 12368

Standards for accessing Webservice in C#

I wanted to know a standard way of accessing web-services in C# where the webservice reference can be defined programmatically.

I have the following scenario :

  1. Multiple webservices can be set up programmatically. Hence I cannot use the "Add Webservice Reference" provided by Visual Studio (or so I think correct me if I am wrong).
  2. the webservices added have the same structure/actions/operations/request/responses but may have belong to different domains feeding different data.

e.g :

  webservice 01 :  http://abc.example.com/getData
  webservice 02 :  http://xyz.example.net/getData
  1. Can I still use a proxy generated from one service and use it for another or would I have to handle raw XML responses?

Edit 01 : I wanted to know if the following snippet of accessing the webservice can be generalized to be used for all webservices

        var binding = new BasicHttpBinding();
        var address = new EndpointAddress("http://www.abc.com/service.asmx");
        var factory = new ChannelFactory<IGeneralProxy>(binding, address);

        var obj = factory.CreateChannel();
        var responseString = obj.GetData("UserName", "Password");
        Assert.IsNotNull(responseString);

Where IGeneralProxy is an interface for the Client

Please let me know if if any of the above points are not clear.

Upvotes: 1

Views: 367

Answers (3)

Alex Mendez
Alex Mendez

Reputation: 5150

Yes, you can use the same generated proxy for both services as long as the service is the same. I do it all the time.

Here is a snipit of my code. I use WSE 3.0 as the project that I am working on is .net 2.0.

ServiceWse serivce = new ServiceWse();
CookieContainer cookieContainer = new CookieContainer();
serivce.Timeout = 1000 * 60 * CommonFunctions.GetConfigValue<int>(Consts.Common.WebServiceTimeout, 20);
serivce.Url = CommonFunctions.GetConfigValue(Consts.Urls.MyServiceSecuredURL, string.Empty);
serivce.CookieContainer = cookieContainer;
if (CommonFunctions.GetConfigValue(Consts.Security.UseSecuredServices, false))
    CommonFunctions.SetWSSecurity(_service.RequestSoapContext);

Upvotes: 1

Derek Risling
Derek Risling

Reputation: 364

We do something similar. Web service references are created against the Dev version of our services. We also have a setting in the web.config for each web service URI, that we use when instantiating the service; that way, when we deploy to production, all we have to do is change the URI in the web.config, rather than rebuild the project.

var myService= new myService(){Uri = [service uri from web.config]};

Upvotes: 0

Allensb
Allensb

Reputation: 260

Check out this previous answer to a similar question:

How to programmatically connect a client to a WCF service?

You can do that for each web service.

Upvotes: 0

Related Questions