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?

Shiraz Bhaiji
  • 64,065
  • 34
  • 143
  • 252

3 Answers3

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;
    }
}
ken2k
  • 48,145
  • 10
  • 116
  • 176
cocogza
  • 350
  • 3
  • 4
  • +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. – CodingWithSpike Jan 31 '11 at 14:38
  • 1
    I agree, this should be marked the answer. Worked great for me, at least with a client also using WCF. – Josh Sklare Aug 11 '16 at 15:40
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

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 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. – Kent Boogaart Apr 28 '10 at 12:42
  • I agree, I'm looking for a method to do the repetitive junk automatically, too. – joelsand May 07 '10 at 15:24
  • 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](http://servicefactory.codeplex.com/) project on codeplex which uses a designer for your contracts and generates client/server code (it's a quite heavy tool). – SandRock Nov 06 '12 at 14:58
0

Yes on each operation contract

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216