简体   繁体   English

我可以从自定义绑定中添加自定义WCF行为吗?

[英]Can I add custom WCF Behaviors from my custom Binding?

I am writing a custom WCF Binding and I want to run some code only if there is a problem processing a message. 我正在编写自定义WCF绑定,我只想在处理消息时遇到问题时运行一些代码。 My thought is to add a custom IOperationInvoker and use a try/catch around the processing of the message. 我的想法是添加一个自定义IOperationInvoker并在处理消息时使用try / catch。 I cannot find a way to add the custom behavior from within my binding. 我找不到从绑定中添加自定义行为的方法。 I would like to avoid having to declare BOTH a binding and behavior for any services that want to use this binding. 我想避免为任何想要使用此绑定的服务声明BOTH绑定和行为。

The model I'm running off of is the Net.Msmq binding, where you can declare retry handling via the binding. 我正在运行的模型是Net.Msmq绑定,您可以通过绑定声明重试处理。 That's essentially what I'm trying to replicate, but via my own binding. 这基本上是我想要复制的东西,但是通过我自己的绑定。

Any WCF gurus out there? 那里有WCF大师吗?

I think it is not possible. 我认为这是不可能的。 Binding cannot add behaviors. 绑定不能添加行为。 You can check default WCF bindings - for example WebHttpBinding is always used in conjunction with WebHttpBehavior or WebScriptEnablingBehavior but WebHttpBinding is not able to add them, you must add them manually. 您可以检查默认WCF绑定 - 例如, WebHttpBinding始终与WebHttpBehaviorWebScriptEnablingBehavior一起使用,但WebHttpBinding无法添加它们,您必须手动添加它们。 To solve this inconvenience WCF offers WebServiceHost class which can be used instead of common ServiceHost . 为了解决这个不便,WCF提供了WebServiceHost类,可以使用它来代替常见的ServiceHost WebServiceHost makes all needed configuration for REST service - it adds WebHttpBehavior and enpoint with WebHttpBinding. WebServiceHost为REST服务提供了所有必需的配置 - 它添加了WebHttpBehavior并使用WebHttpBinding进行enpoint。 You can use the same approach. 您可以使用相同的方法。

But I think this not a good approach. 但我认为这不是一个好方法。 Retry handling is already handled by ReliableSession and it is implemented as channel so you should probably try the same way. ReliableSession已经处理了重试处理,并将其作为通道实现,因此您应该尝试相同的方式。 In such case you will not need to deal with behavior problem. 在这种情况下,您不需要处理行为问题。

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

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