簡體   English   中英

WCF-錯誤處理取決於所使用的綁定?

[英]WCF - Error handling depends on the binding used?

在使用綁定方面,處理異常的方式是否有所不同?

如果使用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格式,但不確定是否可能是原因。

謝謝,

我只是意識到我使用錯誤的方法來捕獲客戶端站點中的異常,

我在用

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

而且一定是

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

這樣,在兩個綁定中都可以正常工作。

這在我看來不對:

<security mode="TransportCredentialOnly"> 

它應該是“ Transport”,“ Message”或“ TransportWithMessageCredential”-不確定是否引起了您的問題,但是您已經為basicHttpBinding添加了安全性,並為wsHttpBinding刪除了安全性,因此不確定是否最終是一個公平的測試?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM