Friday, June 25, 2010

BizTalk & SQL Server 2008 R2 not compatible

Apparently SQL Server 2008 R2 is not verry nice for BizTalk 2009. From the moment that you run the configuration for BizTalk, it gets stuck at the configure "Group" step.

The log file shows an unmeaningfull error:
Message: Failed to create Management database "BizTalkMgmtDb" on server "...".
Errorcode: 0x80040E1D

After googling a bit I found the following discussion (link) which gives a meaning to the error.

Conclusion: wait until BizTalk 2010 or revert your SQL Server to SQL 2008 or earlier.

Tuesday, June 3, 2008

Adding references - WebSite versus WebApplication

On my current project I’m creating an ASP.NET site in Visual Studio 2005 SP1.

NOTE: By default, Visual Studio 2005 only allows you to create ASP.NET sites by using the WebSite template but after installing SP1, you can also use the WebApplication template.

For this project we are using the WebSite template since this is the customer’s standard. While developing the application, I noticed that when using the WebSite template, the Add Reference works differently then when using the WebApplication template.

When adding a reference to dll A using the WebSite template, Visual Studio automatically adds a reference to all dll’s that dll A references. This is done to ease the deployment but it can give some unwanted side effects.

To make this clearer, I’ve created a simple solution structure that shows this issue. In the image on the left, you can see 4 projects:


  • DalLib

  • BllLib: contains a link to DalLib

  • WebSite: project using the WebSite template; contains a link to BllLib

  • WebApplication: project using the WebApplication template; contains a link to BllLib

When you compile this solution, you will notice that the Visual Studio will add the DalLib to the bin folder of the WebSite project. Because of this, you gain access to all the public classes within DalLib from the WebSite. Since I want to avoid direct access from UI to Dal, this is not what I want. When using the WebApplication template, you don’t have this problem.

A possible solution is to make the classes within the DalLib internal and set an InternalsVisible attribute in the AssemblyInfo.cs file of the DalLib (thanks Gert for the tip).

InternalsVisible is a new attribute from.NET 2.0 and is defined in the System.Runtime.CompilerServices namespace.

Wednesday, March 26, 2008

Vertical split view in Visual Studio 2008

Just noticed that in Visual Studio 2008 it is possible to change the split screen behavior.

Visual Studio allows you to set the default view (Split, Source or Design) and to choose between a horizontal split and a vertical split. Especially this last option is very handy when you have a widescreen monitor.


To change the settings, go to Tools\Options\Html Designer



Friday, February 8, 2008

Quiz 1: Generic class with static fields

In the following piece of code you can see 2 instantiations of a generic class. What is the output of this console application?

using System;
using System.Collections.Generic;


namespace DEMO
{
class Program
{
static void Main(string[] args)
{
GenClass
<int> x = new GenClass<int>();
GenClass
<double> y = new GenClass<double>();

x.Add(
1);
y.Add(
2);

Console.WriteLine(y.Get(
0));
}

public class GenClass<T>
{
public static List<T> list = new List<T>();

public void Add(T x)
{
list.Add(x);
}

public string Get(int index)
{
return list[index].ToString();
}
}
}
}


I'll post the response in a comment.

Sunday, January 27, 2008

Anonymous types and equality

Introduction to anonymous types

Most of the applications created in .NET contain classes defined by the developer. There are two types of classes. First you have the type that only contains fields and is used to transfer data, then you have a second type that also provides functionality to the application by implementing logic in methods, events, ... .

As from the .NET 3.5 framework, Microsoft has added anonymous types. Anonymous types that are created dynamically at compile time.

With anonymous types, the developer no longer has to define the type structure. Sounds great doesn't it. Although there are some limitations. For example, anonymous types can only be used as local variables. This means that the definition of a type is only valid within a single method. It is possible to pass instances of an anonymous type between methods but then the parameter of the receiving method should be of type object which is mostly not a best practice and difficult to work with.

This limits the use of anonymous types. As for most of the .NET 3.5 language enhancements, anonymous types are implemented to be able to support Linq.

To create an anonymous type, you need to use the var keyword in combination with the new object initialization syntax (also one of the .NET 3.5 language enhancements).



Sample

In the following sample, I'll show you how to create an anonymous type as well as how an anonymous type behaves when using equality methods.


using System;

class Program
{
static void Main(string[] args)
{
// Creation of an anonymous type
var p1 = new
{
FirstName
= "Geert",
LastName
= "Verhoeven",
Age
= 26
};

// Error:
// Property or indexer 'AnonymousType#1.Age'
// cannot be assigned to -- it is read only
// p1.Age = 27;

Console.WriteLine(p1.ToString());

var p2
= new
{
FirstName
= "Geert",
LastName
= "Verhoeven",
Age
= 26
};

var p3
= p1;

// Comparison based on the == method.
Console.WriteLine("person1 == person2: {0}",
p1
== p2);

// Comparison based on the equals method.
Console.WriteLine("person1.Equals(person2): {0}",
p1.Equals(p2));

// Comparison based on the == method.
Console.WriteLine("person1 == person3: {0}",
p1
== p3);
}
}

Output:


There are some important things to note when looking at this output.

At first, if you look at the result of the ToString() method, you can see that the behavior is different then with normal types. By default, when not overridden, the ToString() method returns the  types fullname. This means that an anonymous type has an override for the ToString() method.

Secondly, the Equals method behaves differently as well. When not overridden, the Equals method checks whether 2 objects refer to the same memory object. This means that the anonymous type for the Equals method as well.

The == operator still acts the same as with normal objects. From these results we can conclude that when using the Equals method, an anonymous types acts like a value type. When using the == operator, the anonymous type acts like a reference type.

Now how is this possible since we didn't write any code for these overrides? When opening the assembly with a tool like Reflector, we can see the structure of the generated anonymous type and see that the compiler did this for us.



internal sealed class <>f__AnonymousType0<<FirstName>j__TPar, <LastName>j__TPar, <Age>j__TPar>
{
private readonly <Age>j__TPar <Age>i__Field;
private readonly <FirstName>j__TPar <FirstName>i__Field;
private readonly <LastName>j__TPar <LastName>i__Field;

public <>f__AnonymousType0(<FirstName>j__TPar FirstName, <LastName>j__TPar LastName, <Age>j__TPar Age);
public override bool Equals(object value);
public override int GetHashCode();
public override string ToString();

public <Age>j__TPar Age { get; }
public <FirstName>j__TPar FirstName { get; }
public <LastName>j__TPar LastName { get; }
}


Thursday, January 17, 2008

Strange behavior with extension methods

In a previous post, I already explained what extension methods are and how they work. While doing some tests, I noticed a strange behavior which I will explain in this post.

The order in which .NET searches for the method to be executed is as follow:

  1. Instance methods of the class
  2. Static methods within the class
  3. Extension methods specified for the class

Because the static methods have a higher priority then the extension methods, this can give strange behavior.

In the following sample, the goal is to create an extension method that enables making comparisons on DateTime objects on different levels. The comparison level allows you for example to only compare until the month and not use the day/time while doing the comparison.

using System;
using System.Collections.Generic;


class Program
{
static void Main(string[] args)
{
DateTime d1
= new DateTime(2008, 1, 1);
DateTime d2
= new DateTime(2008, 1, 10);

// Compile time error:
// Member 'object.Equals(object, object)'
// cannot be accessed with an instance
// reference; qualify it with a type
// name instead
d1.Equals(d2, DateComparisonLevel.Month);
}
}

public enum DateComparisonLevel
{
Year,
Month
}

public static class MyUtils
{
public static bool Equals(this DateTime orgDate,
DateTime compDate, DateComparisonLevel level)
{
switch (level)
{
case DateComparisonLevel.Year:
return orgDate.Date.Equals(
new DateTime(
compDate.Year,
orgDate.Month,
orgDate.Day));

case DateComparisonLevel.Month:
return orgDate.Date.Equals(
new DateTime(
compDate.Year,
compDate.Month,
orgDate.Day));
}

return false;
}
}

When trying to compile this code, you will receive a compile time error on the following line:

d1.Equals(d2, DateComparisonLevel.Month);

Error:

Member 'object.Equals(object, object)' cannot be accessed with an instance reference; qualify it with a type name instead.

This is because DateTime derives from System.Object. Since System.Object contains a method with definition

public static bool Equals(object objA, object objB);

the compiler takes this method in case of the extension method. Since the method of the System.Object class is a static method, it can not be called from an instance of an object. The only way to solve this is to rename the Equals method in the code. This way, the Object.Equals method will no longer interfer.

Microsoft considers this as a bug.

Introduction to extension methods

What it is

Extension methods allow you to extend an existing type without needing access to the type's code. Before .NET 3.5, this wasn't possible without having access to the type's code or without using the System.Reflection.Emit namespace.

In short, extension methods are static methods that can be executed by instances of the extended object. Behind the scenes, the call to the extension method is nothing more then a call to a static method.

Behind the scenes, extension methods are marked with the System.Runtime.CompilerServices.ExtensionAttribute which is located in the Systme.Core.dll. So a reference to this DLL is necessary in order to create extension methods.

When to use

If you have a class that you want to extend but you don't have the source code, extension methods can be an answer. Also when extending a class with extension methods, you don't have the risk of breaking existing code which you can have when changing the type's definition.

Simple sample

In this sample, I'm adding a method IsWeekend to the DateTime type which will return a boolean to indicate whether a given date falls in the weekend or not.


using System;


class Program
{
static void Main(string[] args)
{
// Tuesday
DateTime d1 = new DateTime(2008, 1, 1);

// Saturday
DateTime d2 = new DateTime(2008, 1, 5);

Console.WriteLine(
"{0:d} is weekend: {1}",
d1, d1.IsWeekend());
Console.WriteLine(
"{0:d} is weekend: {1}",
d2, d2.IsWeekend());

}
}

public static class MyUtils
{
public static bool IsWeekend(this DateTime date)
{
return (date.DayOfWeek == DayOfWeek.Saturday
|| date.DayOfWeek == DayOfWeek.Sunday);
}
}

Extension methods must be created within a static class (each method within the class must be static as well). The first parameter of the method is marked with the this keyword and indicates the type to which the method should be applied (in this case DateTime). It is possible to add multiple parameters. Only the parameters after the first parameter, should be provided when using the method.

When you've implemented an extension method for a specific type, you will see that it is showed by intellisense with a specific icon.



It is possible to create a separate assembly with all your extension methods. Note that you need to add using statements for the namespaces where the extension methods are located.