简体   繁体   English

如何为WCF SOAP服务传递标头

[英]How to pass Header for the WCF SOAP Service

I'm trying to consume a WCF SOAP Service through C# console Application. 我正在尝试通过C#控制台应用程序使用WCF SOAP服务。

I have 2 services in context here, one for getting the Authentication Token by passing username and password 2nd for getting the required data, where this service expects the Token which is generated earlier to be passed as a Header with the name "Token" 我在上下文中有2个服务,一个用于通过传递用户名和密码来获取身份验证令牌,第二个用于获取所需数据,其中该服务希望较早生成的令牌作为名称为“令牌”的标头传递

This works fine when I call the service using the SOAP UI, where we have an option to send the Header name and value 当我使用SOAP UI调用服务时,这可以很好地工作,在这里我们可以选择发送标头名称和值

but I'm not able to find a way to do the same programatically below is the sample code what I have tried 但是我无法找到一种方法来以编程方式执行以下操作,下面是我尝试过的示例代码

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

When i try this, it will get me 当我尝试这个时,它会让我

An unhandled exception of type 'System.ServiceModel.Security.SecurityAccessDeniedException' occurred in mscorlib.dll Additional information: Access is denied. mscorlib.dll中发生了类型为'System.ServiceModel.Security.SecurityAccessDeniedException'的未处理异常。其他信息:访问被拒绝。

The scope of the OperationContxtScope is only available within Using statement, namely, the service request with the specific soap header is valid in the Using statement. OperationContxtScope的作用域仅在Using语句中可用,即,具有特定soap标头的服务请求在Using语句中有效。 the other call is restored outside of the Using statement, without a specifical header. 另一个调用将在Using语句之外恢复,没有特定的标头。
We can use the IClientMessageInspector interface if we want to permanently add message headers to every requests. 如果要向每个请求中永久添加消息头,则可以使用IClientMessageInspector接口。
https://putridparrot.com/blog/adding-data-to-wcf-message-headers-client-side https://putridparrot.com/blog/adding-data-to-wcf-message-headers-client-side
Please refer to my previous reply. 请参考我以前的回复。
Adding an outgoing message headers in WCF can't be retrieved in incoming message headers 在WCF中添加传出邮件头无法在传入邮件头中检索
Feel free to let me know if there is anything I can help with. 请随时告诉我是否有什么我可以帮助的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM