Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将发现添加到我的WCF服务_C#_Wcf_Wcf Discovery - Fatal编程技术网

C# 将发现添加到我的WCF服务

C# 将发现添加到我的WCF服务,c#,wcf,wcf-discovery,C#,Wcf,Wcf Discovery,我有WCF服务如何在一台机器上运行,以及在另一台机器上运行的简单comsole应用程序客户端。 服务器执行的操作非常简单:一种方法返回客户端发送的2号值: [ServiceContract] public interface IMySampleWCFService { [OperationContract] int Add(int num1, int num2); [OperationContract] void CreateDirectory(string d

我有
WCF服务
如何在一台机器上运行,以及
在另一台机器上运行的简单comsole应用程序客户端
。 服务器执行的操作非常简单:一种方法返回客户端发送的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>

因为我是一名新开发人员,所以我想做并发现很难实现的事情是将发现添加到我的
WCF服务中
,假设我在多台机器上安装了多个服务,客户机应用程序打开了什么,我现在想知道哪些服务处于活动状态\正在运行,以及我可以从发现中收到的所有这些数据。
我试着读了几篇文章,但正如我提到的,我不知道如何做,我希望得到一些帮助。

我认为最简单的方法是尝试将客户端连接到运行时计算的地址。例如:

    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();
    }
static void Main(字符串[]args)
{
var地址=新列表
{
@"http://192.168.1.1:8730/MySampleWCFService/",
@"http://localhost:8731/MySampleWCFService/",
@"http://localhost:8732/MySampleWCFService/",
@"http://localhost:8733/MySampleWCFService/",
};
foreach(地址中的变量地址)
{
var client=new MySampleWCFServiceClient(new BasicHttpBinding(),new EndpointAddress(address));
尝试
{
client.Open();
客户端。添加(0,1);
WriteLine(“连接到{0}”,地址);
}
捕获(EndpointNotFoundException)
{
WriteLine(“无法访问{0}处的服务”,地址);
}
}
Console.ReadLine();
}
在我的例子中,我创建了一个带有地址的列表,但在您的例子中,您可以使用一些预定义的规则来构建地址。例如,您知道服务使用带有某些名称和端口的http绑定。您还知道您的集群位于192.0.16.xxx LAN中,因此您可以使用以下公式:

address=“http://”+nextlandAddress()+”:“+port+”/“+servicenaname+”/”

使用起来有点复杂,根据我的经验,实际上很少有人使用它,但它确实有效。这包含了将发现添加到服务和客户端配置文件所需的所有详细信息

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>


。我的想法是,你无法提前知道可能的地址。这看起来太复杂了……我甚至很难将其添加到app.configYup,我想“有点复杂”是一种轻描淡写的说法…;-)我可以从代码而不是从配置文件添加发现吗?是的,显示的是示例代码。否,您可以使用配置文件,也可以在代码中动态添加发现。你不需要两者都用。