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

别等时光非礼了梦想. 提交于 2019-12-11 18:07:18

问题


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.


回答1:


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.




回答2:


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; 


来源:https://stackoverflow.com/questions/13923920/lost-access-to-http-request-after-upgrade-to-net-framework-4-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!