Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server
Asked Answered
W

6

176

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.ServiceModel.FaultException: 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 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 3.0 SDK documentation and inspect the server trace logs.

This is confusing because I am running .NET 4.0.

Where do I turn on IncludeExceptionDetailInFaults? I'm battling to find it.

Winsor answered 29/11, 2011 at 18:16 Comment(0)
M
288

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.

Mayle answered 29/11, 2011 at 18:20 Comment(11)
Hi Otivel, I my case there are nested folders containing different site and services. The folder where my service resides and I am getting error is at third degree of nesting relative to main web application and I have dedicated web.config for each service. I change my corresponding web.config accordingly to add <serviceDebug includeExceptionDetailInFaults="true" />. But I am still getting the error. Do I need to change all the web.config in complete web application?Chippewa
@AkshayKulkarni: Not sure, I don't have experience with your case. Make sure your services have a reference to the serviceBehavior (check gagogra answer) first. If that doesn't solve the problem, please ask a question on SO.Mayle
Hi, could you put the entire tree so we know where to put these XML fragments?Orman
@MatthewLock: Updated answer. Also, check <behavior> and <service> if you need more details.Mayle
Visual Studio tells me that serviceBehaviors can't be a direct child of system.serviceModel. Ended up going with rich.okelly's answer.Fauteuil
Note: VS2013 puts the <serviceDebug> tag in the default Web.config with it set to false. If you don't notice like I didn't and add the XML above apparently what's in last in the file wins. Hope this is useful to someone out there.Naphthalene
The error is still the same, but its type has changed. Now it's FaultException<ExceptionDetail> which has property Detail.InnerException where you find server exception.Kalgoorlie
Where can I find the name of "MyServiceName" ?Motherofpearl
There is an error if you copy and paste the second code block. There is no close tag, should be: <service name="MyServiceName" behaviorConfiguration="debug"/>Barrister
@Motherofpearl "MyServiceName" should be the fully qualified name (= with namespace) of the type that provides an implementation of a service contract. For instance, "MyNamespace.MyServiceType". (see documentation)Mayle
Debug information should not be used in production environments. In Production environment disable the tag, set to false like includeExceptionDetailInFaults="false"Manifold
A
66

It's in the app.config file.

<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceDebug includeExceptionDetailInFaults="true"/>
Allwein answered 29/11, 2011 at 18:19 Comment(1)
Don't set name attribute of <behavior> (as in @Mayle answer) if you want it to apply to all your services.Plumper
L
51

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 });
Labourer answered 29/1, 2014 at 22:42 Comment(1)
Add this to your ServiceHost object instance: Example: ServiceHost serviceHost = new ServiceHost(Program.serviceInstance);Adamson
R
30

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.

Reprimand answered 10/10, 2014 at 13:34 Comment(1)
I used this in an application that runs on the backend and will never be publicly viewable, so this works perfectlyDisunite
S
4

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.

Supremacist answered 8/5, 2013 at 11:21 Comment(2)
You should NOT be exposing the underlying exception details. The whole purpose for the separation of exceptions between client and server and the need for this flag at all, is to prevent exception information being made available to the client. A malicious user could use this information to manipulate your service! If you're developing, use the behavior IncludeExceptionDetailInFaults as described to propagate the whole exception, or in deployment, raise a faultexception giving a very basic error, such as "Unable to save file" rather than giving a stack trace and full details of the exception.Travis
Hi .. in my case all other service function is getting called by the function which i m using to save the file is throwing that exception...to know the exact issue i used logging system but no log is created for that method...i m creating 3 logs 1) when the service hit 2) before saving any file and 3) exception log.Dolce
T
0

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.

Turnout answered 16/2, 2016 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.