public void Defaults() { var a = new FaultContractAttribute(typeof(MyDetail)); Assert.AreEqual(typeof(MyDetail), a.DetailType, "#1"); Assert.IsNull(a.Action, "#2"); Assert.IsNull(a.Name, "#3"); Assert.IsNull(a.Namespace, "#4"); }
static FaultInfo CreateFaultInfo(FaultContractAttribute attribute) { return(new FaultInfo() { Type = attribute.DetailType, NameSpace = attribute.Namespace, Name = attribute.Name ?? attribute.DetailType.Name + "Fault", ElementName = attribute.Name ?? attribute.DetailType.Name, Action = attribute.Action }); }
public void ProvideFault(Exception error, MessageVersion version, ref Message fault) { ServiceEndpoint endpoint = OperationContext.Current.Host.Description.Endpoints.Find(OperationContext.Current.EndpointDispatcher.EndpointAddress.Uri); DispatchOperation dispatchOperation = OperationContext.Current.EndpointDispatcher.DispatchRuntime.Operations.Where(op => op.Action == OperationContext.Current.IncomingMessageHeaders.Action).First(); OperationDescription operationDesc = endpoint.Contract.Operations.Find(dispatchOperation.Name); var attributes = operationDesc.SyncMethod.GetCustomAttributes(typeof(FaultContractAttribute), true); if (attributes.Any()) { FaultContractAttribute attribute = (FaultContractAttribute)attributes[0]; var type = attribute.DetailType; object faultDetail = Activator.CreateInstance(type); Type faultExceptionType = typeof(FaultException <>).MakeGenericType(type); FaultException faultException = (FaultException)Activator.CreateInstance(faultExceptionType, faultDetail, error.Message); MessageFault faultMessage = faultException.CreateMessageFault(); fault = Message.CreateMessage(version, faultMessage, faultException.Action); } }