I've been added aspNetCompatibilityEnabled="true" attribute to serviceHostingEnvironment config in my WCF service project, AspNetCompatibilityRequirementsMode.Required to service, but still get null HttpContext.Current value in ReadMessage. How to fix this problem?
I use custom binding with http transport + custom host factory.
Related
When I add a service reference to a WCF web service in my current ASP.net MVC 3 project, proxy classes are successfully generated, but there's no information added to web.config. This results in an error when I attempt to instantiate the proxy class using the no-args constructor. On this line:
var proxy = new ReportingService.ApiServiceClient();
I get the following runtime exception:
Could not find default endpoint element that references contract
'ReportingService.IApiService' in the ServiceModel client configuration
section. This might be because no configuration file was found for your
application, or because no endpoint element matching this contract could
be found in the client element.
This makes sense because there's absolutely nothing in my config file indicating the default endpoint.
What am I missing? Why would adding the service reference fail to create a default endpoint?
We're doing a WCF REST json service (WebHttpBinding). Since pure WCF session doesn't work with this kind of binding, we are using asp.net session. So we set:
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
In our web.config, and :
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
On top of our service implementation.
We have several service methods such as:
public void OpenSession(string userName)
public void GetSomething(int somethingId)
public void CloseSession()
Then we test the sessionId of the HttpContext in each method.
HttpContext.Current.Session.SessionID
Problem is: it's always changing.
If I make a call to OpenSession?userName='toto' in my web browser, and another one right after to GetSomething?somethingId=1234, the session id will change.
It seems that the cookies aren't well handled by wcf. If I call the OpenSession method in fiddler, in the http headers, there're no 'Set-Cookie' returned by the service.
I found that :
By default, WCF does not have cookie
enabled on the client side. So when
the server requires Cookie, you need
to turn on the cookie on the binding
for the client through the property
HttpTransportBindingElement.AllowCookies.
It's in this article. Checki if it will be usefull for you : http://blogs.msdn.com/b/wenlong/archive/2006/01/23/516041.aspx
Is there a way to do this globally, automatically for all my WCF services via configuration in WCF 4.0?
That is, I know WCF 4.0 exposes new configuration techniques that applies certain behaviors by default to all hosted endpoints, and that you don't need to explicitly specify individual endpoints by config anymore...but can I do something in the config that says to automatically host all services with both a BasicHttpBinding and a WebHttpBinding (using a /web relative address for the WebHttpBinding)? Or do I still need to use a custom ServiceHostFactory for this?
Thanks.
See the Developer's Introduction to WCF 4 for lots of interesting stuff in WCF 4.
One of the new features is called default protocol mapping, and this combined with the default endpoints provided by WCF 4 might solve your problem.
Default endpoints means that WCF 4 will provide one endpoint for each contract your service class implements (typically only 1), and for each base address defined in your config (or code for the ServiceHost).
In order to make sure a http:// endpoint gets exposed automagically with the webHttpBinding, you also need to override the system default (which is basicHttpBinding) - which you can do thanks to the protocol mappings.
<configuration>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
</system.serviceModel>
</configuration>
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.
Turns out this is a bug in the framework that was fixed in 4.0. more info
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.
I have a WCF service that has a method to return the Windows Username of a Silverlight client that is consuming the service . The WCF service is using basicHttpBinding with the TransportCredentialOnly mode set and the TransportClientCredentialType set to Windows. In IIS Windows authentication is enabled and anon authentication disabled.
Despite (apparently) configuring the service correctly when I call the service and ask it to return the username it errors. Closer examination shows that HttpContext.Current.User is always null (so a nullreferenceexception is thrown).
Does anyone have any other ideas why this is not working?
Try adding -
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
to your config file
In WCF, there is the OperationContext object from which you can retrieve the security credentials passed in by the caller/client by using the ServiceSecurityContext property.