WCF change endpoint address at runtime

Valamas picture Valamas · Mar 1, 2011 · Viewed 122k times · Source

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"/>

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 error I get is:

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

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

Additional here is my client config, requested by 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>

Answer

Ladislav Mrnka picture Ladislav Mrnka · Mar 1, 2011

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

Actual working final version by 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();