将发现添加到我的 WCF 服务
本文关键字:WCF 服务 我的 发现 添加 | 更新日期: 2023-09-27 18:36:44
我已经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;
}
}
应用配置:
<?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>
我想做的是发现很难实现,因为我是新开发人员,将发现添加到我的WCF service
,假设我在多台计算机上安装了多个服务,客户端应用程序是打开的,我现在想要哪些服务是活动的''正在运行的,以及我可以从 Discovery 接收的所有这些数据。我尝试阅读几篇文章,但正如我提到的,我不明白该怎么做,我希望得到一些帮助。
使用 WCF 发现有点复杂,根据我的经验,很少有人真正使用它,但它确实有效。此 MSDN 文章包含将发现添加到服务和客户端配置文件所需的所有详细信息。
WCF 发现背后的前提是,以与默认 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>
我认为最简单的方法是尝试将客户端连接到运行时计算的地址。例如:
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 绑定。您还知道您的集群位于 LAN 192.0.16.xxx,因此您可以使用公式:
地址 = "http://" + NextLanAddress() + ":" + 端口 + "/" + 服务名称 + "/";