如何在运行时设置web服务端点地址

本文关键字:服务 端点 地址 web 设置 运行时 | 更新日期: 2023-09-27 18:13:16

我想连接到一个web服务url,这是在运行时用户登录时提供的。所以我需要设置端点。运行时在app.config中的地址

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(); 

我将此代码放在按钮单击上,并从文本框中获取uri的值。代码正确执行,然后得到错误消息

" channelfactory的Address属性。终点为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>

如何在运行时设置web服务端点地址

我在最近的一个项目中做了类似的事情,我只想以编程方式设置端点和身份验证,根本不使用配置文件:

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

那么从你的例子中:

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