I have a requirement to access the HttpContext.Current from with-in a RESTful WCF service. I know I am able to achieve this by adding the following to config:
<serviceHostingEnvironment aspNetCompatibilityEnabled=”true” />
and using the following attribute on my service:
[AspNetCompatibilityRequirements(RequirementsMode
= AspNetCompatibilityRequirementsMode.Required)]
Here is my issue, I need to "spin up" an instance of the service in code for unit testing and therefore I cannot use config files to specify service bebaviours etc. At the moment my code looks like follows, but despite scouring the web I have been unable to work out how I set up a ServiceHostingEnvironment class and set the AspNetCompatibilityEnabled property to true without using config, can anyone help?
string serviceUrl = "http://localhost:8082/MyService.svc";
_host = new ServiceHost(typeof(MyService), new Uri[] { new Uri(serviceUrl) });
ServiceEndpoint serviceEndpoint
= _host.AddServiceEndpoint(typeof(IMyService), new WebHttpBinding(), string.Empty);
serviceEndpoint.Behaviors.Add(new WebHttpBehavior());
// Here's where I'm stuck, i need something like...
ServiceHostingEnvironmentSection shes = new ServiceHostingEnvironmentSection();
shes.AspNetCompatibilityEnabled = true;
_host.Add(shes);
_host.Open();
Any help is much appreciated and thanks in advance.
Consider factoring out the explicit use of HttpContext.Current behind an interface that you can stub out during unit testing.
HttpContext.Current is only defined when your wcf service is hosted within an asp.net web application anyway - if some day you need to host it as an ordinary wcf service, HttpContext.Current is not going to be available.
To Elaborate on Austin Harris's answer:
You need to alter the behaviour of ServiceHost.
Because the attribute is read-only, you need to remove, and readd the behaviour to ServiceHost.
If you have a console application or Windows Service, you'll have this servicehost defined.
Something like this:
in which case Austin Harris' code would suffice (if he hadn't written Allowed instead of Required...).
However, if you have the WCF-Service integrated into an ASP.NET application, the tricky part is to get the ServiceHost.
The key is the factory attribute in the YOUR_SERVICE.svc markup file.
Then you need to write your own factory.
The below code is VB.NET, I'll leave it to the reader to translate it into C# (you'll need to set WITH_FORMS_AUTHENTICATION to true by the way, and C# ps: http://converter.telerik.com)
After digging around with Reflector, I was able to set the AspNetCompatibilityEnabled flag using reflection. This approach has obvious drawbacks, but it did the job for me:
You can totally do this, I don't know what these other answers are about, but they are way off!
Just do something like:
-- Edit This removes the existing behavior if it exists, and then adds a new behavior that has the mode you prefer. My example sets it to .Allowed, but you could of course set it to the mode you desire.
It's an AppDomain-wide setting that you can set on the staticServiceHostingEnvironment
class in System.ServiceModel:This should be done before you create and open your service hosts.
Would have been nice - but it's a read-only setting, and the only way to set it appears to be through configuration :-(