API's that Suck

March 6, 2009

WCF Clients – 1 line to create, 16 to destory

Filed under: Uncategorized — Grauenwolf @ 10:40 pm

What most people write when using WCF.

Using service As New SampleClient
    service.DoSomething()
End Using

The right way according to Microsoft. (Assuming you don’t want unexpected exceptions, lingering connections, and other bad things.)

Dim service As New SampleClient
Try
    service.DoSomething()
Finally
    Try
        If service IsNot Nothing Then
            If service.State <> CommunicationState.Faulted Then
                service.Close()
            Else
                service.Abort()
            End If
        End If
    Catch ex As TimeoutException
        service.Abort()
    Catch ex As CommunicationException
        service.Abort()
    Catch ex As Exception
        service.Abort()
        Throw
    End Try
End Try

Advertisements

3 Comments »

  1. The best I can find on the MS site to verify this is http://msdn.microsoft.com/en-us/library/ms733912.aspx. They didn’t seem to recommend the catchall case though…

    Comment by JohnW — March 7, 2009 @ 1:36 am

  2. I haven’t played with .NET in almost a year, but you would think that the “using” keyword would take care of severing connections and releasing relevant resources. What’s the deal?

    Comment by Kunal — May 27, 2009 @ 4:47 pm

    • I suspect that WCF, like much of J2EE, was over-engineered. They wanted to do too much in one release, making it impossible for them to figure out many of the corner cases.

      Comment by grauenwolf — May 27, 2009 @ 4:58 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: