access HttpContext.Current from WCF Web Service

You can get access to HttpContext.Current by enabling AspNetCompatibility, preferably via configuration: <configuration> <system.serviceModel> <serviceHostingEnvironment aspNetCompatibilityEnabled=”true”/> </system.serviceModel> </configuration> That in turn allows you to get access to the current user: HttpContext.Current.User – which is what you’re after, right? You can even enforce AspNetCompatibility by decorating your service class with an additional attribute: [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)] … Read more

What is the WCF equivalent of HttpContext.Current.Request.RawUrl?

You can get the endpoint currently targeted and the Uri for it by doing: OperationContext.Current.RequestContext.RequestMessage.Headers.To which I think is the same thing as: OperationContext.Current.IncomingMessageHeaders.To This is a System.Uri object, and I believe you can just get the OriginalString or PathAndQuery, or whatever parts you want from it.

Read Http Request into Byte array

The simplest way is to copy it to a MemoryStream – then call ToArray if you need to. If you’re using .NET 4, that’s really easy: MemoryStream ms = new MemoryStream(); curContext.Request.InputStream.CopyTo(ms); // If you need it… byte[] data = ms.ToArray(); EDIT: If you’re not using .NET 4, you can create your own implementation of … Read more

Correct way to use HttpContext.Current.User with async await

As long as your web.config settings are correct, async/await works perfectly well with HttpContext.Current. I recommend setting httpRuntime targetFramework to 4.5 to remove all “quirks mode” behavior. Once that is done, plain async/await will work perfectly well. You’ll only run into problems if you’re doing work on another thread or if your await code is … Read more

HttpContext.Current not Resolving in MVC 4 Project

Try prefixing it with System.Web. If I try System.Web.HttpContext.Current, then Current is there, but if I try HttpContext.Current, then it doesn’t recognize ‘Current’. I do have System.Web in my using statements, but I still seem to be required to specify it in order to get access to ‘Current’. @Chris’ answer points and explains the underlying … Read more