简体   繁体   English

是否有使用属性修改 WCF 操作行为的标准方法?

[英]Is there a standard way to use attributes to modify how WCF operations behave?

I'm working on some funky code right now, and I've been wondering whether or not I can use attributes to modify how a WCF operation behaves, like maybe make it perform additional checking or make it skip some logic.我现在正在编写一些时髦的代码,我一直在想是否可以使用attributes来修改 WCF 操作的行为方式,比如让它执行额外的检查或让它跳过一些逻辑。

For example, if we had the following request envelope:例如,如果我们有以下请求信封:

[MessageContract]
public class UserRequest 
{
    [MessageBodyMember]
    public string SessionKey { get; set; }

    [MessageBodyMember]
    public UserModel User { get; set; }
}

and the following service operations:以及以下服务操作:

[ForceSession]
void AddUser ( UserRequest request ) 
{
}

void EditUser ( UserRequest request )
{
}

we could have some automatic functionality on the AddUser operation which checks that the request's session key exists in the current HttpContext .我们可以在AddUser操作上有一些自动功能,检查请求的 session 键是否存在于当前HttpContext中。 Maybe something to the equivalent of checking HttpContext.Current.Session[request.SessionKey] != null , to which end it either rejects the call (sends an empty response envelope) or processes it.可能相当于检查HttpContext.Current.Session[request.SessionKey] != null ,为此它要么拒绝呼叫(发送一个空的响应信封)或处理它。

Of course, I could just add the checking code at the start of each operation where they matter, but that can get pretty repetitive pretty fast, especially if I'm working with a lot of operations.当然,我可以在每个重要的操作开始时添加检查代码,但这可能会很快重复,尤其是在我处理大量操作的情况下。

How should I go about with implementing something of the sort?我应该如何 go 来实现类似的东西?

WCF services use attributes natively just check classes like: WCF 服务本身使用属性,只需检查以下类:

  • ServiceContractAttribute , OperationContractAttribute ServiceContractAttributeOperationContractAttribute合同属性
  • MessageContractAttriubte , MessageHeaderAttriubte , MessageBodyMemberAttribute MessageContractAttriubte , MessageHeaderAttriubte , MessageBodyMemberAttribute
  • WebGetAttribute , WebInvokeAttribute WebGetAttribute , WebInvokeAttribute
  • ServiceBehaviorAttribute , OperationBehaviorAttribute , CallbackBehaviorAttriubte ServiceBehaviorAttributeOperationBehaviorAttribute行为属性、 CallbackBehaviorAttriubte行为属性
  • ServiceKnownTypeAttribute , FaultContractAttriubte ServiceKnownTypeAttribute , FaultContractAttriubte
  • DataContractFormatAttribute , XmlSerializerFormatAttribute DataContractFormatAttribute , XmlSerializerFormatAttribute
  • TransactionFlowAttribute , DeliveryRequirementsAttribute TransactionFlowAttribute , DeliveryRequirementsAttribute
  • AspNetCompatibilityRequirementsAttribute
  • and several others和其他几个

These attributes affect WCF processing but WCF also offers large extensibility model with several injection points where you can add your own processing by implementing any of these interfaces in custom attribute:这些属性会影响 WCF 处理,但 WCF 还提供较大的可扩展性 model 具有多个注入点,您可以通过在自定义属性中实现这些接口中的任何一个来添加自己的处理:

  • IServiceBehavior - affects whole service IServiceBehavior - 影响整个服务
  • IEndpointBehavior - affects single endpoint IEndpointBehavior - 影响单个端点
  • IOperationBehavior - affects single operation IOperationBehavior - 影响单个操作
  • IContractBehavior - affects single service or data contract IContractBehavior - 影响单个服务或数据合同

These behaviors can contain some logic or add some other more advanced custom features like:这些行为可以包含一些逻辑或添加一些其他更高级的自定义功能,例如:

  • IParameterInspector - for example custom parameter validation for operation IParameterInspector - 例如操作的自定义参数验证
  • IDispatchMessageFormater - dealing with serialization and deserialization on server side IDispatchMessageFormater - 处理服务器端的序列化和反序列化
  • IClientMessageFormater - dealing with serialization and deserialization on client side IClientMessageFormater - 在客户端处理序列化和反序列化
  • IDispatchMessageInspector - message modification or validation on server side IDispatchMessageInspector - 服务器端的消息修改或验证
  • IClientMessageInspector - message modification or validation on client side IClientMessageInspector - 客户端的消息修改或验证
  • IDispatchOperationSelector - selection of operation handling the incoming message on server side IDispatchOperationSelector - 选择在服务器端处理传入消息的操作
  • IClientOperationSelector - based on proxy method called can select different operation to be called from the client side IClientOperationSelector - 基于代理方法调用可以 select 从客户端调用不同的操作
  • IOperationInvoker - invoking the operation - allows working with operation parameters and for example add other parameters which were not passed in the message but are stored locally IOperationInvoker - 调用操作 - 允许使用操作参数,例如添加未在消息中传递但存储在本地的其他参数
  • IErrorHandler - global error handling IErrorHandler - 全局错误处理
  • IInstanceContextProvider - custom instance context handling - the basis if you want to implement custom session handling in WCF IInstanceContextProvider - 自定义实例上下文处理 - 如果要在 WCF 中实现自定义 session 处理的基础
  • IInstanceProvider - custom service instance lifetime handling IInstanceProvider - 自定义服务实例生命周期处理
  • any many others任何许多其他人

As you can see extensibility of WCF is pretty large - IMHO with ASP.NET MVC the best in the whole .NET framework (at least among parts I'm regularly using).如您所见,WCF 的可扩展性非常大 - 恕我直言,ASP.NET MVC 是整个 .NET 框架中最好的(至少在我经常使用的部分中)。 Moreover custom behaviors are only one part of WCF extensibility.此外,自定义行为只是 WCF 可扩展性的一部分。 The second part deals with custom bindings and channels.第二部分处理自定义绑定和通道。

If you want to know more about WCF extensibility check如果您想了解更多关于 WCF 扩展性检查

But is it what you need?但这是你需要的吗? First check existing attributes if they already offer you the functionality you are looking for.首先检查现有属性是否已经为您提供了您正在寻找的功能。 Next think about session - ASP.NET session is not normally provided to WCF service.接下来想想 session - ASP.NET session 是不正常提供给 ZFB608636129CB469B63E521 服务的。 You must turn on AspNetCompatibility and after that you will degrade your WCF service to ASMX service.您必须打开 AspNetCompatibility,然后您将 WCF 服务降级为 ASMX 服务。 Even after that you can have problems with ASP.NET session because the information about session is transferred in cookie and WCF by default doesn't use them. Even after that you can have problems with ASP.NET session because the information about session is transferred in cookie and WCF by default doesn't use them.

For last if you only need some custom attributes to add logic to selected methods it more looks like scenario for AoP (aspect oriented programming) which can be offered outside of WCF through several IoC (inversion of control) containers like MS Unity, Windsor Castle or Spring.NET.最后,如果您只需要一些自定义属性来为选定的方法添加逻辑,它更像是 AoP(面向方面编程)的场景,可以通过几个 IoC(控制反转)容器(如 MS Unity、Windsor Castle 或春天.NET。 Another option is pure AoP framework - PostSharp.另一种选择是纯 AoP 框架 - PostSharp。

For AoP with Unity you can check several articles by Dino Esposito from MSDN Magazine:对于 Unity 的 AoP,您可以查看 MSDN 杂志上 Dino Esposito 的几篇文章:

For Spring.NET simply check their excellent documentation .对于 Spring.NET,只需查看其出色的文档即可。 I didn't use AoP with Windsor but you will find plenty of articles on internet.我没有在 Windsor 中使用 AoP,但您会在 Internet 上找到很多文章。 PostSharp is the only mentioned tool which is commercial. PostSharp 是唯一提到的商业工具。 It has a free version with smaller feature set but you can find that features you need are only in commercial version.它有一个功能集较小的免费版本,但您会发现您需要的功能仅在商业版本中。

Yes.是的。 You need to create a custom operation behavior implementing IOperationBehavior and Attribute.您需要创建一个实现 IOperationBehavior 和 Attribute 的自定义操作行为。

IOperationBehavior requires you to implement 4 methods. IOperationBehavior 要求您实现 4 种方法。

  1. ApplyDispatchBehavior()应用调度行为()
  2. AddBindingParameters()添加绑定参数()
  3. ApplyClientBehavior()应用客户端行为()
  4. Validate()证实()

You will have the complete control on every part of communication.您将完全控制通信的每个部分。 You can use parameter and message inspectors for more precise control.您可以使用参数和消息检查器进行更精确的控制。

Refer: IOperationBehavior Interface for more information.更多信息请参考: IOOperationBehavior 接口

it there are security or authorization type things you want to check before every service operation, then you may want to implement a custom System.ServiceModel.ServiceAuthorizationManager在每次服务操作之前,您都需要检查安全或授权类型的东西,那么您可能需要实现自定义System.ServiceModel.ServiceAuthorizationManager

For general behavioral things, you can add custom IEndpointBehavior or IOperationBehavior extension.对于一般行为的事情,您可以添加自定义IEndpointBehaviorIOperationBehavior扩展。

If you REALLY want to control everything, you can implement a custom IServiceInvokerExtension , in which you could check the method being called for custom attributes, and call custom methods before/after the actual service method call (like a fake AOP).如果你真的想控制一切,你可以实现一个自定义的IServiceInvokerExtension ,你可以在其中检查为自定义属性调用的方法,并在实际服务方法调用之前/之后调用自定义方法(如假 AOP)。

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

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