WCF - 需要从服务器启动客户端主机

时间:2014-02-11 06:23:28

标签: wcf

在WCF发现示例中,我有服务器和客户端主机。我需要从服务器启动客户端主机。怎么做。

在服务器中,我在发现合同中有一个方法。

    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }

在客户端主机中,我指向发现合同。

Uri baseAddress = new Uri(string.Format("http://{0}:8000/discovery/scenarios/Myservice/",System.Net.Dns.GetHostName(), Guid.NewGuid().ToString()));
Console.WriteLine(baseAddress);
using (ServiceHost serviceHost = new ServiceHost(typeof(WCFDiscoveryContract.Service1), baseAddress))
{
    serviceHost.AddServiceEndpoint(typeof(WCFDiscoveryContract.IService1), new WSHttpBinding(), string.Empty);
    serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
    serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());
    serviceHost.Open();
    Console.WriteLine("Press to terminate service.");
    Console.ReadLine();
}

在服务器中我有另一个解决方案,它搜索一个发现合同并调用方法GetData()。

ServiceClient.ServiceReference1.Service1Client client = new ServiceClient.ServiceReference1.Service1Client();
client.Endpoint.Address = serviceAddress;
string ss = client.GetData(1);

为了调用GetData方法,客户端主机应该正在运行。有没有办法从服务器启动客户端主机。

0 个答案:

没有答案