WCF Discovery .NET 4:配置/以编程方式定义的问题

发布于 2024-11-09 08:19:01 字数 2127 浏览 0 评论 0原文

我有一个启用发现的 WCF 服务,现在我想将客户端连接到它。

问题:当我使用 udp 端点(1.)并尝试以编程方式发现服务时,它可以工作...当我使用 App.config 方法(2.)时,它不能(错误:未发现端点)。

在我看来,这两个解决方案的“udp 发现结果”应该是相同的,但不幸的是它不是......

1。以编程方式(有效)

代码:

        DiscoveryClient discClient = new DiscoveryClient("udpDiscoveryEndpoint");
        FindCriteria fCriteria = new FindCriteria(typeof(IAlarmServer));
        fCriteria.Duration = TimeSpan.FromSeconds(5);
        fCriteria.MaxResults = 1;
        FindResponse fResponse = discClient.Find(fCriteria);

        EndpointAddress address = fResponse.Endpoints[0].Address;
        Console.WriteLine("Address found: " + address.ToString());

配置:

<system.serviceModel>
  <client>
     <endpoint name="udpDiscoveryEndpoint" kind="udpDiscoveryEndpoint" />
  </client>
</system.serviceModel>

2。使用 App.config 和“集成到端点”方法的方法(不起作用!)

代码:

        var Proxy = new AlarmServerClient("IAlarmServer"); // Default client generated by Visual Studio
        Proxy.SomeMethod(); // throw no endpoints discovered exception

配置:

<standardEndpoints>
  <dynamicEndpoint>
    <standardEndpoint name="discoveryDynamicEndpointConfiguration">
      <discoveryClientSettings>
        <findCriteria duration="00:00:05" maxResults="1">
          <types>
            <add name="AlarmServiceRef.IAlarmServer"/>
          </types>
        </findCriteria>
        <endpoint kind="udpDiscoveryEndpoint"/>
      </discoveryClientSettings>
    </standardEndpoint>
  </dynamicEndpoint>
</standardEndpoints>

配置:

  <client>
     <endpoint binding="basicHttpBinding" bindingConfiguration="DefaultBasicHttpBinding" contract="AlarmServiceRef.IAlarmServer" name="IAlarmServer"
            kind="dynamicEndpoint"
            endpointConfiguration="discoveryDynamicEndpointConfiguration"/>
  </client>

有什么想法为什么会发生这种情况吗?

i have a discovery enabled WCF service and now i want to connect the client to it.

Problem: When i use the udp endpoint ( 1. ) and try to programmatically discover the service, it works... When i use the App.config approach ( 2. ) it does not ( Error: No endpoints discovered ).

To me it seems the "udp discovery result" of both of the solutions should be the same, but unfortunately it isn't...

1. Programmatically approach (works ):

Code:

        DiscoveryClient discClient = new DiscoveryClient("udpDiscoveryEndpoint");
        FindCriteria fCriteria = new FindCriteria(typeof(IAlarmServer));
        fCriteria.Duration = TimeSpan.FromSeconds(5);
        fCriteria.MaxResults = 1;
        FindResponse fResponse = discClient.Find(fCriteria);

        EndpointAddress address = fResponse.Endpoints[0].Address;
        Console.WriteLine("Address found: " + address.ToString());

Config:

<system.serviceModel>
  <client>
     <endpoint name="udpDiscoveryEndpoint" kind="udpDiscoveryEndpoint" />
  </client>
</system.serviceModel>

2. Approach with App.config and "integrated into endpoint" approach (does not work!):

Code:

        var Proxy = new AlarmServerClient("IAlarmServer"); // Default client generated by Visual Studio
        Proxy.SomeMethod(); // throw no endpoints discovered exception

Config:

<standardEndpoints>
  <dynamicEndpoint>
    <standardEndpoint name="discoveryDynamicEndpointConfiguration">
      <discoveryClientSettings>
        <findCriteria duration="00:00:05" maxResults="1">
          <types>
            <add name="AlarmServiceRef.IAlarmServer"/>
          </types>
        </findCriteria>
        <endpoint kind="udpDiscoveryEndpoint"/>
      </discoveryClientSettings>
    </standardEndpoint>
  </dynamicEndpoint>
</standardEndpoints>

Config:

  <client>
     <endpoint binding="basicHttpBinding" bindingConfiguration="DefaultBasicHttpBinding" contract="AlarmServiceRef.IAlarmServer" name="IAlarmServer"
            kind="dynamicEndpoint"
            endpointConfiguration="discoveryDynamicEndpointConfiguration"/>
  </client>

Any ideas why this is happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-11-16 08:19:01

当您通过 IIS 托管服务并进行发现时,需要注意以下几点:

  1. 在服务配置中,确保服务名称与
    类名包括命名空间
  2. 你必须确保服务正在运行才能发现
    与客户一起。您可以手动浏览到服务的 .svc 文件
    或使用 AppFabric 托管服务并将 AutoStart 设置为 true(您
    也可以在 web.config 中指定)
  3. 在服务配置中,您必须发出您想要的类型
    在客户端上的查找条件中使用

以下是设置服务端点的服务器配置示例。请注意,服务“名称”属性是实现该服务的类的完整命名空间。

服务配置

<services>
      <service name="WcfDiscovery.Services.BuzzerService" behaviorConfiguration="sb1" >
        <endpoint binding="basicHttpBinding" contract="WcfDiscovery.Contracts.IAlarmServer" address="" behaviorConfiguration="eb1"  />
        <endpoint kind="udpDiscoveryEndpoint"  />
      </service>
    </services>

还要确保将发现行为添加到服务

服务配置

<serviceBehaviors>
        <behavior name="sb1">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceDiscovery />
        </behavior>
      </serviceBehaviors>

中,因为我希望客户端能够通过类型(WcfDiscovery.Contracts.IAlarmServer)发现服务,所以我还必须在端点的行为配置中指定这一点( eb1)

服务配置

<endpointBehaviors>
        <behavior name="eb1">
          <endpointDiscovery enabled="true">
            <types>
              <add name="WcfDiscovery.Contracts.IAlarmServer" />
            </types>
          </endpointDiscovery>
        </behavior>
      </endpointBehaviors>

现在在客户端我可以使用 findCriteria 发现服务。请注意,查找条件中的类型必须与服务发出的类型相匹配(在服务类型列表中)

客户端配置

<standardEndpoints>
      <dynamicEndpoint>
        <standardEndpoint name="dynamicEndpointConfiguration">
          <discoveryClientSettings >
            <endpoint kind="udpDiscoveryEndpoint" />

            <findCriteria maxResults="2">
              <types>
                <add name="WcfDiscovery.Contracts.IAlarmServer" />
              </types>
            </findCriteria>

          </discoveryClientSettings>
        </standardEndpoint>
      </dynamicEndpoint>
    </standardEndpoints>

这是客户端端点配置

客户端配置

<client>
      <endpoint kind="dynamicEndpoint" name="endpoint" binding="basicHttpBinding" contract="WcfDiscovery.Contracts.IAlarmServer" endpointConfiguration="dynamicEndpointConfiguration" />
    </client>

最后,我可以在控制台应用程序中发现服务,如下所示:

ChannelFactory<IAlarmServer> factory = new ChannelFactory<IAlarmServer>("endpoint");
            var proxy = factory.CreateChannel();

            Console.WriteLine(proxy.SoundAlarm());

希望这个有帮助!

Couple of things when you're hosting a service with discovery via IIS

  1. In the service configuration make sure the service name matches the
    class name including the namespace
  2. You have to make sure the service is running before you can discover
    it with a client. You can manually browse to the service's .svc file
    or host the service with AppFabric and set AutoStart to true (You
    can also specify that in the web.config)
  3. In the service configuration you have to emit the type you're going
    to use in the find criteria on the client

Here is a sample of the server configuration setting up the service endpoints. Note that the service "name" attribute is the full namespace to the class that is implementing the service.

Service Config

<services>
      <service name="WcfDiscovery.Services.BuzzerService" behaviorConfiguration="sb1" >
        <endpoint binding="basicHttpBinding" contract="WcfDiscovery.Contracts.IAlarmServer" address="" behaviorConfiguration="eb1"  />
        <endpoint kind="udpDiscoveryEndpoint"  />
      </service>
    </services>

Also make sure to add the discovery behavior to the service

Service Config

<serviceBehaviors>
        <behavior name="sb1">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
          <serviceDiscovery />
        </behavior>
      </serviceBehaviors>

Since I want clients to be able to discover the service by the type (WcfDiscovery.Contracts.IAlarmServer) I have to also specify that in the behavior configuration for the endpoint (eb1)

Service Config

<endpointBehaviors>
        <behavior name="eb1">
          <endpointDiscovery enabled="true">
            <types>
              <add name="WcfDiscovery.Contracts.IAlarmServer" />
            </types>
          </endpointDiscovery>
        </behavior>
      </endpointBehaviors>

Now on the client side I can discover the service using the findCriteria. Note that the type in the find criteria has to match the one emitted by the service (in the services types list)

Client Config

<standardEndpoints>
      <dynamicEndpoint>
        <standardEndpoint name="dynamicEndpointConfiguration">
          <discoveryClientSettings >
            <endpoint kind="udpDiscoveryEndpoint" />

            <findCriteria maxResults="2">
              <types>
                <add name="WcfDiscovery.Contracts.IAlarmServer" />
              </types>
            </findCriteria>

          </discoveryClientSettings>
        </standardEndpoint>
      </dynamicEndpoint>
    </standardEndpoints>

Here is the client endpoint configuration

Client Config

<client>
      <endpoint kind="dynamicEndpoint" name="endpoint" binding="basicHttpBinding" contract="WcfDiscovery.Contracts.IAlarmServer" endpointConfiguration="dynamicEndpointConfiguration" />
    </client>

Finally, I can discover the service in a console app like this:

ChannelFactory<IAlarmServer> factory = new ChannelFactory<IAlarmServer>("endpoint");
            var proxy = factory.CreateChannel();

            Console.WriteLine(proxy.SoundAlarm());

Hope this helps!

森林很绿却致人迷途 2024-11-16 08:19:01

您不能只向您的客户端项目添加服务引用吗?它将为您生成配置。

Can't you just add a Service Reference to your client project? It will generate the configuration for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文