How to make your Service-Reference proxy URL dynamic?

Elad Benda picture Elad Benda · Sep 27, 2011 · Viewed 12.1k times · Source

I have a web reference to web-service:

using (var client = new GetTemplateParamSoapClient("GetTemplateParamSoap"))
{
    TemplateParamsKeyValue[] responsArray = client.GetTemplatesParamsPerId(
        CtId, tempalteIds.ToArray());

    foreach (var pair in responsArray)
    {
        string value = FetchTemplateValue(pair.Key, pair.Value);
        TemplateComponentsData.Add(pair.Key, value);
    }
}

Tried to change a web-reference url from c# code: as advice here:

1) http://www.codeproject.com/KB/XML/wsdldynamicurl.aspx

2) How to call a web service with a configurable URL

3) http://aspalliance.com/283_Setting_Web_Service_References_Dynamically

But I get symbol is missing when trying to do:

client.Url

In addition I couldn't find a property of "Url_behavior"

Answer

ladenedge picture ladenedge · Sep 27, 2011

It sounds like you've already added the service reference, but here's a walkthrough on adding, updating and removing service references.

Once you've got one of those in your project, you can alter the endpoint URI with one of the constructor overloads, as John Saunders said above. To do this, you'll need to know the name of the endpoint in your config file. For instance, after you add your service you might have elements like this in your config file:

<endpoint address="http://bleh.com/services/servicename.asmx"
    binding="basicHttpBinding" bindingConfiguration="ServiceNameSoap"
    contract="ServiceReference1.ServiceNameSoap" name="ServiceNameSoap" />

Given that endpoint, you can change the address at runtime by using the following overload:

var proxy = new ServiceReference1.ServiceNameSoapClient("ServiceNameSoap",
    "http://new-address.com/services/servicename.asmx");

You can also do it after construction, but that becomes a little bit harder. If you need to do so, see the documentation on the Endpoint property and the associated type ServiceEndpoint.