0

I am writing a WCF service and have a default Fault implementation that I am handling with an IErrorHandler.ProvideFault:

    public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
    {
        log.Error("ProvideFault called. Converting Exception to AdministrationFault....");

        var defaultFault = new FaultException<AdministrationFault>(
            new DefaultFault { Reason = error.Message }, error.Message);

        fault = Message.CreateMessage(version,
            defaultFault.CreateMessageFault(),
            DEFAULT_FAULT_ACTION);
    }

The const you see DEFAULT_FAULT_ACTION needs to have the same value as the FaultContractAttribute of my service. Given that my service is defined in an interface:

[ServiceContract(Namespace = "http://example.com/service/IFooService")]
public interface IFooService
{
    [OperationContract]
    [FaultContract(typeof(DefaultFault),
        Action = "http://example.com/service/IFooService/fault/DefaultFault")]
    void AddUser(UserInfo userInfo, string password);
}

I cannot create const variables to store the namespace/action values. So for now I am forced to create a class to store the constants:

public class StupidHackyConstClass {
    public const string NAMESPACE = "http://exampl.com/service/IFooService";
    public const string DEFAULT_FAULT_ACTION = NAMESPACE + "/fault/DefaultFault"
}

So that i can refactor the interface attributes and the error handler implementation to use common values. I know a class for consts is a terrible anti-pattern, but it seems i am backed into a hole here... Any suggestions?

Lucas
  • 14,227
  • 9
  • 74
  • 124
  • I don't get why a static class for constants only is anti-pattern. I think your code is just fine. E.g. http://stackoverflow.com/questions/1263954/is-global-constants-an-anti-pattern – Thuan Oct 30 '16 at 16:11
  • @thuan, thanks, possible i just inaccurately remembered the Effective Java _dont use interfaces just to declare constants_... – Lucas Oct 30 '16 at 16:16

0 Answers0