(직접) 수 없습니다. 메시지 버전 (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();
}
}
많은 도움을 주셔서 감사합니다. – GazTheDestroyer