C# 自托管WCF引发400个错误请求,而不是WebFaultException

C# 自托管WCF引发400个错误请求,而不是WebFaultException,c#,web-services,wcf,exception,self-hosting,C#,Web Services,Wcf,Exception,Self Hosting,在我的自托管WCF Web服务中,我有两种方法: [System.ServiceModel.OperationContract] [System.ServiceModel.Web.WebGet(UriTemplate = "Auditoria/getProduto/{c}/{emp}")] string AU_getProduto(string c, string emp); [System.ServiceModel.OperationContract, WebInvoke(UriTempla

在我的自托管WCF Web服务中,我有两种方法:

[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);
    }
}
[System.ServiceModel.OperationContract]
[System.ServiceModel.Web.WebGet(UriTemplate=“Auditoria/getProduto/{c}/{emp}”)]
字符串AU_getProduto(字符串c,字符串emp);
[System.ServiceModel.OperationContract,WebInvoke(UriTemplate=“Auditoria/setProdutos/”)]
串AU_setProdutos(流arq);
公共字符串AU_getProduto(字符串cod、字符串emp){
if(!exist(emp)){//exist(string e)方法返回一个指示
//如果在数据库中找到该代码
故障代码=新的故障代码(“Erro”);
抛出新的WebFaultException(“Produto onão cadastrado.”,
System.Net.HttpStatusCode.Forbidden);
}
}
公共字符串AU_setProdutos(流json){
//解析json,获取信息。字符串emp包含与
//解析后使用上述方法
如果(!存在(emp)){
故障代码=新的故障代码(“Erro”);
抛出新的WebFaultException(“Produto onão cadastrado.”,
System.Net.HttpStatusCode.Forbidden);
}
}
当我调用第一个函数AU_getProduto并抛出异常时,一切都很正常。当调用第二个方法AU_setProduto时,我想抛出异常,而不是抛出异常,它将给我“(400)错误请求”。我猜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>

那么我如何抛出预期的异常呢

提前谢谢

编辑:


我尝试删除引发异常的部分,第二个方法可以按预期工作。因此,它唯一的问题是抛出异常。

您是否检查了请求的格式。我发现,通常情况下,如果您从WCF HTTP服务获得400,您的内容类型不正确,或者您的正文格式不正确。在我的情况下,我实际上没有使用任何特定的格式。例如,第一个方法可以在浏览器中使用,它将返回一些XML,表示它是一个字符串,并返回结果。所以,基本上,格式是字符串。奇怪的是,虽然第一种方法可以完美地工作,但第二种方法却不能。在第二种方法中,你不能只使用浏览器。你必须做一个HTTP POST(我相信),所以如果你使用的是Chrome,你可以安装类似邮递员的应用程序。您已启用元数据,因此应该能够浏览到“http:///help“查看如何调用每个方法的示例。是的,是的,我从未提到使用浏览器来调用第二个方法。我这么说是因为第一个可以在浏览器中使用。实际上,我正在使用.NET CF应用程序来使用该服务。如果在那里设置断点,您的第二个方法是否会被命中?