Print Page | Close Window

HttpContext in SaveInterceptor

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4003
Printed Date: 21-Aug-2025 at 9:14am


Topic: HttpContext in SaveInterceptor
Posted By: gregweb
Subject: HttpContext in SaveInterceptor
Date Posted: 23-Feb-2013 at 2:00pm
Is it possible to access the HttpContext.Current from the SaveInterceptor?

I have tried, but it is always null.

What I am trying to do is get the IP address of the request so I can log it.

No authentication is being used - it's just for logging web site analytics.

Greg



Replies:
Posted By: kimj
Date Posted: 23-Feb-2013 at 5:51pm
The EntityServerSaveInterceptor is running within the context of an EntityServer WCF service, so to access the HttpContext you must set the aspNetCompatibilityEnabled flag in the web.config. 
 
<system.serviceModel>
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
 
If you do this, also be sure to set the targetFramework on the httpRuntime element:
 
<system.web>
 <httpRuntime targetFramework="4.5"/>
</system.web>
 
Alternately, you can grab the IP address directly from WCF, like this:
 
var ctx = OperationContext.Current;
var endpoint = ctx.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
string ip = endpoint.Address;
 
You'll need to add a reference to the System.ServiceModel assembly to use these types.

   

 



Posted By: gregweb
Date Posted: 23-Feb-2013 at 7:06pm
Thanks very much, works great.

Greg



Print Page | Close Window