C# An object reference is required for the non-static field, method, or property 'HttpContext.Request'

瘦欲@ 提交于 2020-01-25 08:00:11

问题


I am trying create a common factory class to call WCF and to inject some headers. In this class I am trying to read the HTTP Header properties.

    using System.ServiceModel;
    using System.ServiceModel.Channels;
    using ServiceReference;
    using Microsoft.AspNetCore.Http;
namespace Service
{
     public class ServiceFactory
        {

            public static ServiceClient Create()
            {
                ServiceProxy service = new ServiceProxy();
                string userName = HttpContext.Request.Headers["AUTH_USERNAME"];
                string authenricationType = HttpContext.Request.Headers["AUTH_TYPE"];

                using (new System.ServiceModel.OperationContextScope((System.ServiceModel.IClientChannel)service.InnerChannel))
                {          
                    HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
                    requestMessage.Headers["AUTH_USERNAME"] = userName;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;

                    requestMessage.Headers["AUTH_TYPE"] = authenricationType;
                    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
                }
                return service;
            }       
        }
}

But I get a compile error as "An object reference is required for the non-static field, method, or property 'HttpContext.Request'. Since I am not calling from a Static method or a class how this could happen. Any help would be highly appreciated.

Thank you.


回答1:


HttpContext.Request won't work, because that's trying to access an instance property as if it were a static property. HttpContext.Current.Request should work, assuming the context has been associated with the thread by that point

Difference between HttpContext.Request and Request




回答2:


There is no HttpContext in WCF, WCF session is different from the Http session. Please refer to the below link.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet?redirectedfrom=MSDN
On the server-side, we could enable Asp.net compatibility mode to access the HttpContext. This requires us hosting the service in IIS.

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

Link.
https://docs.microsoft.com/en-us/dotnet/framework/wcf/samples/aspnet-compatibility?redirectedfrom=MSDN
https://www.aspsnippets.com/Articles/Access-and-Use-HttpContextCurrent-in-WCF-Service-in-ASPNet.aspx
However, it is impossible to access it on the client-side. As you have done. configuring the HTTP header could be completed by the OperationContext class.

using (new OperationContextScope((IClientChannel)service))
            {
                //first method to add HTTP header.
                //HttpRequestMessageProperty request = new HttpRequestMessageProperty();
                //request.Headers["MyHttpheader"] = "myvalue";
                //OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = request;
                //WebOperationContext is syntax sugar of wrapper above method.                OperationContext oc = OperationContext.Current;
                WebOperationContext woc = new WebOperationContext(oc);
                woc.OutgoingRequest.Headers.Add("myhttpheader", "myvalue");
                //invocation, only valid in this request.
                var result = service.GetResult();
                Console.WriteLine(result);
            }

Here is a related discussion.
HttpContext in WCF
Feel free to let me know if there is anything I can help with.



来源:https://stackoverflow.com/questions/59637749/c-sharp-an-object-reference-is-required-for-the-non-static-field-method-or-pro

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