WCF 中的自定义标题 [英] Custom Headers in WCF

查看:23
本文介绍了WCF 中的自定义标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的 wsdl 上为传入的soap 消息添加特定的自定义标头,因此我已将所需的标签添加到 web.config 的标头节点中,如下所示:

I've want to add specific custom headers on my wsdl for incoming soap message so i've added the required tags into the header node of the web.config like below:

 <headers>
   <Tag>Value</Tag>
 </headers>

但是,如果在我更改此值时将自定义标记中的值"设置为值",服务将其踢出并给出以下错误,则此方法可以正常工作;

However, this works fine if the 'Value' in the custom tag is set to 'Value' when I change this value the service kicks it out giving the below error;

'DestinationUnreachable - To 'http://localhost:3537/Service1.svc 的消息' 无法在接收器处处理,因为 EndpointDispatcher 处的 AddressFilter 不匹配.检查发送方和接收方的 EndpointAddresses 是否一致.'

'DestinationUnreachable - The message with To 'http://localhost:3537/Service1.svc' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree.'

那么如何为标签之间的实际值指定变量值??就像在 url 模板中一样,即

So how can I specify variable values for the actual value in between the tags?? like in url templates i.e.

 <headers>
   <Tag>{variable value here}</Tag>
 </headers>

任何想法,或者我是否以错误的方式处理自定义标题?我不想使用 messageContract,因为我们使用 RPC 风格的肥皂而不是文档风格.

Any ideas, or am I going about custom headers the wrong way? I don't want to use a messageContract as we use RPC style soap over document style.

另一个奇怪的事情是,要使消息有效,header 元素中定义的自定义必须指定带有 ws-addressing 命名空间的 'IsReferenceParameter="true"' 属性,否则会引发上述错误?

Also another curious thing is that for the message to be valid the custom defined in the header element must specify a 'IsReferenceParameter="true"' attribute with a ws-addressing namespace otherwise it throws the above error?

 <Tag a:IsReferenceParameter="true">Value</Tag>

谁能给我解释一下?

提前致谢

乔恩

推荐答案

我无法真正解释您的具体问题 - 但通常,您会在代码中向 WCF 调用添加自定义标头,通常使用行为,而不是来自 web.config.不确定这是否有效,真的.

I can't really explain the concrete question you have - but typically, you would add custom headers to WCF calls in code, often using a behavior, rather than from web.config. Not sure if that even works, really.

当然,您的自定义标头行为可以做的是读取它将从配置文件或数据库表或其他内容发送的值.

What your custom headers behavior could do, of course, is read its values that it'll send from a config file or a database table or something else.

但是如果你真的想在你的消息中强制执行 SOAP 标头,我认为你最好的选择真的是使用消息契约.为什么不能或不想使用消息契约??这确实是消息契约的唯一目的:定义显式 SOAP 消息布局,包括自定义标头.

But if you really want to enforce the SOAP headers in your messages, I think your best bet would really be to use message contracts. Why is it you can't or don't want to use message contracts?? That's really the sole purpose of message contracts: defining the explicit SOAP message layout, including custom headers.

查看有关该主题的一些文章和博客文章:

See some articles and blog posts on the topic:

如何添加为每个 WCF 调用自定义 HTTP 标头?

这个 此处的博文 展示了如何将自定义 SOAP 标头注入到通过实现自定义WsdlExporter"类生成的 WSDL 中 - 也许这就是您要走的路?

This blog post here show how to inject custom SOAP headers into the WSDL being generated by implementing a custom "WsdlExporter" class - maybe that's the way to go for you?

如果您只是通过谷歌搜索可以获得更多资源 - 这是一个非常常见的场景,并且很多人已经以多种方式实现了它,并提供了一些巧妙的解决方案,并在博客上发表了关于它的信息 - 您应该没有很难找到所有答案!

Plenty more resources available if you just google for it - this is a very common scenario, and lots of folks have already implemented it in a great number of ways, with some ingenious solutions, and blogged about it - you should have no trouble finding all the answers out there!

马克

这篇关于WCF 中的自定义标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆