2011-06-13 1 views
6

런타임에 DuplexChannelFactory로 생성 된 WCF 프록시가 있습니다.프로그래밍 방식으로 내 클라이언트 프록시에서 사용하는 바인딩을 가져올 수 있습니까?

DuplexChannelFactory에서 반환 된 서비스 인터페이스에서만 제공되는 바인딩 정보에 어떻게 액세스합니까?

나는 IClientChannel에 캐스팅하여 대부분의 물건을 얻을 수 있지만 거기에 바인딩 정보를 찾을 수없는 것 같습니다. 내가 얻을 수있는 가장 가까운 IClientChannel.RemoteAddress는 끝점이지만 바인딩 정보도없는 것 같습니다. :/

답변

6

(직접) 수 없습니다. 메시지 버전 (channel.GetProperty<MessageVersion>()) 및 기타 값과 같이 채널에서 얻을 수있는 몇 가지 사항이 있습니다. 그러나 바인딩은 그 중 하나가 아닙니다. 채널은 바인딩이 "해체 된"후에 생성됩니다 (즉, 바인딩 요소로 확장 됨, 각 바인딩 요소는 채널 스택에 하나 이상의 조각을 추가 할 수 있음)

프록시 채널에 바인딩 정보 컨텍스트 채널의 확장 속성 중 하나를 사용하여 직접 추가 할 수 있습니다. 아래 코드는 그 한 예를 보여줍니다.

public class StackOverflow_6332575 
{ 
    [ServiceContract] 
    public interface ITest 
    { 
     [OperationContract] 
     int Add(int x, int y); 
    } 
    public class Service : ITest 
    { 
     public int Add(int x, int y) 
     { 
      return x + y; 
     } 
    } 
    static Binding GetBinding() 
    { 
     BasicHttpBinding result = new BasicHttpBinding(); 
     return result; 
    } 
    class MyExtension : IExtension<IContextChannel> 
    { 
     public void Attach(IContextChannel owner) 
     { 
     } 

     public void Detach(IContextChannel owner) 
     { 
     } 

     public Binding Binding { get; set; } 
    } 
    static void CallProxy(ITest proxy) 
    { 
     Console.WriteLine(proxy.Add(3, 5)); 
     MyExtension extension = ((IClientChannel)proxy).Extensions.Find<MyExtension>(); 
     if (extension != null) 
     { 
      Console.WriteLine("Binding: {0}", extension.Binding); 
     } 
    } 
    public static void Test() 
    { 
     string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
     ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
     host.AddServiceEndpoint(typeof(ITest), GetBinding(), ""); 
     host.Open(); 
     Console.WriteLine("Host opened"); 

     ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress)); 
     ITest proxy = factory.CreateChannel(); 

     ((IClientChannel)proxy).Extensions.Add(new MyExtension { Binding = factory.Endpoint.Binding }); 

     CallProxy(proxy); 

     ((IClientChannel)proxy).Close(); 
     factory.Close(); 

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

많은 도움을 주셔서 감사합니다. – GazTheDestroyer