6
votes

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.

2
Do you mean that after the service runs, the first request to the service does not xt while you get the context during the next request(s)?Kangkan
Yes. The first request to one of the service methods after deploying new dlls or otherwise restarting the host asp.net app crashes with a null pointer on the HttpContext.Current call. The 2nd and following requests to the service work just fine.Craig Quillen
Question updated to include note about the custom UserNamePasswordValidator.Craig Quillen

2 Answers

1
votes

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

0
votes

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.