WCF zmiana adresu punktu końcowego w czasie wykonywania

Mój pierwszy przykład WCF działa. Mam hosta na stronie, która ma wiele wiązań. Z tego powodu dodałem to do mojej sieci.config.

<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>

This is my default binding http://id.web , który działa z poniższym kodem.

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

Próbuję teraz ustawić adres punktu końcowego w czasie wykonywania. Mimo że jest to ten sam adres powyższego kodu.

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

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

Otrzymuję błąd:

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

Proszę zasugerować, Jak mogę zmienić adres punktu końcowego w czasie pracy?

Dodatkowo tutaj jest mój klient config, wymagane przez 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>
Author: BCdotWEB, 2011-03-01

4 answers

Więc twój adres końcowy zdefiniowany w pierwszym przykładzie jest niekompletny. Należy również zdefiniować tożsamość punktu końcowego, jak pokazano w konfiguracji klienta. W kodzie możesz spróbować tego:

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

Aktualna robocza wersja ostateczna 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(); 
 28
Author: Ladislav Mrnka,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-01 10:37:49

To prosty przykład tego, czego użyłem w ostatnim teście. Musisz upewnić się, że ustawienia zabezpieczeń są takie same na serwerze i kliencie.

var myBinding = new BasicHttpBinding();
myBinding.Security.Mode = BasicHttpSecurityMode.None;
var myEndpointAddress = new EndpointAddress("http://servername:8732/TestService/");
client = new ClientTest(myBinding, myEndpointAddress);
client.someCall();
 20
Author: John Petrak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-01 06:21:03

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("","",""));
 14
Author: Abdusselam,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-11-08 10:55:15

Przechowujemy nasze adresy URL w bazie danych i ładujemy je w czasie wykonywania.

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

Realizacja

var client = new ServiceClientFactory<yourServiceChannelInterface>().Create(newUrl);
 6
Author: LawMan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-29 14:57:08