如何为 WCF SOAP 服务传递 Header

How to pass Header for the WCF SOAP Service

我正在尝试通过 C# 控制台应用程序使用 WCF SOAP 服务。

我这里有 2 个上下文服务, 一种用于通过传递用户名和密码获取身份验证令牌 第二个用于获取所需的数据,此服务希望将先前生成的令牌作为 Header 传递,名称为 "Token"

当我使用 SOAP UI 调用服务时,这工作正常,我们可以选择发送 Header 名称和值

但我找不到以编程方式执行相同操作的方法 下面是我试过的示例代码

SecurityService.IInteropSecurityService authenticationClient = new SecurityService.InteropSecurityServiceClient();

string token = client.GetAuthenticationToken("cfadmin", "305f2a0ebb646a2ab32689d5b9c01532");


EntityService.InteropEntityServiceClient entityClient = new EntityService.InteropEntityServiceClient();

 using (new OperationContextScope(entityClient.InnerChannel))
            {
                HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
                httpRequestProperty.Headers["Token"] = token;

                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;

            }
entityClient.GetEntity("Test name");

当我尝试这个时,它会得到我

An unhandled exception of type 'System.ServiceModel.Security.SecurityAccessDeniedException' occurred in mscorlib.dll Additional information: Access is denied.

OperationContxtScope的作用域只在Using语句中有效,即带有特定soap的服务请求header在Using语句中是有效的。另一个调用在 Using 语句之外恢复,没有特定的 header.
如果我们想向每个请求永久添加消息 headers,我们可以使用 IClientMessageInspector 接口。
https://putridparrot.com/blog/adding-data-to-wcf-message-headers-client-side
请参考我之前的回复
Adding an outgoing message headers in WCF can't be retrieved in incoming message headers
如果有什么我可以帮忙的,请随时告诉我。