在使用绑定方面处理异常的方式有什么不同吗?
如果我使用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格式但不确定是否可能是原因。
谢谢,
答案 0 :(得分:1)
我刚刚意识到我使用错误的方法来捕获客户端站点中的异常,
我在用,
if (e.Code.SubCode.Name.Equals("MyFaultID"))
一定是,
if (e.Code.Name.Equals("MyFaultID"))
这样两种绑定都可以正常工作。
答案 1 :(得分:0)
这对我来说不合适:
<security mode="TransportCredentialOnly">
它应该是“Transport”,“Message”或“TransportWithMessageCredential” - 不确定是否会导致您的问题,但您已经为basicHttpBinding添加了安全性并将其删除为wsHttpBinding所以不确定这是否最终是一个公平的测试?