我有WCF service
如何在一台计算机上运行,simple comsole application client
在另一台计算机上运行。
服务器做一些非常简单的事情:一个返回客户端发送的2号值的方法:
[ServiceContract]
public interface IMySampleWCFService
{
[OperationContract]
int Add(int num1, int num2);
[OperationContract]
void CreateDirectory(string directory);
[OperationContract]
string GetVendorToRun(string path);
}
public class MySampleWCFService : IMySampleWCFService
{
public int Add(int num1, int num2)
{
return num1 + num2;
}
}
App.config中:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="WCFServiceHostingInWinService.MySampleWCFService">
<endpoint
name="ServiceHttpEndPoint"
address=""
binding="basicHttpBinding"
contract="WCFServiceHostingInWinService.IMySampleWCFService">
</endpoint>
<endpoint
name="ServiceMexEndPoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
我想做什么并且难以实现,因为我是一名新开发人员,将Discovery添加到我的WCF service
,假设我有多个服务安装在多台机器上,客户端应用程序是打开的,我想要什么到现在哪些服务还在运行以及我可以从Discovery收到的所有这些数据。
我尝试阅读几篇文章,但正如我提到的那样,不明白该怎么做,我会很乐意帮助。
答案 0 :(得分:2)
使用WCF Discovery有点令人费解,很少有人在我的经验中实际使用它,但确实有效。此MSDN article具有将Discovery添加到服务和服务所需的所有详细信息。客户端配置文件。
WCF Discovery背后的前提是您以与默认MEX端点类似的方式公开新的发现端点。 MEX端点允许服务向客户端提供WSDL。 WCF发现端点通过基于UDP的客户端UDP响应响应向客户端公开已配置的服务。上面的概述链接提供了更多细节。
以下是您的服务配置的样子:
<system.serviceModel>
<services>
<service name="WCFServiceHostingInWinService.MySampleWCFService">
<endpoint
name="ServiceHttpEndPoint"
address=""
binding="basicHttpBinding"
contract="WCFServiceHostingInWinService.IMySampleWCFService"
behaviorConfiguration="endpointDiscoveryBehavior">
</endpoint>
<endpoint
name="ServiceMexEndPoint"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
<!-- Discovery Endpoint: -->
<endpoint kind="udpDiscoveryEndpoint" />
<host>
<baseAddresses>
<add baseAddress="http://192.0.16.250:8733/MySampleWCFService/" />
</baseAddresses>
</host>
</service>
<!-- Announcement Listener Configuration -->
<service name="AnnouncementListener">
<endpoint kind="udpAnnouncementEndpoint" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="True" />
<serviceDiscovery>
<announcementEndpoints>
<endpoint kind="udpAnnouncementEndpoint"/>
</announcementEndpoints>
</serviceDiscovery>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="endpointDiscoveryBehavior">
<endpointDiscovery enabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
答案 1 :(得分:0)
我认为最简单的方法是尝试将客户端连接到您在运行时计算的地址。例如:
static void Main(string[] args)
{
var addresses = new List<string>
{
@"http://192.168.1.1:8730/MySampleWCFService/",
@"http://localhost:8731/MySampleWCFService/",
@"http://localhost:8732/MySampleWCFService/",
@"http://localhost:8733/MySampleWCFService/",
};
foreach (var address in addresses)
{
var client = new MySampleWCFServiceClient(new BasicHttpBinding(), new EndpointAddress(address));
try
{
client.Open();
client.Add(0, 1);
Console.WriteLine("Connected to {0}", address);
}
catch (EndpointNotFoundException)
{
Console.WriteLine("Service at {0} is unreachable", address);
}
}
Console.ReadLine();
}
在我的情况下,我创建了一个包含地址的列表,但在您的情况下,您可以使用一些预定义的规则构建地址。例如,您知道服务使用具有某些名称和端口的http绑定。您也知道您的群集位于192.0.16.xxx LAN中,因此您可以使用公式:
address =“http://”+ NextLanAddress()+“:”+ port +“/”+ serviceName +“/”;