@jsbarkan commented on Wed Jan 24 2018
What should we use in .NET Core since the "WebOperationContext" (found in .NET Framework, from System.ServiceModel.Web) is no longer available?
The purpose is to modify the response header being returned from a WCF service method.
WebOperationContext is only a convenience wrapper around OperationContext and you can get everything you need there. You can do something similar to the following:
var responseProp = OperationContext.Current.IncomingMessageProperties[HttpResponseMessageProperty.Name] as HttpResponseMessageProperty;
WebHeaderCollection headers = responseProp.Headers;
// You can modify the response headers
@mconnew Is that possible in asp.net core 2.1?
@prashantpimpale93, I don't understand what you are asking. WCF services aren't currently supported on asp.net core so there's no service call to modify the response headers. If you are using a WCF client and want to modify the headers which were returned from a service call, e.g. via a message inspector, then the code snippet I provided works.
If you want to modify the request headers, then instead you use:
c#
var requestProp = new HttpResponseMessageProperty();
OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name] = requestProp;
requestProp.Headers["foo"] = bar;
@mconnew thank you for your code and I'm facing this issue now. I've to migrate a solution to .net core and it relies on a WCF service having a couple of headers in the request such as the basic authentication for example.
For your information, you mentioned the request headers in your example, however, you've used the HttpResponseMessageProperty class instead of HttpRequestMessageProperty.
var messageProperty = new HttpRequestMessageProperty();
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = messageProperty;
messageProperty.Headers["foo"] = bar;
Most helpful comment
WebOperationContext is only a convenience wrapper around OperationContext and you can get everything you need there. You can do something similar to the following: