简体   繁体   中英

Lost access to HTTP Request after upgrade to .NET Framework 4.5

I have a WCF service that accepts a POST request from jqGrid in the form of HTML form and returns JSON.

While things were on .NET 4.0, all worked fine. I could access form fields inside the service via request["fieldName"] . Once I upgraded to .NET 4.5, all my request["fieldName"] are now blank. Is there some kind of known issue with .NET 4.5, WCF and HttpContext.Current.Request ?

Here is an example:

 POST http://{REMOVED}/Grid.svc/Execute HTTP/1.1
 Accept: application/json, text/javascript, */*; q=0.01
 X-Requested-With: XMLHttpRequest
 Content-Type: application/x-www-form-urlencoded; charset=UTF-8
 Accept-Encoding: gzip, deflate

 _search=false&nd=1355782305975&rows=15&page=1&sidx=modified&sord=desc&search=&category=all

and, here is the service:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class GridAccess
{
    [OperationContract]
    [WebInvoke(Method = "POST",
           BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json
    )]
    public GridResponse Execute()
    {
        var request = System.Web.HttpContext.Current.Request;

All references to request["fieldName"] worked before under .NET 4.0 and now, after upgrade to .NET 4.5, they return NULLs.

The issue's root cause looks to be the same as the issue described in post : Visual Studio 2012 install broke my 2010 WCF project . I can see your code starts working again if I follow the work around suggested in this post. You can refer to this blog post for more information.

To resolve these kind of issues. Just add the System.Web DLL to the Assembly. Then reference it like this for example:

System.Web.HttpRequest request = System.Web.HttpContext.Current.Request; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM