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.