Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the configuration behavior) on the server

前端 未结 6 1522
[愿得一人]
[愿得一人] 2020-11-27 10:12

I have a WCF service that has been working perfectly, and something has changed and I don\'t know what.

I get this exception:

System.ServiceMo

相关标签:
6条回答
  • 2020-11-27 10:28

    I was also getting the same error, the WCF was working properly for me when i was using it in the Dev Environment with my credentials, but when someone else was using it in TEST, it was throwing the same error. I did a lot of research, and then instead of doing config updates, handled an exception in the WCF method with the help of fault exception. Also the identity for the WCF needs to be set with the same credentials which are having access in the database, someone might have changed your authority. Please find below the code for the same:

     [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [FaultContract(typeof(ServiceData))]
        ForDataset GetCCDBdata();
    
        [OperationContract]
        [FaultContract(typeof(ServiceData))]
        string GetCCDBdataasXMLstring();
    
    
        //[OperationContract]
        //string GetData(int value);
    
        //[OperationContract]
        //CompositeType GetDataUsingDataContract(CompositeType composite);
    
        // TODO: Add your service operations here
    }
    
      [DataContract]
    public class ServiceData
    {
        [DataMember]
        public bool Result { get; set; }
        [DataMember]
        public string ErrorMessage { get; set; }
        [DataMember]
        public string ErrorDetails { get; set; }
    }
    

    in your service1.svc.cs you can use this in the catch block:

     catch (Exception ex)
            {
                myServiceData.Result = false;
                myServiceData.ErrorMessage = "unforeseen error occured. Please try later.";
                myServiceData.ErrorDetails = ex.ToString();
                throw new FaultException<ServiceData>(myServiceData, ex.ToString());
            }
    

    And use this in the Client application like below code:

      ConsoleApplicationWCFClient.CCDB_HIG_service.ForDataset ds = obj.GetCCDBdata();
    
                string str = obj.GetCCDBdataasXMLstring();
    
            }
    
            catch (FaultException<ConsoleApplicationWCFClient.CCDB_HIG_service.ServiceData> Fex)
          {
              Console.WriteLine("ErrorMessage::" + Fex.Detail.ErrorMessage + Environment.NewLine);
              Console.WriteLine("ErrorDetails::" + Environment.NewLine + Fex.Detail.ErrorDetails);
              Console.ReadLine();
          }
    

    Just try this, it will help for sure to get the exact issue.

    0 讨论(0)
  • 2020-11-27 10:32

    As the error information said first please try to increase the timeout value in the both the client side and service side as following:

    <basicHttpBinding>
        <binding name="basicHttpBinding_ACRMS" maxBufferSize="2147483647"
          maxReceivedMessageSize="2147483647"
          openTimeout="00:20:00" 
          receiveTimeout="00:20:00" closeTimeout="00:20:00"
          sendTimeout="00:20:00">
          <readerQuotas maxDepth="32" maxStringContentLength="2097152"
            maxArrayLength="2097152" maxBytesPerRead="4006" maxNameTableCharCount="16384" />
        </binding>
    

    Then please do not forget to apply this binding configuration to the endpoint by doing the following:

    <endpoint address="" binding="basicHttpBinding" 
          bindingConfiguration="basicHttpBinding_ACRMS"
          contract="MonitorRAM.IService1" />
    

    If the above can not help, it will be better if you can try to upload your main project here, then I want to have a test in my side.

    0 讨论(0)
  • 2020-11-27 10:41

    It's in the app.config file.

    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceDebug includeExceptionDetailInFaults="true"/>
    
    0 讨论(0)
  • 2020-11-27 10:42

    Define a behavior in your .config file:

    <configuration>
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="debug">
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        ...
      </system.serviceModel>
    </configuration>
    

    Then apply the behavior to your service along these lines:

    <configuration>
      <system.serviceModel>
        ...
        <services>
          <service name="MyServiceName" behaviorConfiguration="debug" />
        </services>
      </system.serviceModel>
    </configuration>
    

    You can also set it programmatically. See this question.

    0 讨论(0)
  • 2020-11-27 10:45

    If you want to do this by code, you can add the behavior like this:

    serviceHost.Description.Behaviors.Remove(
        typeof(ServiceDebugBehavior));
    serviceHost.Description.Behaviors.Add(
        new ServiceDebugBehavior { IncludeExceptionDetailInFaults = true });
    
    0 讨论(0)
  • 2020-11-27 10:45

    You can also set it in the [ServiceBehavior] tag above your class declaration that inherits the interface

    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class MyClass:IMyService
    {
    ...
    }
    

    Immortal Blue is correct in not disclosing the exeption details to a publicly released version, but for testing purposes this is a handy tool. Always turn back off when releasing.

    0 讨论(0)
提交回复
热议问题