March 2005 Blog Posts

Excellent short MSDN TV segment explaining the Code Access Security (CAS) features in ASP.NET 1.1. This allows you to lock down the permissions for different ASP.NET applications using configured trust levels and is especially appropriate for shared server configurations.

The Avalon and Indigo Community Technology Preview - March 05 is now available to MSDN subscribers (Tools, Platform Tools, WinFX SDK).

"Avalon" is the code name for Microsoft’s unified presentation subsystem for Windows, consisting of a display engine and a managed-code framework. It unifies how Windows creates, displays, and manipulates documents, media, and user interface, and enables developers and designers to create visually-stunning, differentiated user experiences.

"Indigo" is the code name for Microsoft’s new Service-oriented communications infrastructure built on top of web services protocols. The advanced web service support in "Indigo" provides interoperable secure, reliable, and transacted messaging.

Congratulations to Brad on his move to Microsoft. Welcome!

The Community Technology Preview (CTP) of Microsoft SQL Server 2005 Express Edition is now available to download.

ASP.NET Web Services (ASMX) in .NET 2.0 introduces support for nullable types. In .NET 1.0 and 1.1, the framework didn't support xsi:nil for value types because, as we know, value types can't be set to null. This commonly caused interop problems, particularly with J2EE applications.

The following simple web service shows how a method with a nullable parameter.

<%@ WebService Language="C#" Class="SimpleService" %>

using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace="urn:simple")]
public class SimpleService {

    [WebMethod]
    public string TestNullable(int? value) {
        if(value.HasValue) {
            return "The number is " + value.ToString();
        } else {
            return "The number was not supplied.";
        }
    }
}

This produces the following WSDL fragment:

<s:element name="TestNullable">
  <s:complexType>
    <s:sequence>
      <s:element minOccurs="1" maxOccurs="1" name="value" nillable="true" type="s:int" /> 
    </s:sequence>
  </s:complexType>
</s:element>

The nillable attribute indicates that it is valid for the <value> element to be empty. In that case, the service will set value to null.

A common problem that developers run into with .NET v1.1 is the situation where you want a value type such as an int or bool but you want to allow for the value to also be undefined or null. For example, data coming from a database might not have a value define for one of the fields. The .NET Framework has had special SQL data types to deal with this issue but what if your problem domain isn't related to SQL?

In .NET 2.0, there is a new generic type System.Nullable<T> that allows you to represent a value that can also be null. The T stands for the specific type you want to use so your code might use System.Nullable<int> or System.Nullable<bool> or even System.Nullable<MyValueType>. C# has a special syntax that maps T? to System.Nullable<T> so you can add a question mark to your value types and they become "nullable". The following two lines are equivalent:

System.Nullable<bool> myNullableBool = false;
bool? myNullableBool = false;

As ever, C# tries to take the typing out of writing code.

The following code sample shows nullable types in use:

class App {
    static void Main() {
        int? i = 1;
        System.Console.WriteLine("i = {0} ({1})",i,i.HasValue);
        i = null;
        System.Console.WriteLine("i = {0} ({1})",i,i.HasValue);
    }
}

This generates the following output:

i = 1 (True)
i = (False)

Nullable types are only meaningful with value types. string? won't work because you can already set a string variable to be null.