【问题标题】:WCF - Error handling depends on the binding used?WCF - 错误处理取决于使用的绑定?
【发布时间】:2012-05-03 06:09:20
【问题描述】:

就所使用的绑定而言,处理异常的方式有什么不同吗?

如果我使用 WSHttpBinding 或 BasicHttpBanding,我会得到不同的结果。

例如,这是我在客户端的错误处理例程:

//MyClient client = new MyClient("WSBinding");
MyClient client = new MyClient("BasicBinding");
try
{

    Result = client.DoTheWork("test");
    Console.WriteLine(Result);
}
catch (FaultException e)
{
    if (e.Code.SubCode.Name.Equals("BadParameters"))
        Console.WriteLine("Bad parameter entered");

    Console.WriteLine(e);
}

当我使用 WSHttpBinding 时,我可以在客户端捕获异常,但是如果我使用 basicHttpHandling 我不能,我得到:

Unhandled Exception: System.NullReferenceException: Object reference not set to
an instance of an object.

这是我的 web.config,

<system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="basicBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows" />
      </security>
    </binding>
  </basicHttpBinding>   
  <wsHttpBinding>
    <binding name="wsBinding">
      <security mode="None">
        <transport clientCredentialType="None" />
        <message establishSecurityContext="false" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>
<services>
  <service name="MailboxServiceLibrary.MailboxService" behaviorConfiguration="ServiceBehavior" >
    <!--endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsBinding" contract="ParServiceLibrary.IParService">
      <identity>
        <dns value="MyServer.com" />
      </identity>
    </endpoint-->     
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicBinding" contract="ParServiceLibrary.IParService"/>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="True"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

似乎 BasicHttpBinding 使用 Soap 1.1 格式,而对于 WSHttpBinding,它使用 Soap 1.2 格式,但不确定这是否是原因。

谢谢,

【问题讨论】:

    标签: c# .net wcf wcf-binding wcf-client


    【解决方案1】:

    我刚刚意识到我使用了错误的方法来捕获客户端站点中的异常,

    我正在使用,

    if (e.Code.SubCode.Name.Equals("MyFaultID"))
    

    一定是这样的,

    if (e.Code.Name.Equals("MyFaultID"))
    

    这种方式在两种绑定中都可以正常工作。

    【讨论】:

      【解决方案2】:

      这对我来说不合适:

      <security mode="TransportCredentialOnly"> 
      

      它应该是 "Transport" 、 "Message" 或 "TransportWithMessageCredential" - 不确定这是否导致了您的问题,但您已经为 basicHttpBinding 添加了安全性并为 wsHttpBinding 删除了它,因此不确定这最终是否是一个公平的测试?

      【讨论】:

      • 我在 WSBinding 上删除安全性的原因是因为我喜欢远离 SSL,所以一旦部署我将删除 wsBinding。 TransportCrendentialOnly 不提供消息安全性,这对我来说没问题。谢谢
      猜你喜欢
      • 2011-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-19
      • 1970-01-01
      • 2017-02-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多