WCF: Same Faultcontract on many methods
Asked Answered
A

3

14

Take for example a project with 10 services and 20 methods on each service.

All services inherit from a base services which has a security check. The first thing each method does is to make a call to the security check. This throws a security exception if there is a problem.

Question is: Do I need to specify a FaultContract on each method (OperationContract), or can I do it once in a central definition?

Albacore answered 8/9, 2009 at 7:49 Comment(0)
P
6

No, you need to do it on each and every method - WCF is rather picky and requires explicit settings pretty much for everything (which really is a good thing in the end, I am convinced).

Marc

Pneumonectomy answered 8/9, 2009 at 7:52 Comment(3)
Surely you'd agree that it should be possible to implement cross-cutting concerns like security in an orthogonal manner. It seems odd to me that I can implement a nice ServiceAuthorizationManager in a decoupled manner, but then I have to go sprinkle my contracts with repetitive junk if I want to throw a custom security fault.Bicarbonate
I agree, I'm looking for a method to do the repetitive junk automatically, too.Welty
You can write some T4 to generate all your contracts from a XML (or other) file and have your custom logic to define global faults. There is also the WSSF project on codeplex which uses a designer for your contracts and generates client/server code (it's a quite heavy tool).Dosimeter
S
31

You can do it by creating a custom attribute.

Implement IContractBehavior and add the fault to each operation on the Validate method.

void IContractBehavior.Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
{
   foreach (OperationDescription od in contractDescription.Operations)
      od.Add(yourFault);
}

Here's a link that details how to achieve this. Below the actual code to use:

[AttributeUsage(AttributeTargets.Interface, AllowMultiple = false, Inherited = true)]
public class StandardFaultsAttribute : Attribute, IContractBehavior
{
    // this is a list of our standard fault detail classes.
    static Type[] Faults = new Type[]
    {
        typeof(AuthFailure),
        typeof(UnexpectedException),
        typeof(UserFriendlyError)
    };

    public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
    }

    public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
    {
    }

    public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
    {
        foreach (OperationDescription op in contractDescription.Operations)
        {
            foreach (Type fault in Faults)
            {
                op.Faults.Add(MakeFault(fault));
            }
        }
    }

    private FaultDescription MakeFault(Type detailType)
    {
        string action = detailType.Name;
        DescriptionAttribute description = (DescriptionAttribute)                Attribute.GetCustomAttribute(detailType, typeof(DescriptionAttribute));

        if (description != null)
            action = description.Description;
        FaultDescription fd = new FaultDescription(action);
        fd.DetailType = detailType;
        fd.Name = detailType.Name;
        return fd;
    }
}
Stickinthemud answered 1/12, 2010 at 16:48 Comment(2)
+1 This does help. This way 1 attribute can be applied at the ServiceContract interface level, instead of to each individual method in the interface.Penmanship
I agree, this should be marked the answer. Worked great for me, at least with a client also using WCF.Filippo
P
6

No, you need to do it on each and every method - WCF is rather picky and requires explicit settings pretty much for everything (which really is a good thing in the end, I am convinced).

Marc

Pneumonectomy answered 8/9, 2009 at 7:52 Comment(3)
Surely you'd agree that it should be possible to implement cross-cutting concerns like security in an orthogonal manner. It seems odd to me that I can implement a nice ServiceAuthorizationManager in a decoupled manner, but then I have to go sprinkle my contracts with repetitive junk if I want to throw a custom security fault.Bicarbonate
I agree, I'm looking for a method to do the repetitive junk automatically, too.Welty
You can write some T4 to generate all your contracts from a XML (or other) file and have your custom logic to define global faults. There is also the WSSF project on codeplex which uses a designer for your contracts and generates client/server code (it's a quite heavy tool).Dosimeter
M
0

Yes on each operation contract

Medicament answered 8/9, 2009 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.