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;
}
}
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