如何从 wcf 服务端检测绑定 [英] How to detect binding from wcf service end

查看:26
本文介绍了如何从 wcf 服务端检测绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 wcf 服务,其中多个端点具有不同类型的绑定,如 tcp、basichttp、wshttp 等.

suppose i have one wcf service with multiple endpoint having different type of binding like tcp, basichttp, wshttp etc.

<endpoint address ="" binding="wsHttpBinding"
                contract="NorthwindServices.ServiceContracts.ICustomerService"
                bindingNamespace = "http://dotnetmentors.com/services/customer" />
    <endpoint address="mex" binding="mexHttpBinding" 
                 contract="IMetadataExchange"/>

    <endpoint address ="" binding ="netNamedPipeBinding" 
                contract ="NorthwindServices.ServiceContracts.ICustomerService"
                bindingNamespace = "http://dotnetmentors.com/services/customer"  />
    <endpoint address="mex" binding="mexNamedPipeBinding" 
                 contract="IMetadataExchange"/>

因此客户端可以使用具有不同绑定的任何端点来调用我的服务.当客户端进行调用并且请求将进入服务端时,我如何以编程方式检测客户端正在使用哪个绑定...是 tcp、basichttp、wshttp 吗?

so client can call my service using any endpoint having different binding. when client make call and when request will go to service end then how could i programmatically detect which binding client is using...is it tcp, basichttp, wshttp ?

所以用代码示例指导我,它帮助我从服务中检测哪个协议&绑定客户端正在使用.谢谢

so guide me with code sample which help me to detect from service that which protocol & binding client is using. thanks

推荐答案

你到底为什么关心这个?总体而言,您的服务真的不应该关心客户端用来访问服务的哪个绑定会影响服务行为(在这种情况下,拥有多个单独的服务可能更有意义).

Why exactly do you care about this? Your service really shouldn't care, overall, which binding the client used to get to the service if that is going to affect the service behavior (in that case, it might really make more sense to have multiple separate services).

也就是说,我认为一种可能的方法是查看服务的端点地址,我认为您可以使用这样的方法(未经测试):

That said, I think one possible way would be to look at the endpoint address for the service, which I think you can get using something like this (untested):

var opCtxt = OperationContext.Current;
var epAddress = opCtxt.EndpointDispatcher.EndpointAddress;
var uri = epAddress.Uri;

这篇关于如何从 wcf 服务端检测绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆