tags:

views:

30

answers:

2

I am a new to WCF. I have written ajax to use a web service before, but on this project I am trying to use ajax to WCF.

After I build the project and wcf using ajax, I receive the return successfully. But, 10 or more minutes later I don't get a return, the ajax calls the error function, and the fiddler returns nothing.

If I rebuild the project without any source modifying, I receive the return successfully again.

Is their anybody who has experienced this or knows why this might be?

Thank You.

A: 

Most likely you're not closing the connections. You should wrap all your calls in Try/Catch/Finally blocks.

In C#:

ServiceClient service = GetService();

        try
        {
            SomeRequest request = new SomeRequest();

            SomeResponse response = service.GetSome(request);

            return response.Result;
        }
        catch (Exception ex)
        {
            // do some error handling
        }
        finally
        {
            try
            {
                if (service.State != CommunicationState.Faulted)
                {
                    service.Close();
                }
            }
            catch (Exception ex)
            {
                service.Abort();
            }
        }

or VB

        Dim service As ServiceClient = GetService()

        Try
            Dim request As New SomeRequest()

            Dim response As SomeResponse = service.GetSome(request)

            Return response.Result
        Catch ex As Exception
            ' do some error handling
        Finally
            Try
                If service.State <> CommunicationState.Faulted Then
                    service.Close()
                End If
            Catch ex As Exception
                service.Abort()
            End Try
        End Try
sparto
A: 

Here is the best practice for calling WCF services:

    public static void CallService<T>(Action<T> action) where T 
           : class, ICommunicationObject, new()
    {
        var client = new T();

        try
        {
            action(client);
            client.Close();
        }
        finally
        {
            if (client.State == CommunicationState.Opened)
            {
                try
                {
                    client.Close();
                }
                catch (CommunicationObjectFaultedException)
                {
                    client.Abort();
                }
                catch (TimeoutException)
                {
                    client.Abort();
                }
            }
            if (client.State != CommunicationState.Closed)
            {
                client.Abort();
            }
        }
    }

Each WCF call should create a new instance of your service class. This code allows you to enforce that and just call the services like this:

CallService<MyService>( t => t.CallMyService());
Nate Noonen
This also does not swallow unnecessary fault exceptions (that you may throw) from within your service code. This only catches and handles communication faults specifically around the client and allows proper exception wrapping around your service calls without messing with the communication channel all over the place.
Nate Noonen