2

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 ??

2
  • See if the initialization context of this docs is helpful to you. Commented Jan 9, 2023 at 7:38
  • @dotnetammar - I am also facing similar issue. If you found the solution, please post your answer in comment. Commented Mar 28, 2023 at 4:31

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.