Wcf 如何以编程方式获取客户端代理正在使用的绑定?

Wcf 如何以编程方式获取客户端代理正在使用的绑定?,wcf,binding,endpoint,Wcf,Binding,Endpoint,我使用DuplexChannelFactory在运行时生成了一个WCF代理 如果只有从DuplexChannelFactory返回的服务接口,如何访问绑定信息 我可以通过强制转换到IClientChannel来获得大部分内容,但我似乎在那里找不到绑定信息。我能得到的最接近的是IClientChannel.RemoteAddress,它是一个端点,但似乎也没有绑定信息:-/ 你不能(直接)。您可以从该频道获得一些信息,例如消息版本(channel.GetProperty())和其他值。但绑定不是其

我使用DuplexChannelFactory在运行时生成了一个WCF代理

如果只有从DuplexChannelFactory返回的服务接口,如何访问绑定信息

我可以通过强制转换到IClientChannel来获得大部分内容,但我似乎在那里找不到绑定信息。我能得到的最接近的是IClientChannel.RemoteAddress,它是一个端点,但似乎也没有绑定信息:-/

你不能(直接)。您可以从该频道获得一些信息,例如消息版本(
channel.GetProperty()
)和其他值。但绑定不是其中之一。通道是在绑定被“解构”后创建的(即,扩展到其绑定元素中,而每个绑定元素可以向通道堆栈中再添加一个片段)

但是,如果您希望在代理通道中拥有绑定信息,您可以使用上下文通道的扩展属性之一自行添加它。下面的代码显示了一个示例

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();
    }
}
公共类堆栈溢出\u 6332575
{
[服务合同]
公共接口测试
{
[经营合同]
整数加(整数x,整数y);
}
公共类服务:ITest
{
公共整数相加(整数x,整数y)
{
返回x+y;
}
}
静态绑定GetBinding()
{
BasicHttpBinding结果=新的BasicHttpBinding();
返回结果;
}
MyExtension类:IExtension
{
公共无效附加(IContextChannel所有者)
{
}
公共无效分离(IContextChannel所有者)
{
}
公共绑定{get;set;}
}
静态void CallProxy(ITest proxy)
{
Console.WriteLine(proxy.Add(3,5));
MyExtension扩展=((IClientChannel)proxy).Extensions.Find();
if(扩展名!=null)
{
WriteLine(“Binding:{0}”,extension.Binding);
}
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
ServiceHost主机=新ServiceHost(类型(服务),新Uri(基地址));
AddServiceEndpoint(typeof(ITest),GetBinding(),“”);
host.Open();
Console.WriteLine(“主机已打开”);
ChannelFactory=newchannelfactory(GetBinding(),newendpointaddress(baseAddress));
ITest proxy=factory.CreateChannel();
((IClientChannel)proxy).Extensions.Add(new MyExtension{Binding=factory.Endpoint.Binding});
委托书;
((IClientChannel)代理).Close();
工厂关闭();
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}