简体   繁体   中英

How to set webservice endpoint address at runtime?

I want to connect to a webservice url which is provided at runtime when user logs in. so i need to set Endpoint.Address in app.config at runtime.

EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
Uri uri = new Uri(txtURL.text.trim());
var address = new EndpointAddress(uri, spn);
var client = new EchoServiceClient("WSHttpBinding_IEchoService", address);
client.Close(); 

I placed this code on button click ,and gets the value of uri from textbox. Code is executing correctly and then getting an error message

"Address property on channelfactory.endpoint was null"

my app.config is:

<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="LoginServiceSoap" />
            </basicHttpBinding>
        </bindings>
        <client>
        <!--<endpoint address="http://localhost:3073/LoginService.asmx" binding="basicHttpBinding"-->
           <endpoint address="" binding="basicHttpBinding"
                bindingConfiguration="LoginServiceSoap" contract="LoginService.LoginServiceSoap"
                name="LoginServiceSoap" />
        </client>
    </system.serviceModel>
</configuration>

I've done something similar in a recent project, where I only wanted to set the endpoint and authentication programatically and not use the config file at all:

public static class ServiceClientFactory
{
    public static HttpBindingBase BuildNavisionBinding(string endpointUrl)
    {
        //http://blog.randomdust.com/index.php/2010/10/could-not-establish-trust-relationship-for-the-ssl-tls-secure-channel/
        //http://www.codeproject.com/Forums/1649/Csharp.aspx?fid=1649&df=90&mpp=25&sort=Position&select=3126652&tid=3121885
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(delegate
        {
            return true;
        });

        if (endpointUrl.ToLower().StartsWith("https"))
        {
            var binding = new BasicHttpsBinding(BasicHttpsSecurityMode.Transport);
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            binding.MaxReceivedMessageSize = int.MaxValue - 1;
            return binding;
        }

        else
        {
            var binding = new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly);
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            binding.MaxReceivedMessageSize = int.MaxValue - 1;
            return binding;
        }
    }

    public static TClient CreateClient<TClient, TChannel>(string endpoint, string username, string password)
        where TClient : ClientBase<TChannel>
        where TChannel : class
    {
        var client = (TClient)
            Activator.CreateInstance(
                typeof (TClient),
                BuildNavisionBinding(endpoint),
                new EndpointAddress(endpoint));

        if (null == client.ClientCredentials)
            throw new Exception(
                string.Format("Error initializing [{0}] client.  Client Credentials object was null",
                    typeof(TClient).Name));

        client.ClientCredentials.Windows.ClientCredential =
            new NetworkCredential(
                username,
                password);

        client.ClientCredentials.Windows.AllowedImpersonationLevel =
            TokenImpersonationLevel.Delegation;

        client.Endpoint.Binding.SendTimeout = new TimeSpan(0, 0, 4, 0);
        client.Endpoint.Binding.ReceiveTimeout = new TimeSpan(0, 4, 0);
        client.Endpoint.Binding.OpenTimeout = new TimeSpan(0, 0, 4, 0);

        return client;
    }

So from your example:

var client = ServiceClientFactory.CreateClient<EchoServiceClient, IEchoServicePort(txtUrl.text.trim(), /* authentication */);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM