就所使用的绑定而言,处理异常的方式有什么不同吗?
如果我使用 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 格式,但不确定这是否是原因。
谢谢,