C#中,非静态字段、方法或属性“HttpContext.Request”需要对象引用。

3

我正在尝试创建一个通用工厂类来调用WCF并注入一些头信息。在这个类中,我想要读取HTTP头属性。

    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;
            }       
        }
}

但我收到了一个编译错误,如下所示:"需要对象引用才能访问非静态字段、方法或属性 'HttpContext.Request'"。由于我不是从静态方法或类中调用,为什么会出现这种情况呢?如有帮助,将不胜感激。

谢谢。

2个回答

7

HttpContext.Request无法正常工作,因为它试图访问一个实例属性,就像它是一个静态属性一样。假设上下文已经与该线程关联,HttpContext.Current.Request应该可以正常工作。

HttpContext.Request和Request的区别


1

WCF中没有HttpContext,WCF的会话与Http会话不同。请参考以下链接。
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/wcf-services-and-aspnet?redirectedfrom=MSDN
在服务器端,我们可以启用Asp.net兼容性模式来访问HttpContext。这要求我们将服务托管在IIS中。

<system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
链接。
https://learn.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
然而,客户端无法访问它。就像你所做的那样,通过OperationContext类可以完成HTTP头的配置。
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);
            }

这里有一个相关的讨论。
WCF中的HttpContext
如果有需要帮忙的地方,请随时告诉我。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接