Wednesday, September 2, 2015

How to get the IP-address of the client in a WCF-service.

If you need to know at what address an appeal to come to your WCF-services, then you can do this in several ways, depending on the version of the .Net Framework. Let's order.

In version 3.0 (ie, the first version of .Net, which became available WCF) the assurances Microsoft does not guarantee there are ways to do this. In principle, if the service is hosted on IIS, you can try a couple of distortions, such as obtaining IP-address of the client from the server logs (although there is a question how to determine which of the log entries corresponding to the current request). In general, I propose to adhere to Microsoft's position on this issue;)
In the version of .Net 3.5 in WCF class appears System.ServiceModel.OperationContext, so that the plug assembly System.ServiceModel here and use this code, which will return to the IP client:

?
1
2
3
4
5
<span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left"> OperationContext context = OperationContext.Current;</span> OperationContext context = OperationContext.Current;</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">MessageProperties prop = context.IncomingMessageProperties;</span> MessageProperties prop = context.IncomingMessageProperties;</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">RemoteEndpointMessageProperty endpoint = prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;</span> RemoteEndpointMessageProperty endpoint = prop [RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">//получаем IP клиента.</span> // get the client IP.</span>
           <span class="notranslate" onmouseover="_tipon(this)" onmouseout="_tipoff()"><span class="google-src-text" style="direction: ltr; text-align: left">string ipAddr = endpoint.Address;</span> string ipAddr = endpoint.Address;</span>


Alternatively, if you are not going to use the protocols other than HTTP, it is possible to use a different, easier way. It's enough to a class that implements your service tag attribute AspNetCompatibilityRequirements:

[AspNetCompatibilityRequirements (RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

Now your WCF-service will use all the ASP.NET pipeline with all its consequences. That is, you can easily get the IP address of the client with the help of HttpContext.Current.Request.UserHostAddress.

In versions of .Net 4.0 and older can use a third option, however, it is only a variation of the second - use appeared in this version of .Net configuration element <serviceHostingEnvironment>. To do this, open the web.config WCF-service and add to the section <system.serviceModel> here the following line:

<servicehostingenvironment aspnetcompatibilityenabled = "true">

Everything is now also possible to use the HttpContext.

By Unknown with No comments

0 коммент.:

Post a Comment