I have created a Wcf service. It will be access by both Wcf clients and non Wcf clients. I created my own class for the FaultException handling as below;
[DataContract]
public class ErrorResponse
{
[DataMember]
public string ErrMsg {get;set;}
}
For my service interface I have
[ServiceContract]
public interface IService
{
[OperationContract]
[FaultContract (typeof(ErrorResponse))]
[WebInvoke(Method = "POST", UriTemplate = "/XML/GetTypes", BodyStyle = WebMessageBodyStyle.Bare)]
TypeResponse XMLTypes(TypeRequest TypeRequest);
}
In my method XmlTypes I have the following;
public static TypeResponse XmlTypes(TypeRequest TypeRequest)
{
//do something
//raise a error
ErrorResponse oErrorResponse = new ErrorResponse();
oErrorResponse.ErrMsg = "Some Error happened";
FaultCode oFaultCode = new FaultCode("12345");
throw new FaultException<ErrorResponse>(oErrorResponse , new FaultReason ("Reason for the fault"),
new FaultCode("TypeRequestFailed", new FaultCode("TypeNotFound")));
This appears to work fine for a Wcf client.
However, when making a call from a non Wcf Client, for example using WebClient UploadString (I know I can use Service Reference, this is for test purposes), I get back
System.Net.WebException: The remote server returned an error: (400)
Bad Request.
This is my webclient code in another test app;
WebClient oClient = new WebClient();
oClient.Encoding = UTF8Encoding.UTF8;
oClient.Headers.Add("Content-Type", "application/xml");
try
{
txtResponse.Clear();
sRequest = "<TypeRequest><UserId>1</UserId><Password>asdax12</Password></TypeRequest>";
txtResponse.Text = oClient.UploadString("http://localhost:49562/Service.svc/XML/XmlTypes", "POST", sRequest).ToString();
}
catch (Exception ex)
{
txtResponse.Text = ex.ToString();
}
In my webconfig file I added the following as taken from this example throwing soap faults for non wcf clients
<system.serviceModel>
<bindings>
<customBinding>
<binding name="basicHttpSoap12Binding">
<textMessageEncoding messageVersion="Soap12"/>
<httpTransport/>
</binding>
</customBinding>
</bindings>
<services>
<service name="MySoap12Service">
<endpoint address="" binding="customBinding" bindingConfiguration="basicHttpSoap12Binding"
bindingNamespace="MySoap12ServiceNamespace"
contract="MySoap12Service">
</endpoint>
</service>
</services>
</system.serviceModel>
Where am I going wrong ?