0

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 ?

neildt
  • 5,101
  • 10
  • 56
  • 107
  • Can you show an example of how you're using `WebClient`? – Tim Feb 18 '14 at 17:20
  • What happens if you don't throw the fault? Faults are meant to return a 500 error, not a 400. – John Saunders Feb 18 '14 at 17:27
  • What is the value for `txtUrl.Text`? Are you trying to call a SOAP service like a REST service? – Tim Feb 18 '14 at 17:30
  • The txtUrl.Text is http://localhost:49562/Service.svc/XML/XmlTypes – neildt Feb 18 '14 at 17:34
  • @JohnSaunders Actually I don't want to return a fault. Ideally I'd like to return the ErrorResponse object, that the client could handle. But from what I've read, people recommend using FaultException ?? – neildt Feb 18 '14 at 17:36
  • Also, looking at your posted code, the method is expecting a parameter of `TypeRequest`, and you are sending in the contents of `txtRequest.Text` - which I would expect to be a `string`. The reason this works in the WCF Test Client is because the WCF Test Client will use the SOAP endpoint (defined in your config file). – Tim Feb 18 '14 at 17:36
  • @Tommo1977 - `FaultException` is, I believe, a SOAP concept. I'm not sure if it would even work in a RESTful scenario (but I've done very little with REST, so I could be wrong). – Tim Feb 18 '14 at 17:37
  • @Tim that is what I'm wanting, to provide a fault response for RESTful scenario and SOAP. I'll update my question with regards the TypeRequest – neildt Feb 18 '14 at 17:42
  • Take a look at [WebFaultException](http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webfaultexception%28v=vs.110%29.aspx). According to the remarks, "However, WebFaultException can be used with non-REST endpoints and behaves like a regular FaultException." – Tim Feb 18 '14 at 17:53
  • Is using WebFaultException a preferred way, or am I better using an alternative approach – neildt Feb 18 '14 at 19:28

1 Answers1

1

Do you have any other web method you can call successfully? (from the same non WCF client)

Since you have a 400 http error seems to me like the call is not being built or done in the right way, so if you can successfully call any other method in the same service that will help us make sure the call is correct.

Jportelas
  • 646
  • 10
  • 21
  • So here are 2 links that might help: http://dotnet.dzone.com/news/wcf-rest-tip-2 and also this: http://stackoverflow.com/questions/4461254/wcf-rest-services-generic-exception-handling – Jportelas Feb 18 '14 at 17:59
  • I'm wanting to get a Xml or Json response for the Wcf RestFul service, is that possible – neildt Feb 19 '14 at 17:03
  • Hi, sure, but I think you should already be getting XML since that's the header you added here: oClient.Headers.Add("Content-Type", "application/xml"); – Jportelas Feb 19 '14 at 21:32