WCF in an ASP.Net Application

I posted a question on Stackoverflow.com today because I am experiancing some issues in production with a web application that is using WCF. To make a long story short, see this post.

My solution was actually quite simple and easy for me to implement. I can’t post actual code here since it’s a production project and I didn’t have time to write up a proof of concept. This is the short of it, however:

My ServiceClient object was already wrapped up in a another class where the client was created and disposed off properly. Each function call also had a prefunction and postfunction call. For example:

public string GetData()
{
   Open();
   string ret = GetData();
   Close();
   return ret;
}

Open and Close do not actually open and close the service client. Some of my functions require back to back calls so I didn’t want to be closing and opening connections repeatedly. I put these functions in, however, so that I could insert code before and after data calls, just in case I needed it. They were both empty until today. I have about 50 function calls in this WCF service, so it was a good thing I put those function calls in ahead of time!

I added 2 member variables to my class. A busy bool variable and a last used datetime. In the open function I now set busy to true and in the close I reset it back to false and also update the last used time. I then created some member properties to get how long it’s been idle (since last used time), if it’s busy, and what the communication state is (in cause of fault, but I also have fault checking inserted into Open, but let’s not go there right now).

I then created a connection pool class that simply holds a generic list of these client classes. Initially on application start, this list is blank. I created a function to get the next free client. This simply goes through this generic list and checks the communication state (has to be opened) and if it’s busy. If it’s open and not busy, I just return that client, otherwise I create a new one, add it to the pool and then return it. I also added some functions to get the state of each client and idle time, etc. For cleanup purposes, I just added a timer that executes every minute and checks to see if the idle time is over 5 minutes. If it is, it disposes that client and removes it from the pool.

My application always calls one single function to get the data client, so inserting the ability to get it from the pool was trivial. I just changed that function to get the pool object out of the Application state and then called the get next free client function and returned that.

All in all, this took less than 20 minutes to build and test. I installed it on the client’s production server since it was just crawling and a crash would be better than it’s current state anyway. I’m happy to report that things are moving quite well now. I would even venture to say that it’s running faster now than it did when I had web services set up instead of WCF.

If anyone sees a problem with this set up, or has a better way to do it, or just wants to tell me I’m dumb, go for it. I’m completely new to WCF and I will always take helpful advice.

How VB.Net can screw up a newbie without them knowing it.

Let me preface this by saying I have no hate for VB.Net. When used properly, it is just as good as C#. However, VB as a whole has gotten a bad rep because generally the people that flock to it are the ones with no formal programming experience. Thus, they don’t really always know what they are doing. I’ve seen it a lot. I will also admit that I have done a lot of these horrendous things that I should have never done. In fact, this post is about something I did that turned out way bad when we fast forward a few years later (yes, it took that long for me to find this problem.)

Here is the problem and the solution I came up with over two years ago. We have a very large MDI application that has a lot of different forms. We didn’t want users opening up multiple copies of those forms, plus we wanted a neat way to bring certain forms to the foreground if they were already open.

My simple and what I thought was ingenious solution was to create a few functions that just check to see if the form is open… Something like this:

  Public Function IsFormOpen(ByVal oFormName As Form) As Boolean
    Dim bAlreadyShown As Boolean = False
    Dim obj As Form
    For Each obj In Me.MdiChildren
      If obj.Name = oFormName.Name Then
        bAlreadyShown = True
      End If
    Next
    Return bAlreadyShown
  End Function

When I wrote that and it worked, it made sense to me that it would just pass in the type and I could get the name of the type and it would match and the world would be happy…

Now fast forward 2 years and we are getting some complaints of the application/whole computer being slow after running for a while. This smelled of memory leaks (which we have had lots of in the past). So I started doing my memory leak testing and now I am seeing all of our forms still being held open when they should be closed!

So turning off just my code debugging and putting a breakpoint in InitializeComponent() told me a big huge story. A story of how VB “helps” a developer by just doing things for them without them knowing. It seems that before going into the IsFormOpen function call, VB creates an object of that form type and passes in that object. That object never gets destroyed, either. Dispose on that object only gets called when the application exits. Nice, huh? So every time we open a form, we actually are creating two and only opening one. One simple change fixed it:

  Public Function IsFormOpen(ByVal oFormName As System.Type) As Boolean
    Dim bAlreadyShown As Boolean = False
    Dim obj As Form
    For Each obj In Me.MdiChildren
      If obj.Name = oFormName.Name Then
        bAlreadyShown = True
      End If
    Next
    Return bAlreadyShown
  End Function

Incidentally, C# wouldn’t allow the first function to happen. It won’t even compile in any way, shape, or form (at least in the way I was trying to use it). It tells me the obvious, I was trying to use a Type as a variable. VB “helps” you with that…

Forums Launched

I added forums to this site just in case there are some discussions on any of the articles or if there are other topics that anyone would want to discuss. It’s easier to ask questions and discuss solutions in a forum format instead of on a blog site.

Check it out. The link is at the top. It’s quite empty right now…

New look.

I haven’t been posting much, mainly due to a busy schedule, but also because I really didn’t like the look of the site… I have changed to a new theme. Hopefully, this will motivate me to post more.

One thing I was looking for in a new template, was a fluid layout. It seems the wordpress themes for fluid layouts are pretty scarce. Especially ones that look good. I will stick with this one for the time being and see how it works. I think my code highlighter will fix the reason I wanted a fluid layout anyway.