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.
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
. 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.
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?
WCF services use attributes natively just check classes like:
ServiceContractAttribute
, OperationContractAttribute
MessageContractAttriubte
, MessageHeaderAttriubte
, MessageBodyMemberAttribute
WebGetAttribute
, WebInvokeAttribute
ServiceBehaviorAttribute
, OperationBehaviorAttribute
, CallbackBehaviorAttriubte
ServiceKnownTypeAttribute
, FaultContractAttriubte
DataContractFormatAttribute
, XmlSerializerFormatAttribute
TransactionFlowAttribute
, DeliveryRequirementsAttribute
AspNetCompatibilityRequirementsAttribute
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:
IServiceBehavior
- affects whole serviceIEndpointBehavior
- affects single endpointIOperationBehavior
- affects single operationIContractBehavior
- affects single service or data contractThese behaviors can contain some logic or add some other more advanced custom features like:
IParameterInspector
- for example custom parameter validation for operationIDispatchMessageFormater
- dealing with serialization and deserialization on server sideIClientMessageFormater
- dealing with serialization and deserialization on client sideIDispatchMessageInspector
- message modification or validation on server sideIClientMessageInspector
- message modification or validation on client sideIDispatchOperationSelector
- selection of operation handling the incoming message on server sideIClientOperationSelector
- based on proxy method called can select different operation to be called from the client sideIOperationInvoker
- invoking the operation - allows working with operation parameters and for example add other parameters which were not passed in the message but are stored locallyIErrorHandler
- global error handlingIInstanceContextProvider
- custom instance context handling - the basis if you want to implement custom session handling in WCFIInstanceProvider
- custom service instance lifetime handlingAs 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). Moreover custom behaviors are only one part of WCF extensibility. The second part deals with custom bindings and channels.
If you want to know more about WCF extensibility check
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. You must turn on AspNetCompatibility and after that you will degrade your WCF service to ASMX service. 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. Another option is pure AoP framework - PostSharp.
For AoP with Unity you can check several articles by Dino Esposito from MSDN Magazine:
For Spring.NET simply check their excellent documentation. I didn't use AoP with Windsor but you will find plenty of articles on internet. PostSharp is the only mentioned tool which is commercial. It has a free version with smaller feature set but you can find that features you need are only in commercial version.