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

发布于 2024-11-15 02:51:35 字数 222 浏览 3 评论 0原文

我有一个在运行时使用 DuplexChannelFactory 生成的 WCF 代理。

仅给出从 DuplexChannelFactory 返回的服务接口,如何访问绑定信息?

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

I have a WCF proxy generated at runtime with DuplexChannelFactory.

How can I access the binding information given only the service interface returned from DuplexChannelFactory?

I can get most stuff by casting to an IClientChannel, but I can't seem to find binding info on there. The closest I can get is IClientChannel.RemoteAddress which is an endpoint, but that doesn't seem to have binding info either. :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青春有你 2024-11-22 02:51:35

你不能(直接)。您可以从通道中获取一些信息,例如消息版本 (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();
    }
}

You can't (directly). There are a few things which you can get from the channel, such as the message version (channel.GetProperty<MessageVersion>()), and other values. But the binding isn't one of those. The channel is created after the binding is "deconstructed" (i.e., expanded into its binding elements, while each binding element can add one more piece to the channel stack.

If you want to have the binding information in the proxy channel, however, you can add it yourself, using one of the extension properties of the context channel. The code below shows one example of that.

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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文