WCF 在运行时更改端点地址 [英] WCF change endpoint address at runtime

查看:26
本文介绍了WCF 在运行时更改端点地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的第一个 WCF 示例.我在一个有很多绑定的网站上有主机.因此,我已将此添加到我的 web.config.

I have my first WCF example working. I have the host on a website which have many bindings. Because of this, I have added this to my web.config.

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

这是我的默认绑定 http://id.web,适用于以下代码.

This is my default binding http://id.web, which works with the following code.

EchoServiceClient client = new EchoServiceClient();
litResponse.Text = client.SendEcho("Hello World");
client.Close();

我现在尝试在运行时设置端点地址.即使是上面代码的同一个地址.

I am now trying to set the endpoint address at runtime. Even though it is the same address of the above code.

EchoServiceClient client = new EchoServiceClient();
client.Endpoint.Address = new EndpointAddress("http://id.web/Services/EchoService.svc"); 

litResponse.Text = client.SendEcho("Hello World");
client.Close();

我得到的错误是:

The request for security token could not be satisfied because authentication failed. 

请建议我如何在运行时更改端点地址?

Please suggest how I may change the endpoint address at runtime?

这里还有我的客户端配置,由 Ladislav Mrnka 请求

 <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IEchoService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                    allowCookies="false">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <reliableSession ordered="true" inactivityTimeout="00:10:00"
                        enabled="false" />
                    <security mode="None" />
                </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://id.web/Services/EchoService.svc" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IEchoService" contract="IEchoService"
                name="WSHttpBinding_IEchoService">
                <identity>
                    <servicePrincipalName value="host/mikev-ws" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>

推荐答案

因此,您在第一个示例中定义的端点地址不完整.您还必须定义端点身份,如客户端配置中所示.在代码中你可以试试这个:

So your endpoint address defined in your first example is incomplete. You must also define endpoint identity as shown in client configuration. In code you can try this:

EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
var address = new EndpointAddress("http://id.web/Services/EchoService.svc", spn);   
var client = new EchoServiceClient(address); 
litResponse.Text = client.SendEcho("Hello World"); 
client.Close();

valamas 实际工作的最终版本

EndpointIdentity spn = EndpointIdentity.CreateSpnIdentity("host/mikev-ws");
Uri uri = new Uri("http://id.web/Services/EchoService.svc");
var address = new EndpointAddress(uri, spn);
var client = new EchoServiceClient("WSHttpBinding_IEchoService", address);
client.SendEcho("Hello World");
client.Close(); 

这篇关于WCF 在运行时更改端点地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆