·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> WCF服务的异常消息

WCF服务的异常消息

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

WCF服务的异常消息

原创地址:http://www.cnblogs.com/jfzhu/p/4055024.html

转载请注明出处

WCF Service发生异常的时候,客户端一般只能看见这样一个错误:“The server encountered an error PRocessing the request”,而异常的类型和引起异常的代码都没有显示,究其原因是出于安全考虑。如果想要暴露这些异常信息的细节给客户端,只需要在服务器的配置文件上修改<serviceDebug includeExceptionDetailInFaults="true" />。

(一)SOAP WCF Service的异常

(1) 当serviceDebug includeExceptionDetailInFaults="false"

IDemoService.cs:

using System.ServiceModel;namespace WCFDemo{        [ServiceContract(Name = "IDemoService")]    public interface IDemoService    {        [OperationContract]        int Divide(int numerator, int denominator);    }}

DemoService.cs:

using System;using System.ServiceModel.Activation;namespace WCFDemo{    [aspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]    public class DemoService : IDemoService    {        public int Divide(int numerator, int denominator)        {            return numerator / denominator;        }    }}

web.config

<?xml version="1.0"?> <configuration>     <system.web>       <compilation targetFramework="4.0" />     </system.web>    <system.serviceModel>       <services>         <service name="WCFDemo.DemoService" behaviorConfiguration="metaBehavior">           <endpoint address="DemoService" binding="basicHttpBinding" contract="WCFDemo.IDemoService" />           <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>           <host>             <baseAddresses>               <add baseAddress="http://localhost:8080"/>             </baseAddresses>           </host>         </service>       </services>         <behaviors>             <serviceBehaviors>                 <behavior name="metaBehavior">                     <serviceMetadata httpGetEnabled="true" />                     <serviceDebug includeExceptionDetailInFaults="false" />                 </behavior>             </serviceBehaviors>         </behaviors>         <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />     </system.serviceModel> </configuration>

建立一个Windows Client,Form1.cs:

private void buttonCalculate_Click(object sender, EventArgs e) {     DemoServiceReference.DemoServiceClient demoServiceClient = new DemoServiceReference.DemoServiceClient();     textBoxResult.Text = demoServiceClient.Divide(Convert.ToInt32(textBoxNumerator.Text), Convert.ToInt32(textBoxDenominator.Text)).ToString(); } 

Client app.config

<?xml version="1.0" encoding="utf-8" ?> <configuration>     <system.serviceModel>         <bindings>             <basicHttpBinding>                 <binding name="BasicHttpBinding_IDemoService" />             </basicHttpBinding>         </bindings>         <client>             <endpoint address="http://169.254.14.147:8080/DemoService.svc/DemoService"                 binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDemoService"                 contract="DemoServiceReference.IDemoService" name="BasicHttpBinding_IDemoService" />         </client>     </system.serviceModel> </configuration>

在正常情况下的消息为:

image

image

Request Body:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Body>    <Divide xmlns="http://tempuri.org/">      <numerator>100</numerator>      <denominator>10</denominator>    </Divide>  </s:Body></s:Envelope>

Response Body:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Body>    <DivideResponse xmlns="http://tempuri.org/">      <DivideResult>10</DivideResult>    </DivideResponse>  </s:Body></s:Envelope>

在发生异常时:

image

image

Request Header:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Body>    <Divide xmlns="http://tempuri.org/">      <numerator>100</numerator>      <denominator>0</denominator>    </Divide>  </s:Body></s:Envelope>

Response Body:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Body>    <s:Fault>      <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>      <faultstring xml:lang="en-US">The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the &lt;serviceDebug&gt; configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.</faultstring>    </s:Fault>  </s:Body></s:Envelope>

(2)当serviceDebug includeExceptionDetailInFaults="true"

可以看到异常的消息为”Attempted to divide by zero.”

image

response body:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">  <s:Body>    <s:Fault>      <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>      <faultstring xml:lang="en-US">Attempted to divide by zero.</faultstring>      <detail>        <ExceptionDetail xmlns="http://schemas.datacontract.org/2004/07/System.ServiceModel" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">          <HelpLink i:nil="true"/>          <InnerException i:nil="true"/>          <Message>Attempted to divide by zero.</Message>          <StackTrace>            at WCFDemo.DemoService.Divide(Int32 numerator, Int32 denominator)&#xD;            at SyncInvokeDivide(Object , Object[] , Object[] )&#xD;            at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]&amp; outputs)&#xD;            at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&amp; rpc)&#xD;            at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&amp; rpc)&#xD;            at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&amp; rpc)&#x