发布WCF后出现获取FaultException错误

本文关键字:获取 FaultException 错误 WCF 发布 | 更新日期: 2023-09-27 18:28:46

我已经为我的一个桌面应用程序和主机开发了WCF服务,并将其安装到带有DB的Azure服务器中。

我刚刚在本地桌面应用程序上连接了WCF服务Url。

有一次我从应用程序调用了登录方法。那么没有问题。

一旦我调用了第二个方法,我总是得到FaultException错误

错误类似于查看内部异常以了解详细错误。但一旦我进入内部,那么内部异常为空。

我还将<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />放入我的wcf-web.config 中

一旦我用桌面应用程序将本地WCF与livedb连接起来,一切都会正常工作

一旦我用桌面应用程序将Live WCF与Live db连接起来,就无法插入和更新数据

我的第二个方法有这个代码。

 public class Service1 : IService1
    {
     public void method1(int nm_Id, string f_LoginStatus)
            {
                class1 lo_class = new class1();
                    DateTime dt = DateTime.UtcNow;
                    //check records exist or not
                        lo_class.dt_date = dt;
                        lo_class.dtCreatedOn = dt;
                        lo_tblAttendance.dtUpdatedOn = dt;
                        _context.tblAttendances.Add(lo_tblAttendance);
                        Save();
                    }
           }
    }
 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(InitializationFault))]
        void method1(int nm_Id, string f_LoginStatus);
     }

   [DataContract]
    public class InitializationFault
    {
        public InitializationFault(Exception exc, string msg)
        {
            Exception = exc;
            Message = msg;
        }
        [DataMember]
        public Exception Exception { get; set; }
        [DataMember]
        public string Message { get; set; }
    }

从桌面应用程序调用上述方法:

  lo_loginServices = new MyService.Service1Client();
  try
                    {
                        lo_loginServices.method1(lo_EmpId, "In"); // getting inner exception error here
                    }
                    catch (FaultException<InitializationFault> ex)
                    {
                        Console.WriteLine("FaultException<InitializationFault>: " + ex.Detail);
                        //more
                    }

我已经写了catch部分,但我也看不到实际的错误。

发布WCF后出现获取FaultException错误

您可以尝试在Fault中包含异常详细信息。将IncludeExceptionDetailInFaults设置为true以使异常信息能够流到客户端进行调试。

阅读此处

<configuration>  
<system.serviceModel>  
  <services>  
    <!-- Step 1. Add a behaviorConfiguration attribute --> 
    <service  
      name="Microsoft.WCF.Documentation.SampleService"  
      behaviorConfiguration="metadataAndDebug">  
      <host>  
        <baseAddresses>  
          <add baseAddress="http://localhost:8080/SampleService" />  
        </baseAddresses>  
      </host>  
      <endpoint  
         address="mex"  
         binding="mexHttpBinding"  
         contract="IMetadataExchange"  
      />  
    </service>  
  </services>  
  <behaviors>  
    <serviceBehaviors>  
      <!-- Step 2. Add a new behavior below.-->
      <behavior name="metadataAndDebug">  
        <serviceMetadata  httpGetEnabled="true" httpGetUrl=""/>  
      <!-- Step 3. Add a <serviceDebug> element -->
    <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />  
      </behavior>  
    </serviceBehaviors>  
  </behaviors>  
</system.serviceModel>  
</configuration>