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 ??
Try making the IManagementService to use the attributes defined in the System.ServiceModel.Primitives package instead of the ones in CoreWCF. CoreWCF will theoretically continue to use those as well, while it becomes possible to invoke the ChannelFactory on them too.