2011-08-15 18 views
3

私は、同じPCにローカルに展開することも、消費するサービスとは離れた場所に展開できるディスカバリを使用するアプリケーションを持っています。名前付きパイプのバインディングをWCFディスカバリ経由で公開する方法はありますか?そうでない場合は、サービスが最も適切なバインディングを決定するために発見された後、私が交渉することができると思います。WCF検出を使用して、名前付きパイプを使用しているWCFエンドポイントを公開することはできますか?

答えて

5

はい、可能です。しかし、アドレスは "localhost"(net.pipeの唯一の可能なアドレス)としてリストされるので、サービスが実際にディスカバリークライアントと同じマシンで実行されていることを確認するためには、ある種のテストが必要になるでしょう。

public class StackOverflow_7068743 
{ 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     string Echo(string text); 
    } 
    public class Service : ITest 
    { 
     public string Echo(string text) 
     { 
      return text + " (via " + OperationContext.Current.IncomingMessageHeaders.To + ")"; 
     } 
    } 
    public static void Test() 
    { 
     string baseAddressHttp = "http://" + Environment.MachineName + ":8000/Service"; 
     string baseAddressPipe = "net.pipe://localhost/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddressHttp), new Uri(baseAddressPipe)); 
     host.Description.Behaviors.Add(new ServiceDiscoveryBehavior()); 
     host.AddServiceEndpoint(new UdpDiscoveryEndpoint()); 
     host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 
     host.AddServiceEndpoint(typeof(ITest), new NetNamedPipeBinding(NetNamedPipeSecurityMode.None), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint()); 
     FindResponse findResponse = discoveryClient.Find(new FindCriteria(typeof(ITest))); 
     Console.WriteLine(findResponse.Endpoints.Count); 

     EndpointAddress address = null; 
     Binding binding = null; 
     foreach (var endpoint in findResponse.Endpoints) 
     { 
      if (endpoint.Address.Uri.Scheme == Uri.UriSchemeHttp) 
      { 
       address = endpoint.Address; 
       binding = new BasicHttpBinding(); 
      } 
      else if (endpoint.Address.Uri.Scheme == Uri.UriSchemeNetPipe) 
      { 
       address = endpoint.Address; 
       binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None); 
       break; // this is the preferred 
      } 

      Console.WriteLine(endpoint.Address); 
     } 

     if (binding == null) 
     { 
      Console.WriteLine("No known bindings"); 
     } 
     else 
     { 
      ChannelFactory<ITest> factory = new ChannelFactory<ITest>(binding, address); 
      ITest proxy = factory.CreateChannel(); 
      Console.WriteLine(proxy.Echo("Hello")); 
      ((IClientChannel)proxy).Close(); 
      factory.Close(); 
     } 

     Console.Write("Press ENTER to close the host"); 
     Console.ReadLine(); 
     host.Close(); 
    } 
} 
+0

サービス彼は、ASP.netのWebアプリケーションでホストされている場合は、名前付きパイプのエンドポイントのベースアドレスは「net.pipe:// {macinename} /サービス」です。この場合、同じマシン上のクライアントは、マシン名の代わりに「localhost」を使用できますか? –

関連する問題