1

I've written a class in VB.Net that is consumed in an ASP.NET Web Application running IIS7. I use .NET Framework 4.0. The class performs a REST request to an online and retrieves an XML response containing strongly typed data.

This class also performs caching using an SQL Server database. The class is compiled to a .DLL and referenced by the Web Application. It works excellent and now I need to know how to make the class thread-safe.

I have no experience with making code 'thread-safe'. I don't know where to begin in determining whether or not it is thread-safe. Am assuming, because I didn't pay attention to this during development, that it is NOT thread-safe and that since it the web application will be used by many users at the same time that it must be payed attention to.

Can anyone point me on how to test for thread-safety? Are there any resources online that that will give me some ideas? Are there any rules of thumb that will tip me off as to where my main concerns are?

rwkiii
  • 5,716
  • 18
  • 65
  • 114
  • 1
    There's not really a "test" per se - even if your class is thread safe, you'll probably have contention at the database level. Your best bet is probably to write functional tests, but without code it's kinda hard to speculate. – lukiffer May 29 '12 at 04:44
  • http://stackoverflow.com/questions/1715822/unit-test-for-thread-safe-ness and http://stackoverflow.com/questions/6675905/which-features-make-a-class-to-be-thread-safe – Habib May 29 '12 at 04:51
  • is your class modifying data in any way? – Grzegorz W May 29 '12 at 06:18
  • To the extent that I add data to the db if it doesn't already exist, yes. But I don't have any routines that are modifying existing data. – rwkiii May 31 '12 at 07:33
  • Doing some research, I've read some articles that seem to indicate that thread-safety isn't a huge concern when running an ASP.NET application under IIS 7. I've also read that VB.NET DLLs don't have a big problem with thread-safety. I don't have a big need for SHARED classes, properties or functions, so I've mostly stayed away from them. After reading some good articles about IIS/ASP and VB DLLs, I'm optemistic that I'm probably close. I just don't grasp how test for problems. lukiffer, I'm not sure what code would help. – rwkiii May 31 '12 at 07:44

1 Answers1

1

The easiest possible thing you can look out for is the use of "static" (C#) or "Shared" (VB.NET) variables. If these variables can be modified throughout the lifetime of the application you will likely run into threading issues which can really often result in "random looking" problems.

I would also be concerned about how you are doing the caching in your database as multiple .NET threads hitting SQL (for the cache) could cause issues as well depending on how its designed.

Bottom line is you are likely going to need to learn more about threading if you want to be sure this is going to not have issues. Probably the best book I have ever read in terms of simple to very complex C# topics is C# 4.0 In a Nutshell I would take a look at that book especially the threading chapters. (Seriously read the whole thing though) If you get that read through and have a good understanding of the concepts mentioned you should be fine.

John Culviner
  • 22,235
  • 6
  • 55
  • 51
  • C seems to cause confusion for me. I can eyeball it and get a good idea of what C code is doing, but I'm just not experienced with it. Some things that confuse me are like disposing of objects created in C - I do understand it needs to be done to avoid memory leaks, but it doesn't need to be done in VB. So when I read about C and learn something new, I never know when it applies to VB. Their code can look very much alike, but they are very different. Also, as I commented above, I have been paying attention to SHARED declarations. Is there a problem with multiple reads of db at the same time? – rwkiii May 31 '12 at 07:51
  • C# (you mean C# right?) and VB.NET have different syntax but the underlying compiled code is exactly the same for the most part. If you think you are are seeing differences between how C# and VB.NET work I recommend reading about the differences in syntax. This site has worked very well in the past for me to help train VB.NET -> C# converts http://www.harding.edu/fmccown/vbnet_csharp_comparison.html – John Culviner Jun 06 '12 at 05:24
  • Also, there is likely not a problem with doing multiple database reads at the same time as long as you are not accessing "shared/static" variables that are global to the application – John Culviner Jun 06 '12 at 05:26
  • Thank you John. I'm going to take a look at this URL tonight. Would you mind telling me why you recommend I read up on C# as apposed to VB? I doubt if I'd have too tough of a time catching on to C# if I set my mind to it, but I've been doing VB for about 30 years now. Once upon a time, VB was limited and couldn't do a lot of things C# could do, but it's gotten much better in that regard. I don't experience limitations with it, for my purposes anyway. I'm just wondering if reading C# material is going to help me understand threading as it pertains to coding in VB. Respectfully, of course. – rwkiii Jun 10 '12 at 03:35
  • Sorry about that, I only was mentioning C# because that book is written in it. I'm not aware of any really good VB.NET language books out there but I'm sure there are some. The concepts are pretty much the same between the two and I have found I felt like I became a much better programmer in VB.NET when I learned C#, same thing happens for people learning a new spoken language as well – John Culviner Jun 11 '12 at 02:52