WCF WebFaultException详细信息未发送到客户端

时间:2015-05-23 14:24:51

标签: c# asp.net .net wcf iis

好的,所以..

我有一个抛出 WebFaultException<Error>(Error, HttpStatusCode.BadRequest)的WCF服务,其中Error是我自定义的可序列化对象。

当我在本地计算机和GoDaddy上托管服务时,这一切都按预期工作(如此处所示:链接已删除)。 但是当我与Arvixe一起托管时,我收到的是一个“错误请求”&#39;响应(如此处所示:链接已删除

分析响应标头,我的本地计算机,GoDaddy和Arvixe似乎都在使用相同的.NET版本。但是,我的本地计算机运行IIS 8.0,GoDaddy运行IIS 7.0,Arvixe运行IIS 8.5。

那么,导致这种差异的原因是什么? IIS 8.5处理WebFaultException的方式有何不同?我在互联网上找不到的东西表明它确实如此。或者IIS是否需要配置为返回WebFaultException?同样,我读到的所有内容都表明它完全是在ASP Web.config中配置的。

还有其他建议吗?

**编辑**

我相当肯定这与IIS而不是我的代码有关,因为它在我的本地计算机(IIS8)和GoDaddy(IIS7)上运行正常,但在Arvixe(IIS8.5)上没有运行

无论如何,这里有一些片段:

我试图返回的错误对象

    [DataContract]
public class Error {

    [DataMember]
    public int Code { get; set; }

    [DataMember]
    public string Message { get; set; }

    [DataMember]
    public string Display { get; set; }

    [DataMember]
    public List<Error> Details { get; set; }

    public Error(int code, string message, List<Error> details = null, string display = null) {
        this.Code = code;
        this.Message = message;
        this.Display = display;
        this.Details = details ?? new List<Error>();
    }
}

尝试通过以下方式将其返回给客户:

throw new WebFaultException<Error>(new Error(802, "games/asdf"), HttpStatusCode.BadRequest);

我试图抛出WebFaultException的方法:

    [OperationContract]
    [WebInvoke(
        Method = "GET", 
        UriTemplate = "/games/{game}/scores"
    )]
    List<Score> GetScores(string game);

我尝试在方法中添加[FaultContract(typeof(Error))]属性,但它没有效果。

这是Web.config注册我的服务

<?xml version="1.0"?>
<configuration>

<appSettings>
</appSettings>

<connectionStrings>
  <!-- DEV -->
  <add name="App" connectionString="XXX" />
  <add name="Log" connectionString="XXX" />
</connectionStrings>  

<system.web>

  <compilation debug="true" targetFramework="4.5" />
  <httpRuntime targetFramework="4.5" />

  <webServices>
    <protocols>
      <clear />
      <add name="HttpGet"/>
      <add name="HttpPost" />
      <add name="HttpPostLocalhost" />
    </protocols>
  </webServices>

  <pages>
    <namespaces>
      <add namespace="System.Web.Optimization" />
    </namespaces>
  </pages>

</system.web>

<system.serviceModel>

  <behaviors>

    <serviceBehaviors>

    <!--old-->
    <behavior name="OldService">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>

    <!--new-->
    <behavior name="V3">
      <serviceAuthorization serviceAuthorizationManagerType="ScoresWs.V3.Auth, ScoresWs"/>
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />          
    </behavior>

  </serviceBehaviors>

  <endpointBehaviors>
    <behavior name="REST">
      <!--faultExceptionEnabled needs to be false or else we return .net exceptions as xml instead of our custom WebFaultException-->
      <webHttp 
        helpEnabled="true"
        faultExceptionEnabled="false" 
        automaticFormatSelectionEnabled="false" 
        defaultOutgoingResponseFormat="Json" />
    </behavior>
  </endpointBehaviors>

</behaviors>

<bindings>
  <webHttpBinding>
    <binding name="defaultBinding" maxReceivedMessageSize="2097152">
      <security mode="None" />
    </binding>
  </webHttpBinding>
</bindings>

<services>

  <service name="ScoresWs.WebServiceV2" behaviorConfiguration="OldService">
    <endpoint 
      address="" 
      binding="webHttpBinding" 
      behaviorConfiguration="REST" 
      bindingConfiguration="defaultBinding" 
      contract="ScoresWs.IWebServiceV2" />
  </service>

  <service name="ScoresWs.V3.Api" behaviorConfiguration="V3">
    <endpoint
      address=""
      binding="webHttpBinding"
      behaviorConfiguration="REST"
      bindingConfiguration="defaultBinding"
      contract="ScoresWs.V3.IApi"/>
  </service>

</services>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />

</system.serviceModel>

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <directoryBrowse enabled="false" />
</system.webServer>

<runtime>

  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31ad335" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
        </dependentAssembly>
      </assemblyBinding>

  </runtime>  

</configuration>

Annnnnnd最后我在Global.asax中激活了webservices:

    public class Global : System.Web.HttpApplication {

      protected void Application_Start(object sender, EventArgs e) {
        RouteTable.Routes.Add(new ServiceRoute("api/v2", new WebServiceHostFactory(), typeof(ScoresWs.WebServiceV2)));
        RouteTable.Routes.Add(new ServiceRoute("api/v3", new WebServiceHostFactory(), typeof(ScoresWs.V3.Api)));
      }

    }

1 个答案:

答案 0 :(得分:0)

解决了!就像我怀疑的那样,它与我的代码无关。

在Arvixe主机面板中,我必须启用&#34; Show detailed ASP.NET errors in browser&#34; (WebSites-&GT;错误 - &GT; ASP.NET)。但是,由于Web.config没有更改,我无法看到IIS配置,因此我不确定这对安全性有何影响。

奇怪的是,Arvixe支持团队不会立即指导我这里考虑我的详细ASP.NET错误&#39;没有被发送给客户。