Solutions to some problems hosting Windows Workflow Foundation applications in AppFabric

I'm trying to stand up some Windows Workflow Foundation applications in AppFabric for a spike on an application I'm building and so far it has been... unpleasant. Here are a couple of solutions to some frustrating problems I encountered. Hopefully I can help save someone else a bit of time.

Workflow persistence is not functional because the net.pipe binding is not enabled for this web site

Encountered while trying to configure my WF service on an IIS virtual directory. In particular I encountered this error on the Workflow Persistence tab and a similar one onthe Workflow Host Management tab in the Configure WCF and WF for Application dialog.

Enable the net.pipe protocolNow, in theory this will be automatically fixed when you click Apply. And in theory, even if it's not then it should be resolved by taking the following steps:

  • Right click the web site that hosts your service in the IIS Connections pane, click Manage Websites -> Advanced Settings and add ,net.pipe to the end of the "Enabled Protocols" setting (no spaces!)
  • With the same site selected, click Edit Bindings on the Actions pane and add a net.pipe binding with Binding Information = *

And in theory, practice and theory are the same. But in practice, unfortunately, they are not. If you're not running Windows Server (or your install is a bit wonky) then there's an extra step to take, because you'll find that net.pipe is not an available binding type in the bindings dialog.

To resolve: Open the "Turn Windows features on or off" dialog (find it by searching in the Start menu/screen, under Settings). Open up the Microsoft .NET 3.5 folder and tick "Windows Communication Foundation Non-HTTP Activation". This should make the net.pipe binding available to you. You may need to run iireset in a console as admin first.


Error: Unable to start debugging on the Web Server

or, if you try running without debugging

Error: Cannot obtain Metadata from xamlx The remote server returned an unexpected response: (405) Method Not Allowed.

or, if you drill further into that error

HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map

The honest truth is I'm not sure what caused this error. I tried a whole bunch of stuff. I added some handlers to my root Web.config, installed some Workflow hosting modules in IIS, played with permissions, all kinds of stuff and I couldn't get the site to handle the .xamlx request. Ultimately here's what resolved the issue for me. The first step may not be necessary, I honestly don't know (I recommend reading the linked article). But it worked.

  • Run the following command: "%WINDIR%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\ServiceModelReg.exe" -r
  • Uninstall and reinstall the following Windows features from the "Turn Windows features on or off" dialog: Windows Communication Foundation HTTP Activation, Windows Communication Non-HTTP Activation. They are both under the .NET Framework 3.5 feature.

applicationHost.config : Unrecognised attribute: 'serviceAutoStartMode'

After I got my workflow up and running I realised that every other service and website on my machine was broken. It seems that AppFabric stepped all over my applicationHost.config file and left an invalid attribute in there. Take a look at this thread for the solutions to this issue.

Building a scriptcs script pack

If you haven't seen scriptcs yet you should check it out. It's a scripting environment for C#, but more interestingly it incorporates a module system similar to what you'd see in node.js using NuGet as a packaging mechanism. I won't go into an intro to scriptcs, because others like Scott Hanselman and the man himself, Glenn Block (and contributors) have done a better job that I could have.

Instead I'll talk about a concept that (so far) hasn't had a lot of documentation love - script packs.

What is a scriptcs script pack?

The basic idea is that a script pack is an object (generally compiled in a DLL and available in your script's bin directory) that either exposes some functionality to the scripting environment or simplifies the usage of some other functionality. This seems like an overly broad definition so let's start with an example of script pack usage. From the scriptcs.WebApi script pack.

public class TestController : ApiController {
  public string Get() {
    return "Hello world!";
  }
}
var webApi = Require();
var server = webApi.CreateServer("http://localhost:8080");
server.OpenAsync().Wait();
Console.WriteLine("Listening...");
Console.ReadKey();
server.CloseAsync().Wait();

That's the complete script - no using statements and ridiculously simple API. The script pack takes care of "injecting" using statements into any script that uses the script pack (ie, calls that Require method above - we'll get into that) and returning a script-friendly object to the shell.

Let's get started building a script pack. I want to create a script pack that allows me to pull down arbitrary strings from an HTTP request - let's keep it really nice and simple.

Fire up Visual Studio (note: you don't actually need Visual Studio for this, but I'll assume if you're comfortable enough to use other tools then you'll know when to adjust the steps) and create a new Class Library project. Delete that damn Class1.cs file, open the Package Manager console and execute the command

Install-Package scriptcs.contracts

The contracts library has a couple of interfaces we'll need to implement to make our script pack work. Let's start by creating our "convenience object". We'll call it Http, and all it's going to do is make GET requests and return strings. So let's implement this class.

using System.Net;
using ScriptCs.Contracts;
namespace ScriptCs.Http
{
    public class Http : IScriptPackContext
    {
        public string Get(string uri)
        {
            var client = new WebClient();
            return client.DownloadString(uri);
        }
    }
}

That IScriptPackContext interface there is a marker interface that scriptcs seems to use to identify objects that are capable of being returned by script packs. It has no members, so there's nothing to implement.

The next step is to implement the script pack itself. This object will be picked up by the scriptcs engine and is capable of generating and returning IScriptPackContext objects. Because our script pack is so simple we don't need to do much in the implementation of this interface:

using ScriptCs.Contracts;
namespace ScriptCs.Http
{
    public class HttpScriptPack : IScriptPack
    {
        IScriptPackContext IScriptPack.GetContext()
        {
            return new Http();
        }
        void IScriptPack.Initialize(IScriptPackSession session)
        {}
        void IScriptPack.Terminate()
        {}
    }
}

This is fairly self-explanatory. The scriptcs engine will cache all available IScriptPack objects and when a particular script pack is requested (see below) then the appropriate script pack context will be returned.

The Initialize method here is interesting. We're not doing anything with it in this particular program, but it's worth going over what can be done. This method gives us an IScriptPackSession object which has two important methods:

  • AddReference - use this method to add library references to be available in your script. After your script pack is loaded into a script the specified references will be available for your use with no further code inside the script.
  • ImportNamespace - this method can import namespaces for use in your scripts. It's just a nice convenience so that your user's scripts can stay nice and clean. We don't need to do this in our current script pack, but if you refer back to the WebApi script above you'll notice we're using the ApiController class without any using statements. This is because the WebApi script pack imports this namespace for you.

Obviously the Terminate method should be used for cleaning up any resources after yourself and leaving the machine in a good state.

So that's pretty much all there is to building a script pack. Now we'll go ahead and use it. Build your library, grab the DLLs from your bin directory and move it into a test folder like c:\myscripts\bin. Create a new script that looks like

var http = Require();
Console.WriteLine(http.Get("http://google.com"));

Save it into your scripts folder (c:\myscripts\myscript.csx in my above example) and execute it using

scriptcs myscript.csx

And observe the output. All done! Now all that's left is to package your script up in a NuGet package and publish.

.NET Daily Quiz Archive - Week 9

.NET Daily Quiz #041

Can you predict the output of this program? Do you know the rules determining the order in which these constructors are called?

using System;
public class MyApplication
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Foo.i);
        Console.WriteLine(Foo.i);
        new Foo();
        new Foo();
        new Foo();
        Console.WriteLine(Foo.i.ToString());
        Console.WriteLine(Foo.i.ToString());
    }
}
class Foo
{
    public static int i = 0;
    static Foo()
    {
        i++;
    }
    public Foo()
    {
        i++;
    }
}

Answer

The output is

1
1
3
2

Here's what's happening. When

Console.WriteLine(Foo.i);
is called, the constructed type Foo is realized. This constructed type contains a static variable i=0. Now according to the C# spec (section 10.12):

The execution of a static constructor is triggered by the first of the following events to occur within an application domain (emphasis mine):

  • An instance of the class type is created.
  • Any of the static members of the class type are referenced.

Next we call

Console.WriteLine(Foo.i);
and according to the C# spec (same section) a new static constructor execution is called because we have a new closed type:

A static constructor is a member that implements the actions required to initialize a closed class type.

We now have two closed types, each with their own static members. From here the execution is trivial to predict - each closed type acts on its own static members.


.NET Daily Quiz #042

See comments in code for question…

using System;
public class Dynamics
{
    public static void Main(string[] args)
    {
        // Why doesn't this work...
        IGeneric c = new Generic();
        // ... but this does?
        string s = new Generic().Thing();
    }
}
interface IGeneric<out T>
{
    T Thing();
}
class Generic : IGeneric
{
    public T Thing()
    {
        return default(T);
    }
}

Answer

The dynamic type in C# is essentially compiler smoke and mirrors. When you compile this:

// Why doesn't this work...
IGeneric c = new Generic();

All the CLR sees is:

IGeneric c = new Generic();

Covariance and contravariance on object is not allowed (because there is no implicit conversion from object to anything else), so the above code fails. There is an implicit conversion from string to object, so swapping the type arguments works:

// The following works - hooray?
IGeneric c = new Generic();

The next piece of code has a lot more going on:

// ... but this does?
string s = new Generic().Thing();

When compiled, the CLR sees something like this:

IGeneric c = MagicalDynamicRuntimeTypeCast(new Generic().Thing());

The upshot is that dynamic is not a feature of the CLR — it’s a fancy trick by the C# compiler to generate the appropriate runtime code (which itself will instantiate the compiler). The CLR has no knowledge that a dynamic object is being used.

This means that as far as the CLR knows, the runtime type of dynamic is just object (or as Eric Lippert says, ‘"dynamic" as a type is nothing more than "object" with a funny hat on’). So any covariance/contravariance operations with a dynamic constructed typed behave exactly as if you replaced dynamic with object.


.NET Daily Quiz #043

This one might be a bit of a brain-bender. Think carefully.

Is the call to method M bound statically or dynamically (ie, is the call site for this method call determined at compile time, or is it dynamically dispatched)? Can you describe the line of reasoning that leads to your conclusion?

using System;
using System.Collections.Generic;
public class C1
{
    public static void Main(string[] args)
    {
        IEnumerable strings = null;
        IEnumerable dynamics = null;
        var result = C1.M(strings, dynamics);
        result.NoCompilerErrorHere();
    }
    public static T M(IEnumerable a1, IEnumerable a2)
    {
        return default(T);
    }
}

Hint: This program compiles. This information is relevant.

Answer

The correct answer is a static dispatch, although the reasoning is quite subtle (if you want to answer without diving into reflector or ildasm.exe). Most of my reference comes from Chris Burrows’ excellent blog. Burrows is the guy who designed much of and implemented almost all of dynamic in C#.

First you need to understand the rules behind two features: overload resolution with dynamic and assignment conversions.

Overload resolution — any method call with a dynamic argument is dispatched dynamically. This is simple.

Assignment conversions — assignment conversions are a new type of conversion introduced into C# to support dynamic. Just as implicit conversions are a subset of explicit conversions, assignment conversions are a superset of implicit conversions and a subset of explicit conversions. So the conversion hierarchy says that all implicit conversions are assignment conversions and all assignment conversions are explicit conversions. See the image to the right.

conversions

Why this was done is out of the scope of this quiz — see this article.

The main point here is the set of rules for dynamic conversions (emphasis mine):

  1. There is an implicit conversion to dynamic from every type (modulo the weirdos, like pointer types). Basically, if it has an implicit conversion to object, it has an implicit conversion to dynamic too.
  2. There are no implicit conversions from dynamic to any type aside from itself and object.
  3. However (this is the clever part), there are implicit conversions from any dynamic expression (not type!) to any type.
  4. There are implicit conversions between types (in both directions) when they differ only by object and dynamic (as in the exception in rule 2).

Now to answer the quiz question. Our first argument is IEnumerable. Now string is a candidate for type T. The second argument is of type IEnumerable, so dynamic is a candidate for T. The compiler does not throw an exception so the call must be unambiguous, which proves that there must be a conversion from one type to the other in the set {string,dynamic}, but not the other way.

From rule 1 above we know there must be an implicit conversion from type string to type dynamic. From rule 3 we know that the type dynamic is NOT implicitly convertible to type string. Because we’re dealing with types and not expressions then the call must be static — if we used expressions (eg, if we used naked dynamic and string objects) then this whole analysis would be deferred until run time — the static type analysis would never be performed because of the overload resolution rule given above.


.NET Daily Quiz #044

Continuing on with our exploration of dynamic. What is the result of this program, and why?

using System;
using System.Collections;
using System.Dynamic;
public class DynamicConverter : DynamicObject
{
    public override bool TryConvert(ConvertBinder binder, out object result)
    {
        Console.WriteLine("Converting to type {0}", binder.Type.Name);
        result = null;
        return true;
    }
    public static void Main(string[] args)
    {
        dynamic d = new DynamicConverter();
        IEnumerable i1 = d;
        IEnumerable i2 = (IEnumerable)d;
    }
}

Answer

The application prints the first conversion but crashes attempting the second. But why?

This one is slightly tricky, even if you know dynamic well. It’s important to note that DynamicObject is just a convenience object that hides a lot of the complexity of creating dynamic types (the more difficult but more flexible way is to implement IDynamicMetaObjectProvider, which is not trivial).

As a result, DynamicObject has a weird and interesting way of defining call sites for what appear to be dynamic methods. If you call a method that exists statically on a DynamicObject then the static call site will be used — basically, if the underlying language (whether it be C# or something else) gets first dibs on resolving any method calls, and this includes conversions.

So what’s happening in my example code? When we call IEnumerable i1 = d; the C# compiler says, “do I have an implicit conversion from variable d of type DynamicConverter to IEnumerable?” The answer is no, so the compiler dispatches the call dynamically. No problem. Next, we call IEnumerable i2 = (IEnumerable)d; and the compiler asks itself if it can perform this conversion. The answer is “yes!” because in C# there is always an explicit conversion from any class type to any interface type. Because C# can handle this conversion, it is bound statically and fails at run time.

This is an important point to keep in mind if you’re using DynamicObject. If you try to do something like define and call a method named ToString then you should be aware that C# will bind this method statically at compile time — you will not get the dynamic behaviour you might expect!


.NET Daily Quiz #045

Why is my application crashing? I know for a fact that dynamicCollection implements get_Count!

using System;
using System.Collections;
public class DynamicCount
{
    public static void Main(string[] args)
    {
        PrintCount(new int[10]);
    }
    public static void PrintCount(ICollection collection)
    {
        dynamic dynamicCollection = collection;
        Console.WriteLine(collection.Count);
        Console.WriteLine(dynamicCollection.Count);
    }
}

Result:

10
Unhandled Exception: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'System.Array' does not contain a definition for 'Count'
   at CallSite.Target(Closure , CallSite , Object )
   at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)
   at DynamicCount.PrintCount(ICollection collection)
   at DynamicCount.Main(String[] args)

Answer

Count is implemented explicitly on instances of Array.

When a method is bound at runtime due to dynamic arguments, the runtime type of the dynamic argument is used. In this case the runtime type is System.Array, while the compile-time type is ICollection. The statically bound call to ICollection.Count succeeds because System.Array explicitly implements the ICollection interface. The dynamically bound call fails because… well for the same reason — ICollection is explicitly implemented, so Count is not available through the System.Array type.

.NET Daily Quiz Archive - Week 8

.NET Daily Quiz #036

Why is my T-SQL behaving this way? Is this standard SQL behaviour? Can you think of a good solution, assuming you need your string equalities to be strictly correct?

select 1 where 'e' = 'e   '    -- true
select 1 where '  e' = '  e  ' -- true
select 1 where 'e' = '  e'     -- false
select 1 where 'e' = '  e  '   -- false

Answer

The SQL 92 spec requires a comparison of two strings of unequal length to append trailing spaces to the shorter string until they are the same length, and then make the comparison between the now-equal-length strings. What this means is that if the longer of the two strings has trailing spaces but everything before the trailing spaces is a match then the strings will compare equal.

I found this excellent blog post by Anthony Bloesch which goes over the various ways you can work around this, assuming that the trailing spaces in your strings are meaningful and you need a proper comparison. Also note that any of these approaches will kill your indexing performance.


.NET Daily Quiz #037

Try to do these without compiling. Which of these type definitions is not valid, and why?

using System.Collections;
using System.Collections.Generic;
class T1 where T : System.Enum
{}
class T2 where T : IEnumerable, IEnumerable
{}
class T3 where T : List, IEnumerable, IEnumerable
{}
class T4<L,M,N,O,P> where L : M where N : O where P : N where M : P where O : L
{}
class T5<Q,R,S> where Q : R where R : S where Q : S
{}
class T6<T,S> where S : struct where T : struct
{}
class T7<T,S> where S : T where T : struct
{}
class T8 where T : string
{}
class T9<Q, R, S> where Q : R where R : S where S : IEnumerable
{}
class T10<Q,R,S> where Q : struct, R where R : S where S : T10<Q,R,S>
{}

Answer

Invalid type definitions are (with comments):

// System.Enum is not a valid type constraint - neither are System.Array, System.Delegate or System.ValueType
class T1 where T : System.Enum
{ }
// Circular dependency - L depends on M which depends on P which depends on N which depends on O which depends on L
// (try drawing a dependency graph to make this clearer)
// Type constraints must not contain circular dependencies, either direct or indirect.
class T4<L, M, N, O, P> where L : M where N : O where P : N where M : P where O : L
{ }
// There is a duplicate type constraint - each type parameter must appear only once in the parameter
// constraint list - here Q appears twice
class T5<Q, R, S> where Q : R where R : S where Q : S
{ }
// If S is a T then T must not be a struct - T must be extensible.
class T7<T, S> where S : T where T : struct
{ }
// System.String is a sealed class. Type constraints must not be sealed (this would be logically equivalent
// to just declaring T8 because no other type could be substituted).
class T8 where T : string
{ }
// Q must be a struct and an R but R has a class constraint because R must be an S and S must be a T10<Q,R,S>
// (again, draw a depedency graph and follow the class constraint up).
class T10<Q, R, S> where Q : struct, R where R : S where S : T10<Q, R, S>
{ }

See the C# spec, section 10.1.5 — some of the rules around type parameter constraints are not necessarily obvious.


.NET Daily Quiz #038

In the following program, which DoThings overload is chosen by the compiler?

using System;
class Foo
{
    static void DoThings(params string[] args)
    {
        Console.WriteLine("params method");
    }
    static void DoThings(T args)
    {
        Console.WriteLine("generic method");
    }
    static void Main(string[] args)
    {
        DoThings("why hello there");
        Console.ReadLine();
    }
}

What happens if I change DoThings to this:

using System;
class Foo
{
    static void DoThings(params string[] args)
    {
        Console.WriteLine("params method");
    }
    static void DoThings(T args)
    {
        Console.WriteLine("generic method");
    }
    static void DoThings(string arg1, object arg2 = null)
    {
        Console.WriteLine("two args method");
    }
    static void Main(string[] args)
    {
        DoThings("why hello there");
        Console.ReadLine();
    }
}

Can you explain this behaviour?

Answer

It’s a trick question. This program exploits a bug in the C# compiler. In the first program will select DoThings(params string[] args) while the second program will fail to compile due to an ambiguous invocation. The bug is that both programs are ambiguous and the first program should fail to compile. For a really interesting and detailed analysis of this overload resolution problem take a look at this excellent StackOverflow answer by Eric Lippert.


.NET Daily Quiz #039

I want consumers of my class to be able to do this:

class Program
    {
        static void Main(string[] args)
        {
            Foo foo = new Foo
                          {
                              "insane",
                              "initializer",
                              "syntax!"
                          };
        }
    }

Show me how to write Foo without extending a class that currently supports this kind of initializer syntax.

Answer

We need to implement IEnumerable (why? I don't know. The spec never explains this point and there's no mechanical reason for it I can see) and a method named Add which accepts a single argument (return type not important, apparently). Totally awesomely, the Add method does not need to come from an interface.

Any overload of Add will allow you to add that type to your collection constructor syntax. Normal overload resolution rules apply to Add.

E.g.

using System;
using System.Collections;
public class InsaneConstructorTuesday
{
    public class Foo : IEnumerable
    {
        public void Add(string item) { }
        public void Add(int item) { }
        public void Add(Foo item) { }
        public IEnumerator GetEnumerator() { return null; }
    }
    public static void Main()
    {
        Foo foo = new Foo {
            "insane",
            10000000,
            "syntax!",
            new Foo()
        };
    }
}


.NET Daily Quiz #040

My application crashes at runtime. Why? What important design consideration (C#-specific) am I violating?

class Program
{
    static void Main(string[] args)
    {
        B b = new B();
    }
}
public class A
{
    public A()
    {
        Something();
    }
    public virtual void Something()
    {
    }
}
public class B : A
{
    private string _foo;
    public B()
    {
        _foo = "sdf";
    }
    public override void Something()
    {
        var b = _foo.ToLower();
    }
}

Answer

_foo is null and _foo.ToLower() throws an error as constructors execute from the base type up so _foo hasn't been initialized. If you use ReSharper is a similar tool no doubt you have seen the warnings about calling virtual methods from constructors, and this is why. For more detail check out this StackOverflow question, or read Eric Lipperts excellent blog articles on the topic of constructor and initializer call order, part I and part II.

.NET Daily Quiz Archive - week 7

.NET Daily Quiz #031

Note: this question and the next one generated a lot of intense discussion in my office, which was awesome. See if they do in yours, too!

I need my Javascript application to be fast. Really fast. There are some major performance bottlenecks for this program running on V8. Where are the bottlenecks and why does this cause my program to run slow?
(note: let’s pretend my algorithms are fine. We’re looking for bottlenecks in terms of the way my application is interpreted and compiled)

var Vector = function (x, y) {
    this.x = x;
    this.y = y;
    this.length = function() {
        return Math.sqrt(this.x * this.x + this.y + this.y);
    }
}
var vectors = [];
// some stuff to populate a huge contiguous list of points
for (var i = 0; i < vectors.length; i++) {
    // do some vector processing
    if (vector[i].length() > 10) {
        vector[i].z = 5.0;
    }
}
// do some stuff with my vectors
for (i = 0; i < vectors.length; i++) {
    if (vectors[i].length() > 15.0) {
        vectors[i].label = "large";
    }
}
vectors.push({ x: 1.0, y: 2.0 });
// keep working with vector list

Answer

This one is a little more thorny than most quiz questions, but I wanted to share some interesting stuff I found in a Google IO talk from earlier in the year, Breaking the Javascript Speed Limit with V8. I specified V8 for this question because I know for a fact that this is how it works, but I suspect other engines will generate machine code in a similar way. It probably can’t hurt to apply these techniques regardless of your target platform. It would be really interesting if someone put together some benchmarks to show whether or not this stuff has an effect cross-platform.

I’m not going to go into as much detail as Daniel Clifford did in his talk, so I highly recommend watching it.

The most significant and obvious bottleneck in this example occurs inside the first loop. We are conditionally appending a z field to Vector instances. This is a no-no. The first time the Vector constructor is called, the interpreter will generate a hidden class (at run time, not compile time) that represents the data inside a Vector. This class will be used when working with Vectors going forward. Now if we modify any instance of Vector a new hidden class must be generated.

This is a costly operation and it also results in us losing any optimizations that have been optimistically applied to our Vectors. V8 will apply optimizations under the assumption that things aren’t going to change from under it. If things do change (such as a hidden class change) these optimizations must be thrown out.

Another hidden class change is applied further down when we apply the label field to some vectors.

A couple more little issues that may or may not be lurking in this code:
Are x and y doubles or integers? Integers in V8 are represented as “Small Integers”, or SMIs. A SMI is a 31-bit integer and they are very fast to work with. When a supplied numeric value cannot be represented as a SMI (it’s not an integer or it overflows) it must be represented as a “boxed double”. In this case the memory previously used for the SMI is now a 31-bit object pointer to a boxed double-precision float.

How is the array being prepared?
If an array is initialized to be very big but sparse, for example by using "new Array(128);" then the compiler will use a less efficient data structure because it expects a sparse array (because that’s exactly what you’ve told it to expect). Similarly you should always fill an array in order instead of leaving lots of holes. Contiguous arrays are much faster than sparse arrays when you’re expecting contiguous data at the end.

This has already gone on much longer than I expected, so I’ll leave it here. Expect more Javascript performance questions in the near future. In the meantime, prepare yourself by watching Breaking the JavaScript Speed Limit with V8


.NET Daily Quiz #032

Back to C# (for now).
Is this a valid overload?

interface Foo
{
    void F();
    void F(int i);
    void F(out int i);
}

How about this?

interface Foo
{
    void F();
    void F(int i);
    void F(ref int i);
}

Is this?

interface Foo
{
    void F();
    void F(int i);
    int  F(out int i);
}

Or this?

interface Foo
{
    void F();
    void F(int i);
    void F(out int i);
    void F(ref int i);
}

Why/why not? Do you know the specific rules that determine a method signature?

Answer

The only overload that will fail is

interface Foo
{
    void F();
    void F(int i);
    void F(out int i);
    void F(ref int i);
}

The reason this overload fails is because of a special rule in the C# spec (section 3.6, Signatures and overloading). The definition of a signature is (basically) composed of the method name, type (generic) parameters and the ordered list of parameters (taking into account type and kind, ie value, reference and output). The method return type is NOT part of the method signature. Neither is a params modifier (the params parameter is part of the signature, the modifier is not), nor are type parameter constraints.

A correctly defined method overload must have a unique method signature. Although ref and out modifiers are considered part of the method signature and are distinct, this is an additional rule on overloads — method overload signatures must not differ only in ref and out modifiers. More specifically, a compile-time error occurs if all ref modifier are changed to out modifiers and there is a method signature collision.

Interestingly, the reason for this rule is so that C# programs can be easily translated to run on the CLI. The CLI doesn’t support the same ref and out semantics as C#.

For more detail check out the C# spec section 3.6.


.NET Daily Quiz #033

How many boxing/unboxing operations occur in the following program?

using System.Collections;
using System.Collections.Generic;
using System;
public class Boxer
{
    public static void Main(string[] args)
    {
        int i = 0;
        int j = 0;
        i.ToString();
        ((IConvertible)i).ToInt32(null);
        List l1 = new List();
        l1.Add(i);
        j = l1[0];
        List l2 = new List();
        l2.Add(i);
        j = (int)l2[0];
        ArrayList l3 = new ArrayList();
        l3.Add(i);
        j = (int)l3[0];
        DoThings(i);
    }
    static void DoThings(T t)
    {
        Console.WriteLine(t.ToString());
        List l = new List();
        l.Add(t);
    }
}

Answer

The quick answer here is 3 boxings and 2 unboxings.

Int32 implements its own version of ToString() so no boxed copy is required (more on this in tomorrow’s(?) quiz).

Casting i to IConvertible causes a boxed copy to be created (ToInt32() is implemented explicitly on all numeric types in .NET, so the cast is necessary).

Inserting a retrieving items from List requires no boxed copies — the internal data structure is a simple int[], itself a reference type but capable of containing value types.

List uses object[] as its internal data structure. Clearly this array must store references to types (otherwise how could it possibly allocate the correct amount of memory for any type of object). All value types placed inside a List must be boxed into a reference type for storage and unboxed for retrieval.

The same argument for the prior applies to ArrayList, which is backed by object[].

What about DoThings()? The answer is, there is no boxing or unboxing applied here. Thanks to the magic of generics, even the List is instantiated correctly as a class backed by int[] and therefore storing the correct value types. ToString() is called virtually (as always) but a value type dereference results in no boxing.

Two things to consider about this relatively simple example:

  • All .NET numeric types implement IConvertible explicitly. What are the performance implications of this? Some people believe this was a mistake in the design of the framework — do you agree?
  • You could very easily have answered this quiz by opening the compiled application in ildasm.exe and doing a search for “box” and “unbox”. Is it always this easy to find instances of boxing? (hint: no — wait for tomorrow’s(?) quiz to see why).

.NET Daily Quiz #034

Following on from yesterday’s quiz, is there a boxing operation in the following code? If yes, where and why?

public class Boxer
{
    public static void Main(string[] args)
    {
        ValueType t = default(ValueType);
        var str = t.ToString();
        System.Console.WriteLine(str);
    }
}
public struct ValueType
{
}

Answer

The answer is yes, there is indeed a boxing operation. If you compile this code and open it in ildasm you’ll find the following IL in the main method:

.method public hidebysig static void  Main(string[] args) cil managed
{
  .entrypoint
  // Code size       31 (0x1f)
  .maxstack  1
  .locals init (valuetype ValueType V_0,
           string V_1)
  IL_0000:  nop
  IL_0001:  ldloca.s   V_0
  IL_0003:  initobj    ValueType
  IL_0009:  ldloca.s   V_0
  IL_000b:  constrained. ValueType
  IL_0011:  callvirt   instance string [mscorlib]System.Object::ToString()
  IL_0016:  stloc.1
  IL_0017:  ldloc.1
  IL_0018:  call       void [mscorlib]System.Console::WriteLine(string)
  IL_001d:  nop
  IL_001e:  ret
} // end of method Boxer::Main

So if there is no box opcode, where is the boxed copy? The clue is in the virtual function call to ToString(). Just before the ToString() call you can see the constrained opcode. According to the documentation for constrained:

If thisType is a value type and thisType does not implement method then ptr is dereferenced, boxed, and passed as the 'this' pointer to the callvirt method instruction.

So all of this is just a fancy way of saying that a boxed copy of a value type is always created when you call the virtual members on Object, if and only if your value type does not implement these virtual members. The relevant Object members are GetHashCode(), Equals() and ToString(). Implement these methods to avoid unwanted boxed copies!


.NET Daily Quiz #035

Note: Unless otherwise specified all SQL questions will apply to T-SQL on the latest version of MS SQL Server.

I’m trying to retrieve all log entries with a non-null severity level that is not level 1 or 2. Why isn’t my query returning anything?

select [Time], [Text] from [Logs] where [Logs].LogLevel not in (1,2,null)

Answer

SQL uses three-valued logic for logical predicates like this one. This means that in addition to the usual TRUE and FALSE logical values we have a NULL value. NULL is not the same as FALSE, but it does evaluate to NOT TRUE. So NULL = FALSE is false, as is NULL = TRUE, but NULL != TRUE is true. This causes the NOT IN predicate to fail on all NULL values (and every comparison involves a null comparison AND’ed together with comparison to 1 and 2 in our predicate).

A colleague pointed out to me that you can “resolve” this issue in T-SQL by using SET ANSI_NULLS OFF. He also correctly pointed at that you should never do this. According to the documentation:

In a future version of SQL Server, ANSI_NULLS will always be ON and any applications that explicitly set the option to OFF will generate an error. Avoid using this feature in new development work, and plan to modify applications that currently use this feature.

There were a couple of alternative queries posted. One colleague offered this

Select [Time], [Text] from [Logs] where (ISNULL([Logs].LogLevel , -1) NOT IN (-1, 1, 2))

And another gave me:

select [Time], [Text] from [Logs] where [Logs].LogLevel not in (1,2) and [Logs].LogLevel is not null

I’m not enough of a SQL expert to know which is the better solution, but they both seem to work correctly.