Category Archives: .Net

Finding the path to the running assembly

There are several ways to get the path to the running assembly in c#. I used to do something like Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName); However when running unit tests etc this would cause pain and some old dirty solutions included injecting a hardcoded path for testing. I recently found a much better way to do this when googling [...]

Generating xsd schema from class

I always forget the syntax so here it is for reference Lets say we want to generate a schema for the class User in the assembly SomeCodez.dll C:\Somepath\SomeCoedz\bin\Debug>xsd.exe -t:User SomeCodez.dll /o:c:\temp\

NSubstitute and functions calls in setup

I am getting a behaviour I didnt expect from NSubstitute when setting up my mocks to call a function. A simplified version of this [Test] public void NSubstituteTest() { var mockedFoo = Substitute.For <IFoo>(); mockedFoo.GenerateString(Arg.Any<string>()).Returns(x => GetValue(x.Args()[0])); mockedFoo.GenerateString(“0″).Returns(“hi”); string result1 = mockedFoo.GenerateString(“0″); string result2 = mockedFoo.GenerateString(“1″); Assert.AreEqual(“hi”, result1); Assert.AreEqual(“1″, result2); } private string GetValue(object val) [...]

Uploading files with asp.net mvc–Request.Files is null

I have used asp.net mvc since beta and uploaded files without any problems until yesterday. I kept getting null from Request.Files (Actually I use HttpPostedFile as inparameter to the action). Nevertheless both Request.Files and the inparamer was null. In the form: File In the controller: [HttpPost] public ActionResult UploadFile(HttpPostedFileBase file) { if (file != null [...]

Defensive coding with code contracts

I find defensive coding a really great way to improve the quality of a codebase. Code contracts is an implementation of Design by contract (dbc). Dbc was introduced in 1986 by Bertrand Mayer for use with Eiffel. The concept is simple, check that the input to a method is valid and that the return values [...]

SpecFlow – getting up and running

SpecFlow is by far the BDD framework I like the best in the .NET ecosystem. It uses gherkin as natural language to describe features and scenarios. It has really good integration with visual studio. Under the hood it uses T4 templates to generate code that can be run using your favorite test runner like NUnit, [...]

Strapping up StructureMap with Caliburn

We are using Caliburn in our current project. Its a great framwork and cuts away much plumbing. You can use all the common DI containers for setting up dependencies. We use Structuremap in other parts of the application. Here is how to set it up for use with Caliburn: (code tags a bit wierd) using [...]

Valid letters in Alias for groups

We recently added support for automatically generating distrubution groups in ILM 2007 FP1 for a customer. Initially we were getting som errors because of bad Aliases. After some digging around I found exactly what letters are valid in an Alias. Valid leter are strings formed with characters from a to z (uppercase or lowercase), digits [...]

Windows services – a nice way to develop, debug and install

A nice way to develop sevices is to start your project with an ordinary console application. Within the console application you can swith input arguments. If there are no input arguments you can start the service and if there is for example “/test” you could do some processing or debug into some code. Some example [...]

Two nice tools for getting started with mocking and DI

Mocking and dependency injection can seem overwhelming the first time you read about and try to implement them. Two nice and clean frameworks are moq and ninject They are very easy to get into and both has good basic tutorials. For a beginner they cover what you want to do. I will try to show [...]