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?