43

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>
BCdotWEB
  • 1,009
  • 1
  • 14
  • 35
Valamas
  • 24,169
  • 25
  • 107
  • 177

5 Answers5

33

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(); 
Valamas
  • 24,169
  • 25
  • 107
  • 177
Ladislav Mrnka
  • 360,892
  • 59
  • 660
  • 670
  • I have modified my post displaying the code i used based on your sample and the error I am now getting. Thank you. – Valamas Mar 01 '11 at 08:56
  • @Valamas: Are you sure that your didn't accidentally change some part of config only on one side of communication? What happens if you only call your initial code without passing EndpointAddress from code? – Ladislav Mrnka Mar 01 '11 at 09:46
  • It is working now. the problem was the config change I added while working with John Petrak. Not helped by leaving that in when I left the office for an hour. Thank you very much for your help. Perfect, cheers :) – Valamas Mar 01 '11 at 10:07
  • This did the trick for me. For others, like me who also need to pass credentials, you can add the following lines immediately after `client` is declared: `client.ClientCredentials.UserName.UserName = "myUsername"; client.ClientCredentials.UserName.Password = "myPassword";` – Tod Birdsall Aug 15 '17 at 13:35
23

This is a simple example of what I used for a recent test. You need to make sure that your security settings are the same on the server and client.

var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndpointAddress = new EndpointAddress("http://servername:8732/TestService/");
client = new ClientTest(myBinding, myEndpointAddress);
client.someCall();
John Petrak
  • 2,898
  • 20
  • 31
  • In the web.config i changed to ``. I have appended my current code and error to my original post. – Valamas Mar 01 '11 at 07:46
16

app.config

<client>
    <endpoint address="" binding="basicHttpBinding" 
    bindingConfiguration="LisansSoap" 
    contract="Lisans.LisansSoap" 
    name="LisansSoap" />
</client>

program

 Lisans.LisansSoapClient test = new LisansSoapClient("LisansSoap",
                         "http://webservis.uzmanevi.com/Lisans/Lisans.asmx");

 MessageBox.Show(test.LisansKontrol("","",""));
stuartd
  • 70,509
  • 14
  • 132
  • 163
Abdusselam
  • 161
  • 1
  • 2
9

We store our URLs in a database and load them at runtime.

public class ServiceClientFactory<TChannel> : ClientBase<TChannel> where TChannel : class
{
    public TChannel Create(string url)
    {
        this.Endpoint.Address = new EndpointAddress(new Uri(url));
        return this.Channel;
    }
}

Implementation

var client = new ServiceClientFactory<yourServiceChannelInterface>().Create(newUrl);
LawMan
  • 3,469
  • 1
  • 29
  • 32
0

You can create an EndpointAddress and use an instance of the client to determine the binding

        EndpointAddress a = new("http://localhost:50415/Service1.svc");
        Service1Client c = new(new Service1Client().Endpoint.Binding, a);
        string result = c.GetData(3);

If you don't mind initializing the client twice

symbiont
  • 1,428
  • 3
  • 21
  • 27