zoukankan      html  css  js  c++  java
  • Detect Client IP in WCF 3.5

    One of the features that was missing from WCF 3.0 was the ability to detect client IP address in services. For a technology that is completely built on top of SOA hence a server/client mechanism where clients are an important part of the story this looked like a big lack!

    There are various situations where you need to retrieve the client IP address on your service side and there is no doubt that having such a feature can be a common need and request.

    However, this is a new feature added to .NET 3.5 and Windows Communication Foundation 3.5 for developers and you're now able to retrieve the client's IP address and port in your code easily.

    Suppose that I have a service contract like what you see below where there is a single method to get a string argument and return another string value.

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Runtime.Serialization;

    using System.ServiceModel;

    using System.Text;

    using System.ServiceModel.Channels;

     

    namespace ClientInfoSample

    {

        
    public class MyService : IService

        {

            
    public string GetData(string value)

            {

                OperationContext context 
    = OperationContext.Current;

                MessageProperties messageProperties 
    = context.IncomingMessageProperties;

                RemoteEndpointMessageProperty endpointProperty 
    =

                    messageProperties[RemoteEndpointMessageProperty.Name]

                    
    as RemoteEndpointMessageProperty;

     

                
    return string.Format("Hello {0}! Your IP address is {1} and your port is {2}",

                    value, endpointProperty.Address, endpointProperty.Port);

            }

        }

    }

    You simply can notice that there are a few steps to retrieve the client information from the OperationContext. First you need to get access to the current instance of the OperationContext then retrieve its IncomingMessageProperties as a MessageProperties object. The last step is to create an instance of RemoteEndpointMessageProperty by looking in the MessageProeprties collection for the name of the RemoteEndpointMessageProperty. Now RemoteEndpointMessageProperty has two separate properties to get access to the client's IP address and port.

    It's worthwhile to know that behind the scenes WCF is passing the client's IP and port (along some other information) with each message as the properties for the message. This is happening for services hosted on HTTP or TCP protocols so the important point is here and you can't apply this code for other protocols.

    I can self-host this service with a simple configuration file that is already generated by Visual Studio 2008 and just needs some modifications to apply new contract and service names.

    <?xml version="1.0" encoding="utf-8" ?>

    <configuration>

      
    <system.web>

        
    <compilation debug="true" />

      
    </system.web>

      
    <system.serviceModel>

        
    <services>

          
    <service name="ClientInfoSample.MyService" behaviorConfiguration="ClientInfoSample.MyServiceBehavior">

            
    <host>

              
    <baseAddresses>

                
    <add baseAddress = "http://localhost:8731/Design_Time_Addresses/ClientInfoSample/MyService/" />

              
    </baseAddresses>

            
    </host>

            
    <endpoint address ="" binding="wsHttpBinding" contract="ClientInfoSample.IService">

              
    <identity>

                
    <dns value="localhost"/>

              
    </identity>

            
    </endpoint>

            
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>

          
    </service>

        
    </services>

        
    <behaviors>

          
    <serviceBehaviors>

            
    <behavior name="ClientInfoSample.MyServiceBehavior">

              
    <serviceMetadata httpGetEnabled="True"/>

              
    <serviceDebug includeExceptionDetailInFaults="False" />

            
    </behavior>

          
    </serviceBehaviors>

        
    </behaviors>

      
    </system.serviceModel>

    </configuration>

    Hosting and running the service, I can test it quickly to get an output like what you see here:

    WCF Test Client

    原文url:http://nayyeri.net/blog/detect-client-ip-in-wcf-3-5/

    KidYang
  • 相关阅读:
    绝对路径和相对路径的问题
    get请求中的中文乱码问题的解决方法
    jsp中的另一种分页实现方法
    jsp中退出功能实现代码
    jsp中完整的分页显示和页面跳转功能实现的源代码
    jsp中未登录用户也可以浏览页面的功能实现代码
    date和calendar对象的转化,使用,以及插入数据库中的总结
    jsp中向数据库中插入当前时间的方法精确到秒
    硬盘方式安装 Windows 7
    HP笔记本中CQ4x系列,在XP下的未知设备与声卡设备驱动
  • 原文地址:https://www.cnblogs.com/EasyLive2006/p/1290317.html
Copyright © 2011-2022 走看看