Category Archives: .NET

Windows Identity Foundation released

Windows Identity Foundation, formerly known as “Geneva”, has shipped. I’ve been talking about Geneva/WIF on several occasions and I absolutely love it. It opens the door for a whole new realm of authentication/authorization scenario’s. SharePoint 2010 will be the first Microsoft Product to support it, apart from the new Active Directory Federation Services 2.0, which was part of the development effort and was formerly known as “Geneva” Server. Be sure to check it out!

DataContractSerializer and interfaces

I was working on this little app I wrote a while ago and wanted to add some features requiring (de)serialization. So, I took the original class and made it a DataContract so I could use it with the DataContractSerializer. The class then looked more or less like this:

[DataContract]
public class MyClass
{
List<Trip> m_Items = new List<Item>();

[DataMember]
public IList<Item> Items
{
get { return m_Items; }
}
}


Serialization went fine, but when I tried to deserialize the same object, I got a null reference exception. Of course you say, you should have added a method tied to de OnDeserializing event, because the constructor of the object doesn’t work and hence the m_Items field is never initialized. The code I added to solve this looked like this:

[OnDeserializing]
protected void Init(StreamingContext context)
{
m_Items = new List<Item>();
}

To my surprise I still got the same exception. I finally figured out that the problem was the type of Items. It is was an IList<> instead of a List<>. To avoid tying a class to a specific implementation of a list, I usually use an interface, which is good practice in most cases… however, not when you want to do deserialization :).

#if vs. ConditionalAttribute

At BataviaLabs we were debating coding guidelines the other day and came across this one: do you use #if or the ConditionalAttribute to indicate to the compiler if a method should be compiled. Let me elaborate…


If you have a method you only want to compile in a debug scenario, you have the following options:


1) Use #if DEBUG as shown below

class Program
{
static void Main(string[] args)
{
#if DEBUG
SomeMethod();
#endif
Console.WriteLine(“End”);
Console.ReadKey();
}

#if DEBUG
internal static void SomeMethod()
{
Console.WriteLine(“SomeMethod”);
}
#endif
}


2) Use the ConditionalAttribute as shown below

class Program
{
static void Main(string[] args)
{
SomeMethod();
Console.WriteLine(“End”);
Console.ReadKey();
}

[Conditional(“DEBUG”)]
internal static void SomeMethod()
{
Console.WriteLine(“SomeMethod”);
}
}


The difference between these two methods is enormous. The first sample is very explicit. Any code you don’t want to compile into the production build is placed between #if DEBUG and #endif. If you try to call SomeMethod in a production build, the compiler will give you a compile error. The ConditionalAttributeon the other hand doesn’t require you to remove the calls to SomeMethod. If a method is marked [Conditional], any calls made to that method are removed from the build by the compiler. A proviso here is that [Conditional] only works with methods that don’t return a value (i.e. void).


I much more prefer #if DEBUG, because it is explicit. I can’t run into a situation where from reading the code I’m thinking “SomeMethod is being executed”, but it actually isn’t because the compiler removed the call. Comments anyone?

VSLive! 2009 San Francisco slides and samples

Thank you for all who attended my sessions in San Francisco. Below are the slides and samples for my sessions.


VTH4 – Understanding Transactions in WCF Slides (561.73 KB) | Samples (540.1 KB)


VTH16 – Supporting POX/REST with WCF Slides (369.58 KB) | Samples (302.82 KB)


VTH25 – Simplify WebPart (and Control) Development with WebPart Skinning Slides (359.85 KB) | Samples (447.66 KB)


The VTH25 samples include the full installer. However, be sure to change the uploadskinfeature.bat to point to the correct server. You can read more about the VirtualPathProvider I mentioned in VTH25 session here.

Speaking at VSLive San Francisco, February 23-27

The wonderful folks at VSLive! have invited me again to do three sessions at their San Francisco event from February 23 to February 27, 2009. These are the sessions I’ll do (all on Thursday 26):



  • Understanding Transactions in WCF

  • Supporting POX/REST with WCF

  • Simplify WebPart (and Control) Development with WepPart Skinning

The first two are obviously about WCF. The last talk is primarily about SharePoint, but the discussed techniques will work with ASP.NET WebParts and WebControls as well.


I really like VSLive! because they have some great content and top tier speakers. The speakers (myself included of course) are also very accessible, because the event is not as huge as some of the other conferences these days. So if you intend to go to a conference this year, VSLive! is going to be worth your money.

Speaking at VSLive in Dallas 8-11 December

I will be speaking at VSLive! again, this time in Dallas from 8 until 11 December. I’ll be doing the following sessions:



  • Introduction to ASP.NET Dynamic Data (Tuesday 9 December, 11:15 AM)

  • POX/REST Strategies with WCF (Thursday 11 December, 1:45 PM)

  • Understanding Transactions in WCF (Thursday 11 December, 4:45 PM)

VSLive! is a very nice and relaxed event with great content. So it’s a great opportunity to learn new stuff and new people. If you want to attend the Dallas show and see me speak, you can get a $300 discount by entering the discount code SPVAN. I hope to see you there.

LINQ to SQL performance

LINQ to SQL ontwikkelt heerlijk als je weet dat je alleen tegen SQL Server hoeft te praten. Maar LINQ to SQL voegt wel weer een extra laagje toe, waardoor de kans groot is dat er vertraging optreedt. Hoewel… LINQ to SQL doet ook aan caching, dus als je veel met dezelfde data moet werken, hoef je niets extra’s to doen om code aanzienlijk sneller te laten lopen dan wanneer de database iedere keer benaderd moet worden. Rico Mariani geeft in vijf delen een aardig inzicht in de performance van LINQ to SQL en wat je kunt doen (of niet hoeft te doen) om de performance te verbeteren. Het eerste deel, met links naar de andere delen, vind je hier.

Good books on SharePoint development are rare (or I haven’t found them yet)

I’ve now read several books on SharePoint development, such as Developer’s Guide to the Windows SharePoint Services v3 Platform , Microsoft harePoint 2007 Development Unleashed, and Programming Excel Services. I’ve found each of these books lacking, particularly the last two. The Unleashed book is more a reference than anything else and especially in the more advanced topics I really didn’t find what I was looking for, which is not what you’d expect from an Unleashed title. The Excel Services book is a disappointment if you are looking for good information about Excel Services. If you want to learn to develop WebParts it is quite good however. In that sense the book has been completely misnamed. I hope that my next pick will be better (that said, two of the three books were given to me).

Finding patterns for the log4net conversionPattern setting

Log4net is a great tool for logging in a .NET application and everything is well documented. Finding what you need can be a pain though. If you checkout the configuration manual, you read about all the appenders and a little about the pattern of the message that is logged. You can set that pattern if you want. What they fail to provide however is a reference of the tokens you can use in a pattern (for instance %exception). They only show a sort of default pattern. After digging for about 10 minutes, I finally found the list within the SDK reference. It would be great it someone were to add I direct link to there from the configuration manual.

Exception handling in LINQ to SQL

I’ve been doing stuff with LINQ to SQL for a while now and there’s one thing that keeps bugging me: Proper exception handling. IMHO LINQ to SQL should encapsulate the database. I mean, I don’t care which database (SQL Server, Oracle, whatever) I’m working with, but if I call SubmitChanges on the DataContext, I’ll get a SqlException if it fails on the database. I know that currently LINQ to SQL only supports SQL Server, but the model is just as valid for any other (relational)database. To my surprise there is no LinqException or something that encapsulates the SqlException, so now I have to write code that is aware of the fact that I’m dealing with SQL. You can of course solve this by using the Exception Handling Application Block, so you don’t deal with specific exceptions in code, but it still feels funky.