0

In my self hosted WCF webservice, I have two methods:

[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebGet(UriTemplate = "Auditoria/getProduto/{c}/{emp}")]
string AU_getProduto(string c, string emp);

[System.ServiceModel.OperationContract, WebInvoke(UriTemplate = "Auditoria/setProdutos/")]
string AU_setProdutos(Stream arq);

public string AU_getProduto(string cod, string emp) {
    if (!exist(emp)){  //exist(string e) method returns a boolean indicating 
                       //if that code is found on a database
        FaultCode code = new FaultCode("Erro");
        throw new WebFaultException<string>("Produto não cadastrado.",
                 System.Net.HttpStatusCode.Forbidden);
    }
}

public string AU_setProdutos(Stream json) {
    //parse json, get information. string emp contains the same info as 
    //the method above after parsing
    if (!exist(emp)){
        FaultCode code = new FaultCode("Erro");
        throw new WebFaultException<string>("Produto não cadastrado.",
                              System.Net.HttpStatusCode.Forbidden);
    }
}

When I call the first function, AU_getProduto, and I throw the exception, everything works great. When calling the second method, AU_setProduto, and I want to throw the exception, instead of thrown exception it will give me "(400) Bad Request". My guess is WebInvoke is working different than WebGet, and WCF is somehow consuming my thrown exception and throwing its own. Following is my app.config:

<?xml version="1.0"?>
<configuration>
    <system.serviceModel>
      <bindings>
        <webHttpBinding>
          <binding name="streamedBinding" >
          </binding>
        </webHttpBinding>
      </bindings>
     <services>
       <service name="WebService.RestService" behaviorConfiguration="Default">
         <host>
           <baseAddresses>
           </baseAddresses>
         </host>
         <endpoint name="WebService.RestSevice" address="" bindingConfiguration="streamedBinding" binding="webHttpBinding" behaviorConfiguration="webBehavior" contract="WebService.ICarga">
         </endpoint>
       </service>
     </services>
     <behaviors>
       <endpointBehaviors>
         <behavior name="webBehavior">
           <webHttp />
         </behavior>
       </endpointBehaviors>
       <serviceBehaviors>
         <behavior name="Default">
           <serviceMetadata httpGetEnabled="true"/>
           <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
         </behavior>
       </serviceBehaviors>
     </behaviors>
   </system.serviceModel>
   <startup>
     <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
   </startup>
</configuration>

So how can I throw intended exception?

Thanks in advance.

EDIT:

I tried removing the part where exception is thrown and the second method work as desired. So the only problem with it is throwing the exception.

jRicardo
  • 804
  • 10
  • 20
  • Have you checked the format of your request. I've found that typically, if you are getting a 400 from a WCF HTTP service, your content-type is incorrect or your body is not formatted correctly. – Zeus82 Jul 21 '14 at 18:59
  • In my case, I'm not really using any specific format. The 1st method, for example, can be used in a browser and it will return some XML saying it's a string and with the result. So, basically, the format is string. What is strange is that while the 1st method works perfectly, the second don't. – jRicardo Jul 21 '14 at 19:12
  • In the second method, you can't just use a browser. You have to do an HTTP POST (I believe), so if you are using Chrome, you can install something like the POstMan app. You have metadata enabled, so you should be able to browse to "http:///help" to see examples on how to call each method. – Zeus82 Jul 21 '14 at 19:16
  • Yes, yes, I never mentioned using the browser for the second method. I just said that because the 1st one can be used in a browser. I'm actually using a .NET CF app to consume the service. – jRicardo Jul 21 '14 at 19:18
  • does your 2nd method get hit if you put a breakpoint there? – wal Jul 21 '14 at 23:47
  • Yes. Exception is thrown, too, because I put a try/catch around the throw part and the exception was thrown. Like I said, it's like WCF is consuming my thrown exception and throwing it's own. – jRicardo Jul 22 '14 at 12:35

0 Answers0