Getting the NServiceBus 2.0 to work with .NET Framework 4 can be little problematic. Here’s a short tutorial on how to get the server side up and running.

1. Downloading and compiling the source

First, get the latest NServiceBus source code from the github. Patch was added to the source in April 13, 2010, which allows NServiceBus to work with the latest framework. After downloading the source, use the build-ne4.bat to compile the project.

2. Creating the project

Now start up your Visual Studio 2010 and create a new Class Library project. Add the references to these files (use the dlls you compiled in the first phase):

  • log4net.dll
  • NServiceBus.Core.dll
  • NServiceBus.dll
  • System.Data.SQLite.dll (make sure to use the one from x64 folder if you have x64 Windows)

Now open up the automatically generated Class1.cs and decorate it with two different NServiceBus interfaces:

<span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">using</span> NServiceBus;

<span style="color: #606060" id="lnum2">   2:</span>  

<span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">namespace</span> ClassLibrary1

<span style="color: #606060" id="lnum4">   4:</span> {

<span style="color: #606060" id="lnum5">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Class1 : IConfigureThisEndpoint, AsA_Server

<span style="color: #606060" id="lnum6">   6:</span>     {

<span style="color: #606060" id="lnum7">   7:</span>  

<span style="color: #606060" id="lnum8">   8:</span>     }

<span style="color: #606060" id="lnum9">   9:</span> }

Build your solution at this point. Then open the project’s properties and make the project start an external program NServiceBus.Host.exe when debugging. Make sure you point to the exe file in your bindebug-folder:


3. Adding the configuration file

We need a configuration file to get things working. Add app.config to the project and fill in the NServiceBus-related sections. Your configuration should look like this:

&lt;?xml version=<span style="color: #006080">&quot;1.0&quot;</span> encoding=<span style="color: #006080">&quot;utf-8&quot;</span> ?&gt;




    &lt;section name=<span style="color: #006080">&quot;MsmqTransportConfig&quot;</span> type=<span style="color: #006080">&quot;NServiceBus.Config.MsmqTransportConfig, NServiceBus.Core&quot;</span> /&gt;

    &lt;section name=<span style="color: #006080">&quot;UnicastBusConfig&quot;</span> type=<span style="color: #006080">&quot;NServiceBus.Config.UnicastBusConfig, NServiceBus.Core&quot;</span> /&gt;




  InputQueue=<span style="color: #006080">&quot;InputQueue&quot;</span>

  ErrorQueue=<span style="color: #006080">&quot;error&quot;</span>

  NumberOfWorkerThreads=<span style="color: #006080">&quot;1&quot;</span>

  MaxRetries=<span style="color: #006080">&quot;5&quot;</span>




    DistributorControlAddress=<span style="color: #006080">&quot;&quot;</span>

    DistributorDataAddress=<span style="color: #006080">&quot;&quot;</span>&gt;






You’re ready to start your server! Hit F5 and you should see a console window to pop out.  Too bad that the program will crash almost immediately with the following exception:

Exception when starting endpoint, error has been logged. Reason: Could not load file or assembly 'file:///C:devspikesClassLibrary1ClassLibrary1binDebuglog4net.dll' or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0x80131515)

If we would have used the .NET Framework 3.5, everything would have gone nice and smooth.

4. Adding the .NET Framework 4.0 compatibility sugar

So, what went wrong? If you use the command line and start your server from there, you will get more detailed descriptions. Here’s an interesting part from the exception:

System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does  not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

This is one of the small things which have changed between the .NET Framework 3.5 and 4.0. If you follow the link provided by the exception, you will find a detailed description on what has changed between the frameworks. As you can see from the exception, the new framework is treating the log4net.dll as it was run from a network location. This is because the file “is flagged by Windows as being a Web application, even if it resides on the local computer.”

To fix the problem, add a new configuration file to your program and name it “NServiceBus.Host.exe.config”. Before editing the file, make sure to tick the “Copy to output directory” property of the file to “Copy always”. We’re going to use the information acquired from the exception details to add the correct configuration:

&lt;?xml version=<span style="color: #006080">&quot;1.0&quot;</span> encoding=<span style="color: #006080">&quot;utf-8&quot;</span> ?&gt;



    &lt;loadFromRemoteSources enabled=<span style="color: #006080">&quot;true&quot;</span>/&gt;



And that’s it! After compiling your project you should have two configuration files in the  debug-directory:

  • ClassLibrary1.dll.config
  • NServiceBus.Host.exe.config

Now just run the exe or press F5 in Visual Studio and you should see the familiar NServiceBus debug output.


Comment by bloggertemplatesblog


NServiceBus and .NET Framework 4 - Mikael Koskinen...

Comment by hostgator discount

hostgator discount...

NServiceBus and .NET Framework 4 - Mikael Koskinen...

Comment by download wordpress themes

download wordpress themes...

NServiceBus and .NET Framework 4 - Mikael Koskinen...