1

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.

5
  • I am not expert but consuming Sharepoint WS I did not have access to the WSDL of the site I wanted to connect that; I did access some other site (in the same server farm) and updated its URL. It did work ok.
    – SJuan76
    Jun 5, 2012 at 15:15
  • If you can add a service reference to one of the services, you can later change its .Url property to switch between the various endpoints (assuming each and every service you call is exactly the same)
    – Alex
    Jun 5, 2012 at 15:15
  • Is the abc and xyz services the same? Meaning it is one service deployed in different locations? Jun 5, 2012 at 15:17
  • But is this a good practice (adding a reference and then changing the Url)? Jun 5, 2012 at 15:18
  • @Alex Mendez they are deployed at different locations Jun 5, 2012 at 15:18

3 Answers 3

1

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);
2
  • Nice! Though this was not part of the original question how do you hadnle dynamic Namespaces if any? Jun 5, 2012 at 18:32
  • With generated proxies, you don't need handle dynamic namespaces. You can delploy your service to as many servers as you want. As long as the service name and methods are the same, it will work. Jun 7, 2012 at 15:12
0

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.

0

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]};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.