WCF无法处理自定义抛出的异常

时间:2012-11-02 12:17:36

标签: .net wcf

我创建了一个WCF服务并将其部署在服务器上。它可以正常工作,直到我抛出自定义异常(从数据库返回的数据无效)。

我需要捕获异常,并希望在客户端的异常中捕获消息并将其显示给他。但是,它将异常显示为:

  

不使用HTTP协议的服务端点绑定

这是我目前正在使用的代码。

合同:

[FaultContract(typeof(DefinedException))]
[OperationContract]
MyData GetData(TestValues values);

业务层:

public MyData GetData (TestValues values)
{
    try
    {
        data = db.GetData (values);

        if (data == null)
        {
            throw new DefinedException();
        }               
    }
    catch (Exception ex)
    {
        throw ex;
    }
    return data;
}

SVC文件:

public MyData GetData (TestValues values)
{
    try
    {
        Bo.GetData(values);
    }
    catch (DefinedException exp)
    {
        throw new FaultException< DefinedException >( exp);
    }
    catch (Exception ex)
    {
        throw new FaultException(ex.Message.ToString());
    }
    return data;
}

定义例外:

public class DefinedException: System.ApplicationException
{ }

配置:

<system.serviceModel>   
    <diagnostics wmiProviderEnabled="false" performanceCounters="Off">
      <messageLogging logEntireMessage="true"
        logMalformedMessages="true" logMessagesAtServiceLevel="true"
        logMessagesAtTransportLevel="true" maxMessagesToLog="100" />
      <endToEndTracing propagateActivity="true" activityTracing="true" messageFlowTracing="true" />
    </diagnostics>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBinding" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
          transactionFlow="false"  hostNameComparisonMode="StrongWildcard"  maxBufferPoolSize="2147483647"
           maxReceivedMessageSize="2147483647">
          <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
            maxArrayLength="16384" maxBytesPerRead="16384" maxNameTableCharCount="2147483647" />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
            enabled="false" /> 
        </binding>
      </wsHttpBinding>
    </bindings>
    <services>
      <service behaviorConfiguration="sb " name="DataService ">
        <endpoint address="wsHttpBinding"
          binding="wsHttpBinding" bindingConfiguration="wsHttpBinding" name=" wsHttpBinding" contract="IData" >
        </endpoint>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior name="NewBehavior">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name=" sb ">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

请告诉我需要更改以解决此问题。

0 个答案:

没有答案