How to Dynamically Switch Web Service Addresses in .Net Without a Recompile

How can I dynamically switch web service addresses in .NET without a recompile?

When you generate a web reference and click on the web reference in the Solution Explorer. In the properties pane you should see something like this:

Web Reference Properties

Changing the value to dynamic will put an entry in your app.config.

Here is the CodePlex article that has more information.

Dynamically set URL of web service

As far as I know, Visual Studio Tools for Office projects allow you to have an app.config file in your project. I'd expect that Visual Studio created the app.config file and added the necessary configuration settings related to the web service reference. In any case, you need to store the correct web service url somewhere - app.config, registry or even a database.

If you are not able to store the web service endpoint address information in the app.config file, there's a way to configure the proxy manually.

  1. If it's a .asmx reference added as a legacy "Web Reference" in Visual Studio then all you need to do is set the Url property value of the proxy object before you call any web service methods. For example:

    MyASMXWebService proxy = new MyASMXWebService();
    proxy.Url = "web service url";
    proxy.HelloWorld();
  2. If it's a .svc WCF service reference then things get a bit more complicated. You'll need to create your web service endpoint programmatically. For example:

    BasicHttpBinding binding = new BasicHttpBinding();
    EndpointAddress endpoint = new EndpointAddress("web service url");

    ChannelFactory<IMyWCFWebService> factory = new ChannelFactory<IMyWCFWebService>(binding, endpoint);
    IMyWCFWebService proxy = factory.CreateChannel();

    proxy.DoWork();

Need help on setting web service proxy dynamically without build?

You can use the same code generated by Visual Studio when you change the behavior to dynamic:

public Service1() {
string urlSetting = System.Configuration.ConfigurationSettings.AppSettings["WebApplication1.localhost.Service1"];
if ((urlSetting != null)) {
this.Url = string.Concat(urlSetting, "");
}
else {
this.Url = "http://localhost/WebService1/Service1.asmx";
}
}

generate webservice in Visual Studio with a dynamic root URL

if your "CoBRA_BaseClient" and "CoBRA_DMSClient" inherited from System.ServiceModel.ClientBase< TChannel >
then you can try the following:

public static CoBRA_BaseClient CreateService()
{
CoBRA_BaseClient service = new CoBRA_BaseClient();
service.Endpoint.Address = new EndpointAddress("uri");
return service;
}

public static CoBRA_DMSClient CreateService()
{
CoBRA_DMSClient service = new CoBRA_DMSClient();
service.Endpoint.Address = new EndpointAddress("uri");
return service;
}

How do I call a webservice without a web reference?

Create the web reference, and convert the web service to a dynamic web service. A dynamic web service allows you to modify the Url.

You need to create the web reference now to ensure your application understands the interfaces available. By switching to a dynamic web service you can then modify the .Url property after you have initialised the web reference in your code.

service = new MyWebService.MyWebService();
service.Url = myWebServiceUrl;

how to change the web service link dynamically?

YourService service = new YourService();
service.Url = "http://some.other.url/";

How do I configure and initialize a .NET 2.0 SOAP web-reference manually?

Have you checked the URL Behavior setting for the web reference? If it's Dynamic, it will be attempting to get the URL from the config file.

This question covers programmatic configuration of the URL in more detail:

How can I dynamically switch web service addresses in .NET without a recompile?

Calling SOAP web service programmatically

why not just add it as a "web reference" instead of a service reference.

  1. From the solution explorer right click and add Service references
  2. Click advanced on the lower left
  3. Click "Add Web Reference" in the lower left.

Then add you web reference. Once the reference is added to your project. Right click on the new reference and show the properties. Select "URL Behavior" as Dynamic.

This will allow you to change the url in the future if the web service moves.



Related Topics



Leave a reply



Submit