开发者

Is there a way to check availability of the services defined in the App.config <system.serviceModel> section?

开发者 https://www.devze.com 2023-03-18 21:23 出处:网络
I have a large project which consumes a lot of WCF services. Endpoints are defined in <system.serviceModel> section of my App.config file.

I have a large project which consumes a lot of WCF services. Endpoints are defined in <system.serviceModel> section of my App.config file.

We can enumerate these endpoints the following way:

ClientSection clientSection = ConfigurationManager.GetSection("system.serviceModel/client") as ClientSec开发者_开发百科tion;

Is there a way to "ping" somehow each of these endpoints to make sure that the service behind it is alive (or not)? I don't want to implement Ping() method for each service explicitly and call it to check service availability (actually, I can't do that because some WCF services my project uses is third-party services). I just want to make sure that each endpoint in App.config works (or not), without invoking any service methods through the service proxy.


Without implementing a specific 'Monitor' interface, the easiest method that I can think of would be to perform a standard GET with a WebRequest (assuming http WCF) or Ping (assuming tcp WCF) using the endpoint address.

If using http, this will at least confirm that the service is accessible, but not much else.

If using tcp, ping will only confirm that the host is available.

Ping example; http://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx

WebRequest example; http://msdn.microsoft.com/en-us/library/system.net.webrequest(v=vs.71).aspx


You can add a discovery endpoint to your service by modifying the config file. Then you will be able to query the local network (via UDP Ad-hoc discovery) to find (or confirm the existence of) services that provide the specific service contract you are looking for.

The config file for the service will look something like this:

 <system.serviceModel>
<services>
  <service name="MyServiceLibrary.Service1">
    <host>
      <baseAddresses>
        <!--Use * instead of localhost, so that the URI returned by discovery will display the machine name correctly-->
        <add baseAddress = "net.tcp://*:8887/Design_Time_Addresses/MyServiceLibrary/Service1/" />
        <add baseAddress = "http://localhost:8732/Design_Time_Addresses/MyServiceLibrary/Service1/" />
      </baseAddresses>
    </host>
    <endpoint address="" binding="netTcpBinding" contract="MyServiceLibrary.IService1">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <!--This is the endpoint used in service discovery-->
    <endpoint name ="udpDiscovery" kind ="udpDiscoveryEndpoint" />
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
      <!--This denotes that the service should allow the discovery behavior-->
      <serviceDiscovery />
    </behavior>
  </serviceBehaviors>
</behaviors>

And the code on the client will look like this:

 DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());
 FindResponse response = discoveryClient.Find(new FindCriteria(typeof(ServiceReference1.IService1)));

 if (response.Endpoints.Count > 0)
 {
     foreach (EndpointDiscoveryMetadata metaData in response.Endpoints)
     {
       // Add whatever logic you want to use to find the expected endpoint
     }
 }

The advantage is that you can add search criteria to specifically describe the service you want to find. The only problem with ad-hoc UDP is that it can be a little slow. You can improve the speed by providing the information about services from a dedicated discovery server.

0

精彩评论

暂无评论...
验证码 换一张
取 消