如何在单元测试中初始化CoreWCF.OperationContext?

问题描述 投票:0回答:1

我有一个 CoreWCF .NET 6 服务,它使用以下代码返回自定义 HTTP 状态代码:

var requestProp = new HttpResponseMessageProperty(); OperationContext.Current.OutgoingMessageProperties[HttpResponseMessageProperty.Name] = requestProp; requestProp.StatusCode = httpStatusCode;

问题是我无法在单元测试中初始化或模拟 OperationContext.Current

可以在旧的 WCF .NET Framework 4.8 中通过执行以下操作来完成此操作:

var factory = new ChannelFactory<IManagementService>(new BasicHttpBinding(), new EndpointAddress("http://localhost/Program.Interface/ManagementService.svc")); OperationContext.Current = new OperationContext(factory.CreateChannel() as IContextChannel);

这不再可能,因为 **ChannelFactory ** 来自 System.ServiceModel 包,而我的 IManagementService 服务合同使用 **CoreWCF ** 包。

我也尝试使用HttpContextAccessor,在单元测试中使用它很容易但是HTTP状态代码在我的服务中没有改变!!

有谁知道如何在单元测试中初始化 CoreWCF.OperationContext ??

.net http wcf http-status-codes corewcf
1个回答
0
投票

尝试使 IManagementService 使用 System.ServiceModel.Primitives 包中定义的属性,而不是 CoreWCF 中的属性。理论上,CoreWCF 也将继续使用它们,同时也可以在它们上调用 ChannelFactory。

© www.soinside.com 2019 - 2024. All rights reserved.