【问题标题】:How to pass Header for the WCF SOAP Service如何为 WCF SOAP 服务传递标头
【发布时间】:2019-08-16 17:00:06
【问题描述】:

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

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

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

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

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");

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

未处理的类型异常 'System.ServiceModel.Security.SecurityAccessDeniedException' 发生 在 mscorlib.dll 中附加信息:访问被拒绝。

【问题讨论】:

    标签: c# wcf


    【解决方案1】:

    OperationContxtScope的作用域只在Using语句中有效,即带有特定soap头的服务请求在Using语句中有效。另一个调用在 Using 语句之外恢复,没有特定的标头。
    如果我们想为每个请求永久添加消息头,我们可以使用 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
    如果有什么我可以帮忙的,请随时告诉我。

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多