Skip to main content

Posts

Showing posts from September, 2009

Transactions and .NETs TransactionScope, what and why?

Transactions are used to add the Rollback functionality to calls, Rollback meaning the if an exception or something is thrown your call will not be executed (Commited is the term used for this). Another benefit of using Transactions is that you can group calls together, if somethign happens to one call then all of calls in that group can also be rolled back. This type of feature is particularly useful when taking user input and saving to DB, you may not want anything to get written to the DB is some error occurs on the client. .NET provides us with the System.Transaction assembly, it's not completly straight forward to use but it does hide alot of the underlying complexities. A couple of things I've learned about using System.Transactions or more specifically the System.Transactions.TransactionScope class: The safest way to use it is with the using clause. Always call Commit() before the end of the using. The optional TransactionScopeOptions enum parameter in the constructor de...

Loading Assemblies, Assembly.Load, Assembly.LoadFrom, Assembly.LoadFile, appDomain.Load

I always find this topic a great source of confusion each time I get back to it. I've just across various Exceptions being thrown in my application, FileNotFoundException and AccessViolationException. The first caused because the application cannot location a Type that one of my assemblies references and lives in another assembly. The second is caused by that same file being found but it's locked by another process i.e. something else is reading or has read and not released the assembly. Both of the above were caused by messy code which used a simple Assembly.LoadFile(...). Looks to me like you should never use this method, I'll explain more below. There are many ways to create an Assembly instance. Assembly.Load(assemblyName); Assembly.LoadFile(assemblyName); Assembly.LoadFrom(assemblyName); or even AppDomain.Load(assemblyName); There are some subtelties however, some consequences which may later cause issues e.g. the assembly you've loaded is locked. Assembly.LoadFile...