I have a CoreWCF .NET 6 service that returns custom HTTP status codes with this code:
var requestProp = new HttpResponseMessageProperty(); OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name] = requestProp; requestProp.StatusCode = httpStatusCode;
The problem is I cannot initialize or mock the OperationContext.Current in my unit tests.
It is possible to do it in old WCF .NET Framework 4.8 by doing:
var factory = new ChannelFactory<IManagementService>(new BasicHttpBinding(), new EndpointAddress("http://localhost/Program.Interface/ManagementService.svc")); OperationContext.Current = new OperationContext(factory.CreateChannel() as IContextChannel);
This is no longer possible because **ChannelFactory **is from the System.ServiceModel package whereas my IManagementService service contract is using **CoreWCF **package.
I also tried to use the HttpContextAccessor, it is easy to use it in unit tests but the HTTP status code doesn't change in my service !!
Does anyone have an idea on how to initialize the CoreWCF.OperationContext in unit tests ??