在我自己托管的WCF网络服务中,我有两种方法:
[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);
}
}
当我调用第一个函数AU_getProduto时,我抛出异常,一切都很好。当调用第二个方法AU_setProduto时,我想抛出异常,而不是抛出异常,它会给我&#34;(400)Bad Request&#34;。我的猜测是WebInvoke与WebGet的工作方式不同,而WCF在某种程度上消耗了我抛出的异常并抛出了自己的异常。以下是我的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>
那我怎么能抛出预期的异常?
提前致谢。
编辑:
我尝试删除抛出异常的部分,第二种方法可以根据需要运行。因此唯一的问题是抛出异常。