简体   繁体   中英

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

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.

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

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.

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