如何在WCF客户端中找到服务地址

时间:2018-10-23 06:44:22

标签: wcf intranet

我在同一台计算机上创建了WCF服务和客户端,服务地址写入了客户端的代码中,因此我可以轻松找到服务并创建与服务的连接。
然后,我尝试将它们部署到Intranet中。第一个问题是:客户端如何找到服务器的地址。在实际环境中,客户可以在Intranet中的任何计算机上安装服务,有什么方法可以让客户端找到服务器地址?

1 个答案:

答案 0 :(得分:1)

WCF服务可以将特定终结点作为发现终结点公开给所有客户端,以便客户端可以找到该服务所在的位置。您甚至可以使用UDP多播来使客户端发现该服务。

您可以检查正式文件。

https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-discovery

我制作了一个演示,希望它对您有用。

服务器。

class Program
{
    static void Main(string[] args)
    {
        using (ServiceHost sh=new ServiceHost(typeof(MyService)))
        {
            sh.Open();
            Console.WriteLine("serivce is ready...");

            Console.ReadLine();
            sh.Close();
        }
    }
}
[ServiceContract]
public interface IService
{
    [OperationContract]
    string SayHello();
}
public class MyService : IService
{
    public string SayHello()
    {
        return "Hello, I am a Clown";
    }

}

服务器app.config

<system.serviceModel>
<services>
  <service name="DiscoveryEndpoint20181024.MyService" behaviorConfiguration="mybehavior">
    <endpoint address="http://10.157.18.188:4800" binding="wsHttpBinding" contract="DiscoveryEndpoint20181024.IService"></endpoint>
    <endpoint kind="discoveryEndpoint" address="http://localhost:9999" binding="wsHttpBinding"></endpoint>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="mybehavior">
      <serviceMetadata />
      <serviceDiscovery />
    </behavior>
  </serviceBehaviors>
</behaviors>

客户。

class Program
{
    static void Main(string[] args)
    {
        DiscoveryClient client = new DiscoveryClient("my_client");
        client.ClientCredentials.Windows.ClientCredential.UserName = "administrator";
        client.ClientCredentials.Windows.ClientCredential.Password = "abcd1234!";
        FindCriteria crit = new FindCriteria(typeof(IService));
        FindResponse resp = client.Find(crit);

        if (resp != null && resp.Endpoints.Count > 0)
        {
            EndpointDiscoveryMetadata epaddrMtd = resp.Endpoints[0];
            ChannelFactory<IService> factory = new ChannelFactory<IService>(new WSHttpBinding(), epaddrMtd.Address);
            factory.Credentials.Windows.ClientCredential.UserName = "administrator";
            factory.Credentials.Windows.ClientCredential.Password = "abcd1234!";
            IService service = factory.CreateChannel();
            var result=service.SayHello();
            Console.WriteLine(result);
            Console.ReadLine();
        }

    }
}
[ServiceContract]
public interface IService
{
    [OperationContract]
    string SayHello();
}

class DemoService : IService
{
    public string SayHello()
    {
        OperationContext context = OperationContext.Current;
        return $"the address:{OperationContext.Current.Channel.LocalAddress.Uri}";
    }

}

Client.config

<system.serviceModel>
    <client>
      <endpoint name="my_client" kind="discoveryEndpoint" address="http://10.157.18.188:9999" binding="wsHttpBinding"></endpoint>
    </client>
  </system.serviceModel>