C# WCF-错误处理取决于使用的绑定?
对于所使用的绑定,异常的处理方式是否存在一些差异 如果我使用WSHttpBinding或BasicHttpBanding,会得到不同的结果 例如,这是我在客户端的错误处理例程:C# WCF-错误处理取决于使用的绑定?,c#,.net,wcf,wcf-binding,wcf-client,C#,.net,Wcf,Wcf Binding,Wcf Client,对于所使用的绑定,异常的处理方式是否存在一些差异 如果我使用WSHttpBinding或BasicHttpBanding,会得到不同的结果 例如,这是我在客户端的错误处理例程: //MyClient client = new MyClient("WSBinding"); MyClient client = new MyClient("BasicBinding"); try { Result = client.DoTheWork("test"); Console.WriteLin
//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格式,但不确定这是否是原因
谢谢,我觉得这不合适:
<security mode="TransportCredentialOnly">
它应该是“Transport”、“Message”或“TransportWithMessageCredential”-不确定这是否导致了您的问题,但您已经为basicHttpBinding添加了安全性,并为wsHttpBinding删除了安全性,因此不确定这最终是否是一个公平的测试?我看不太对:
<security mode="TransportCredentialOnly">
它应该是“Transport”、“Message”或“TransportWithMessageCredential”-不确定这是否导致了您的问题,但您为basicHttpBinding添加了安全性,并为wsHttpBinding删除了安全性,因此不确定这最终是否是一个公平的测试?我刚刚意识到我使用了错误的方法来捕获客户端站点中的异常 我用的是
if (e.Code.SubCode.Name.Equals("MyFaultID"))
而且一定是,
if (e.Code.Name.Equals("MyFaultID"))
这样在两种绑定中都可以正常工作。我刚刚意识到我使用了错误的方法来捕获客户端站点中的异常 我用的是
if (e.Code.SubCode.Name.Equals("MyFaultID"))
而且一定是,
if (e.Code.Name.Equals("MyFaultID"))
这样在两种绑定中都可以很好地工作。我删除WSBinding上的安全性的原因是因为我喜欢远离SSL,所以一旦部署,我将删除WSBinding。TransportCredentialy不提供消息安全性,对我来说没有问题。感谢我删除WSBinding上的安全性的原因是因为我喜欢远离SSL,所以一旦部署,我将删除WSBinding。TransportCredentialy不提供消息安全性,对我来说没有问题。谢谢