WCF change endpoint address at runtime

后端 未结 4 643
囚心锁ツ
囚心锁ツ 2020-11-28 04: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.



        
相关标签:
4条回答
  • 2020-11-28 05:05

    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();
    
    0 讨论(0)
  • 2020-11-28 05:10

    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("","",""));
    
    0 讨论(0)
  • 2020-11-28 05:11

    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(); 
    
    0 讨论(0)
  • 2020-11-28 05:19

    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);
    
    0 讨论(0)
提交回复
热议问题