Emil’s Blog

Programming Windows, .Net, EPiServer and whatnot…

[Powered by WordPress.]

November 20, 2009

NUnit with SQLite and .Net 4.0 Beta 2

by @ 12:05. Filed under .Net programming, NHibernate, Tools

SQLite and unit testing is a great combination for testing database operations without having to manage database files. You can simply create an in-memory database in your setup code and work with that. Perfect in combination with NHibernate for example.

If you want to do this in the current .Net 4.0 beta your out of luck though, you'll get an exception:

System.IO.FileLoadException: Mixed mode assembly is built against version 'v2.0.50727' of the runtime
and cannot be loaded in the 4.0 runtime without additional configuration information.

The solution is pointed out by Jomo Fisher. What you do is to include this snippet in the application config file:

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0"/>
</startup>

When unit testing assemblies that references System.Data.SQLite.DLL then you have to put that snippet in NUnit's config file (C:\Program Files\NUnit 2.5.2\bin\net-2.0\nunit.exe.config).

If you combine this with the tip in my post NUnit with Visual Studio 2010 Beta 2, you should insert the following

<startup useLegacyV2RuntimeActivationPolicy="true">
  <supportedRuntime version="v4.0"/>
  <requiredRuntime version="v4.0.20506" />
</startup>

plus

<loadFromRemoteSources enabled="true" />

under the runtime tag.

This works for me, hopefully it will for you as well.

/Emil

November 19, 2009

log4net configuration Xml Schema

by @ 10:26. Filed under Tools

I just found a very useful XSD for editing log4net configurations:

http://csharptest.net/?p=38

Just copy the schema file to C:\Program Files\Microsoft Visual Studio *\Xml\Schemas and add the correct namespace to the log4net element in your config file:

<log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

/Emil

November 16, 2009

NUnit with Visual Studio 2010 Beta 2

by @ 13:35. Filed under .Net programming, Tools

Here's a tip of how to run NUnit tests in .Net 4 code:

Running NUnit 2.5 against Visual Studio 2010 .Net 4 code

Note that it's the NUnit.exe.config file in C:\Program Files\NUnit 2.5.2\bin\net-2.0 that should be updated.

November 13, 2009

Twittering…

by @ 23:04. Filed under Uncategorized

I'm now on Twitter...

http://twitter.com/emilast

Wonder if it's gonna be useful or a time-stealer...

/Emil

Distributed transactions with WCF and NHibernate

by @ 22:54. Filed under .Net programming, NHibernate, WCF

I recently started working on a new project in which we wanted to use WCF services that utilized NHibernate for database work. We also wanted those services to support distributed transactions so that several calls to one or more service would be done within the same client transaction. This is possible thanks to functionality in the System.Transactions namespace and in WCF which supports transaction flowing and of course the Distributed Transaction Coordinator in the operating system (see MSDN for more info on the DTC).

Note: The code below has been tested on NHibernate 2.1.1, Windows XP and .Net 4 Beta 2. Older versions of the .Net Framework should also work, but not necessarily older versions of NHibernate. I believe distributed transaction support was introduced in 2.1.0, but it may or may not work in similar ways to what is described here in older versions since the ADO.Net supports the System.Transactions namespace.

The goal is to write code like this on the WCF client side:

// TransactionScope is from the System.Transactions namespace
using (TransactionScope tx = new TransactionScope())
{
    service1.MyMethod();
    service2.MyMethod();
    tx.Complete();
}

If all goes well, the results of both service calls are comitted to the database. If the call to service2 fails and we get an exception so that tx.Complete() is never executed, then all database updates are rolled back are rolled back and nothing is persisted, even if service1 is hosted in another process or on another machine.

Note also that we're not limited to database updates, any resource that supports transactions and knows about System.Transactions will be able to roll back updates.

For the above to work, we have to do several things:

This is actually all that is required! NHibernate will now detect if there is a so called ambient transaction (to do this yourself, look at the System.Transactions.Transaction.Current static property, if it's non-null there there is a transaction) and will enlist its session in it. When the transaction completes, then the saved data will be comitted to the database. If there is an exception so that transaction is never completed then all data will be rolled back.

Important notes:

I think this is really cool stuff. Not only does it simplify transaction management in NHibernate, it also allows us to write much more robust distributed service-oriented application with very little effort. You also get support in the operating system, for example för statistics:

DTC statistics

I haven't tried with other databases than SQL Server but as NHibernate seems to support System.Transactions it is possible that it works with other DB systems as well. If you have any experience with that, please leave a comment :-)

I will continue to update this post if I do more findings on this subject. When I google about this there wasn't much information on this subject so hopefully this post will help others with the same needs.

/Emil

Data transforms using LINQ

by @ 19:55. Filed under .Net programming

Here's som simple code to transform an array or list from one type to another:

Kursdeltagare[] kdArr = ...

// Alternative 1
var kdDtos = matchingKd.Select(kd => new KursdeltagareDto()
                                    {
                                        PersonId = kd.PersonID,
                                        Fornamn = kd.Fornamn,
                                        Efternamn = kd.Efternamn
                                    }).ToList();

// Alternative 2
var kdDtos2 = (from kd in matchingKd
             select new KursdeltagareDto()
                        {
                            PersonId = kd.PersonID,
                            Fornamn = kd.Fornamn,
                            Efternamn = kd.Efternamn
                        }
            ).ToList();

Both alternatives will give the same result but the first one contains slightly less code. Which is better from a performance point of view? If you know, feel free to leave a comment...

/Emil

[powered by WordPress.]

jour·nal n. A personal record of occurrences, experiences, and reflections kept on a regular basis; a diary.

Internal links:

Categories:

Search blog:

Archives:

November 2009
M T W T F S S
« Oct   Jan »
 1
2345678
9101112131415
16171819202122
23242526272829
30  


View Emil Åström's profile on LinkedIn

General links:

I read:

Visitors

Recent Comments

Spam caught

Other:

Clicky Web Analytics

34 queries. 0.456 seconds