Search code examples
wcfexception

WCF getting meaningful channel exceptions


I have a simple WCF service with one method:

[ServiceContract]
public interface TestServiceContract
{
    [OperationContract]
    int[] Test();
}

public class TestService:TestServiceContract
{
    public int[] Test()
    {
        return new int[1000000];
    }
}

When on the client side I call

client.Test();

it fails, obviously because object I pass is too large.

BUT

instead of a meaningful description I get a totally useless

The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

I tried enabling

<serviceDebug includeExceptionDetailInFaults="true" />

but it doesn't help.

Is it possible to get a meaningful error description?


Solution

  • Use "try catch" to catch exceptions when creating service endpoints.According to your description, I did a test and found that if the passed object is too large, there will be exceptions. Here is the exception I got:

    enter image description here

    Here is my demo:

        namespace Test
        {
        [ServiceContract]
    public interface TestServiceContract
    {
        [OperationContract]
        int[] Test();
    }
    public class TestService : TestServiceContract
    {
        public int[] Test()
        {
            return new int[1000000];
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
    
            Uri baseAddress = new Uri("http://localhost:8000/GettingStarted/");
            ServiceHost selfHost = new ServiceHost(typeof(TestService), baseAddress);
            try
            {
                selfHost.AddServiceEndpoint(typeof(TestServiceContract), new WSHttpBinding(), "Test");
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);
                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <Enter> to terminate the service.");
                Console.WriteLine();
                Console.ReadLine();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
    

    }

    This is the server-side code.

           static void Main(string[] args)
        {
            WSHttpBinding myBinding = new WSHttpBinding();
    
            EndpointAddress myEndpoint = new EndpointAddress("http://localhost:8000/GettingStarted/Test");
    
            ChannelFactory<TestServiceContract> myChannelFactory = new ChannelFactory<TestServiceContract>(myBinding, myEndpoint);
            TestServiceContract wcfClient1 = myChannelFactory.CreateChannel();
            wcfClient1.Test();
    
        }
    

    This is the client-side code.I create a channel factory to call the service. You can also use Svcutil to generate proxy classes to call services.