tags:

views:

39

answers:

2

I have some WCF services that are hosted by IIS with ASP.Net compatibility turned on.

I have the following in my web.config:

< serviceHostingEnvironment aspNetCompatibilityEnabled="true" />

And my service class is decorated with the following:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]

For some weird reason, the HttpContext.Current is null for the first request after the web app starts up. The HttpContext.Current is not null for following requests as expected.

Does anyone have an idea why it isn't working for the very first request?

The call to HttpContext.Current is from within a custom UserNamePasswordValidator class I wrote to check creds stored in the database.

A: 

If you are using IIS 7.0 integrated mode to host your application HttpContext.Current might not be initialized in Application_Start so if you are trying to access it there it will crash.

Darin Dimitrov
I'm not calling it from with the Application_Start event. The UserNamePasswordValidator class is called by the WCF plumbing.
Craig Quillen
What happens if you deactivate the integrated mode in IIS and use classic mode instead?
Darin Dimitrov
Classic mode actually fixes the problem, but now I have the new problem of not being in integrated mode.
Craig Quillen
Do you have any thoughts on why classic mode fixes it?
Craig Quillen
I explained in my answer why. Because HttpContext.Current might not be available in integrated mode during Application_Start. Then you said that it is called by the *WCF plumbing* which is not very precise for me. By the way probably off-topic but it is not recommended to use HttpContext.Current at all in a WCF service as it might be hosted in different environments, even in a console application and having your service depend on it is very bad. In WCF it is preferred to use [OperationContext](http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.aspx).
Darin Dimitrov
I am aware that this is frowned on, but my services will never be hosted outside of IIS. I am using an IoC container that keeps per-request scoped objects in the HttpContext items collection.
Craig Quillen
I don't know enough about WCF to be more precise than "WCF plumbing". I just put the type of my custom UserNamePasswordValidator in the config file and the framework calls the Validate method on it for every call. I would expect this to not be associated with the Application_Start because the validator is invoked per service method call.
Craig Quillen
A: 

Turns out this is a bug in the framework that was fixed in 4.0. more info

Craig Quillen