Reputation: 555
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.
Upvotes: 3
Views: 6992
Reputation: 7532
There is no HttpContext
in WCF, WCF session is different from the Http session. Please refer to the below link.
https://learn.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://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
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.
Upvotes: 1
Reputation: 1181
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
Upvotes: 7