这里给出服务端获取客户端IP地址信息的示例代码分析和实现过程,这里的测试主要是针对HTTP、TCP相关的协议做了4个测试。NamePipeBinding等协议不做测试了,本地协议不需要IP和端口。我们主要测试的是几个主要的协议,来验证以上的结论。
3客户端:启动宿主和添加服务引用的过程就详细介绍了。我们分别针对每个不同的服务终结点,实例化代理进行测试。宿主会输出信息,而且客户端打印返回的消息。代码如下:
1服务端:
主要是对RemoteEndpointMessageProperty属性的使用来获取地址、端口信息。具体代码如下:
//1.服务契约
[ServiceContract(Namespace = "http://www.cnblogs.com/frank_xl/")]
public interface IWCFService
{
//操作契约
[OperationContract]
string SayHelloToUser(string name);
}
//2.服务类,继承接口。实现服务契约定义的操作
public class WCFService : IWCFService
{
//实现接口定义的方法
public string SayHelloToUser(string name)
{
//提供方法执行的上下文环境
OperationContext context = OperationContext.Current;
//获取传进的消息属性
MessageProperties properties = context.IncomingMessageProperties;
//获取消息发送的远程终结点IP和端口
RemoteEndpointMessageProperty endpoint = properties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
Console.WriteLine(string.Format("Hello {0},You are from {1}:{2}", name, endpoint.Address, endpoint.Port));
return string.Format("Hello {0},You are from {1}:{2}", name, endpoint.Address, endpoint.Port);
}
}
2宿主:使用的是控制台托管宿主方式,延续上一个些列的特色。大家很应该很容易就上手了。简单介绍一下配置过程。 这里配置了HTPP和TCP相关的4个服务终结点进行测试:
<service behaviorConfiguration="WCFService.WCFServiceBehavior"
name="WCFService.WCFService">
<endpoint
address="http://localhost:8001/WCFService"
binding="wsHttpBinding" contract="WCFService.IWCFService">
</endpoint>
<endpoint
address="net.tcp://localhost:8002/WCFService"
binding="netTcpBinding" contract="WCFService.IWCFService">
</endpoint>
<endpoint
address="http://localhost:8003/WCFService"
binding="basicHttpBinding" contract="WCFService.IWCFService">
</endpoint>
<endpoint
address="http://localhost:8004/WCFService"
binding="wsDualHttpBinding" contract="WCFService.IWCFService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/"/>
</baseAddresses>
</host>
</service>
</services>
3客户端:启动宿主和添加服务引用的过程就详细介绍了。我们分别针对每个不同的服务终结点,实例化代理进行测试。宿主会输出信息,而且客户端打印返回的消息。代码如下:
//1.WSHttpBinding_IWCFService
using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
{
string strUserName = "Frank Xu Lei WSHttpBinding ";
string strMessage = "";
//通过代理调用SayHelloToUser服务
strMessage =wcfServiceProxyHttp.SayHelloToUser(strUserName);
Console.WriteLine(strMessage);
}
//2.NetTcpBinding_IWCFService
using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
{
string strUserName = "Frank Xu Lei NetTcpBinding ";
string strMessage = "";
//通过代理调用SayHelloToUser服务
strMessage = wcfServiceProxyHttp.SayHelloToUser(strUserName);
Console.WriteLine(strMessage);
}
//3.BasicHttpBinding_IWCFService
using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
{
string strUserName = "Frank Xu Lei BasicHttpBinding ";
string strMessage = "";
//通过代理调用SayHelloToUser服务
strMessage = wcfServiceProxyHttp.SayHelloToUser(strUserName);
Console.WriteLine(strMessage);
}
//4.WSDualHttpBinding_IWCFService,
using (WCFServiceClient wcfServiceProxyHttp = new WCFServiceClient("WSHttpBinding_IWCFService"))
{
string strUserName = "Frank Xu Lei WSDualHttpBinding ";
string strMessage = "";
//通过代理调用SayHelloToUser服务
strMessage = wcfServiceProxyHttp.SayHelloToUser(strUserName);
Console.WriteLine(strMessage);
}
4测试结果:
启动宿主、客户端,进行测试测试。结果如图:
总结:
1)以上测试可以看出,客户端的每次请求,服务端都可以获取响应的地址信息:IP+Port.获得以后大家可以再进行别的处理,比如系统日志LOG、安全限制等等。
2)此特性只是对http和tcp相关的绑定器作用,其它绑定WCF没有做支持,和实际绑定的应用有关系。比如NamePipe等。
3)双向通信,这个属性可以获取服务端的信息。
4)这个属性没做欺骗检测,如果使用作为安全标准,要慎重考虑。