How the Async support in RestSharp can help with Report Generation

I've been using RestSharp in the past weeks for some backend tools, mainly because REST is easy to implement and I like the Deserialization support that comes with it. One of the reasons I wanted to add async support is because I wanted to write a monitoring application.

I am using Microsoft Chart Controls, and I have a server that accepts REST Requests in the form /reports/health/{entity} to return something like this:

<healthdata>
    <healthindex>73</healthindex>
</healthdata>

I don't know which entities are being pinged at compile time. I can't even put them in a config file, as someone might add or remove entities while the application is still running. So I decided to have an architecture with a Timer, a Request Dispatcher and a Response Handler.

The Timer executes every 5 seconds, gets the current list of entities and calls the Request Dispatcher:

private void timer1_Tick(object sender, EventArgs e)
{
    foreach(string entity in GetEntities()){
        DispatchRequest(entity);
    }
}

The Request Dispatcher creates a new RestRequest and a custom State Object. The state object will be important later, for now lets just note that it contains the entityname and the time the request was sent.

private void DispatchRequest(string entityName)
{
    var rq = new RestRequest("health/{entity}", Method.GET);
    rq.AddParameter("entity", entityName, ParameterType.UrlSegment);

    var state = new ServiceState {Entity = entityName, RequestDateUtc = DateTime.UtcNow};
    _client.BeginExecute<healthdata>(rq, HandleResponse, state);
}

The Request Dispatcher doesn't care what happens afterwards, it just happily fires new requests all the time and tells BeginExecute to call HandleResponse afterwards.

private void HandleResponse(IAsyncResult res)
{
    // Note: This assumes the response is always good. In reality, you want to
    // check response.ResponseStatus and act accordingly
    var result = (AsyncResult) res;
    var caller = (RequestExecuteCaller<healthdata>) result.AsyncDelegate;
    var state = res.AsyncState as ServiceState;
    var response = caller.EndInvoke(res);
    AddDataToChart(state.Entity, response.Data.healthindex, state.RequestDateUtc);
}

Here comes our state object into play. If you look at the XML from the server above, you see that it doesn't echo the Entity Name. As HandleResponse handles all responses, it has to know which response corresponds to which server. Also, async operations can come in a different order than the one executed - I may send requests for entities s1, s2, s3, s4 and receive them back in the order s3, s1, s4, s2. If the timer interval is too small and the server latency is too high, I might even receive a later request for s2 before an earlier one - it's pure anarchy.

That's why I've passed a state object to BeginExecute, as this allowed me to capture the entity name and the exact Date of the request. So all that HandleResponse has to do is to get the state and healthdata and call the function that adds the data to the chart with precise information about the Time, the Entity and it's health.

All without ever locking the UI or without giving the dispatcher or the handler too much work that they shouldn't be doing.


(Excuse the horrible colors, I'm only a developer :) )

How Optional Parameters work, why they can be dangerous, and why they work in .net 2/3 as well

One of the changes I really like in Visual Studio 2010 are optional parameters to method. Basically they allow you to specify a default value for each parameter and thus reduce the number of overloads to a method. Make sure to read to the end as there is a huge word of caution regarding optional parameters.

Instead of having this:

private static string SomeMethod(int value)
{
    return SomeMethod(value, "Was Empty");
}

private static string SomeMethod(int value, string data)
{
    return string.Format("{0}: {1}", value, data);
}

You can just have this:

// static because it's a console app, no extra magic
private static string OptionalMethod(int value, string data = "Was Empty")
{
    return string.Format("{0}: {1}", value, data);
}

The nice thing about this feature is that it also works with .net 2/3. Why? Because it is implemented like this:

private static string OptionalMethod(int value,
                          [Optional, DefaultParameterValue("Was Empty")] string data)
{
    return string.Format("{0}: {1}", value, data);
}

Looks like a normal method (the longest overload) with some attributes. Are those attributes the magic? No, it's much simpler.
If this is your calling code:

Console.WriteLine(OptionalMethod(1));
Console.WriteLine(OptionalMethod(2,"Test"));

Then this is how it looks in the compiled assembly:

Console.WriteLine(OptionalMethod(1, "Was Empty"));
Console.WriteLine(OptionalMethod(2, "Test"));

As you see, the compiler changes the method call to put in all optional parameters into the caller. This explains why this works with pre-.net 4 applications. However, this also explains the huge warning I want to give:

Changing the optional parameter value will not change the behavior of your callers!

Imagine you want to change the value to "No value specified". With the overload solution this is trivial:

private static string SomeMethod(int value)
{
    return SomeMethod(value, "No value specified");
}

Recompile your provider assembly and every consumer who uses that overload will get the new value. However, with optional parameters you have to re-compile each and every consumer assembly as well, so changing an optional parameter is a big, breaking change.

Does that mean that Optional Parameters are evil and should be avoided at all costs? Some people might think so, but I think they are useful for variables that almost never change. Think about a function like SendMail(string server, int port = 25).

However, I do object that ReSharper 5 offers the hint to introduce Optional Parameters too lightly:

Edit: I gave the ReSharper guys bad credit. If the method is declared as public, it does not offer that hint. Sorry guys for not double checking that! This then makes complete sense, as Optional Parameters are fine and safe for private/internal members.

I know too many people why blindly follow any advice that ReSharper give and that might see optional parameters as "Ohh, Shiny, no more Overloads!". Of course, you can argue that developers should know their stuff and deserve to fall hard on their face once they rolled it out as a big API and suddenly realize they have to recompile/redeploy dozens or hundreds of consumer assemblies because of a change. I am however not a fan of this type of developer elitism and would have preferred if ReSharper would not show that hint by default.

Just my 2 cents though.

Writing a BF Compiler for .net (Part 3: pointer++ and pointer–)

The last article gave an introduction to the concepts and we started looking at the memory[pointer]++/-- functions. Today is a quicky post to look at two other instructions, pointer++ and pointer-- or > and < in BF.

Unsurprisingly, these are extremely simple operations, requiring only 5 IL Commands:

  IL_0000:  ldsfld     int16 BFHelloWorldCSharp.Program::pointer
  IL_0005:  ldc.i4.1
  IL_0006:  add
  IL_0007:  conv.i2
  IL_0008:  stsfld     int16 BFHelloWorldCSharp.Program::pointer

ldsfld loads a static field onto the evaluation stack, ldc.i4.1 pushes the number 1 to the stack, add takes the two values from the stack and pushes the result back. conv.i2 converts the value on the stack (which is an Int32) to Int16 (2-Byte Int), pads it to be Int32 (as that is the smallest datatype possible on the stack) and pushes it back. stsfld then replaces the value in the static variable with the value from the stack.

As usual, pointer-- works the exact same way with the difference of using sub instead of add. One word of note: sub subtracts value2 from value1. While the order doesn't matter for add, it does for sub. Also note that add and sub do not detect overflow, so you can happily add 1 to Int16.MaxValue. If you want overflow checking, there is sub.ovf and add.ovf which throw an OverflowException.

With the 4 easy operations done, we will look at . and , next for input and output. Finally, we will look at [ and ]. After we have looked at the IL for each operation, we will write our compiler.

Writing a BF Compiler for .net (Part 2: Writing BF in C# and looking at the IL)

In the last posting, we looked at BF as a language and how to write an interpreter in C#. The point of doing that was to understand what each function actually does. As I am no IL Expert, my second step was to write the Hello World BF Application as C#, that is one extremely long function with each BF command spelled out. You can find the full function here. This is an Excerpt:

static void Main(string[] args)
{
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    memory[pointer]++;
    while(memory[pointer] > 0)
    {
        pointer++;
        memory[pointer]++;
        memory[pointer]++;
        memory[pointer]++;
        // snipped
    }
    // snipped
}

As you see, we literally translated each instruction into a C# instruction. Compile this in Debug Mode, load it up in ildasm:

Whoa, now were talking! Note that the ildasm will look different between Debug and Release modes due to optimizations. I prefer Debug to get an understanding how this works, and then later on Release mode to see the differences. The point is really to understand how this stuff looks in IL as we have to write IL ourselves later on.

So the first C# instruction is memory[pointer]++, which is a short way of saying memory[pointer] = memory[pointer] + 1. It is important that many language constructs and short-hand commands simply do not exist in IL - they are convenience functions in the C# compiler. Looking at the IL, our first C# instruction is now 9 IL instructions, starting from IL_0001 and ending at IL_0019.

  IL_0001:  ldsfld     uint8[] BFHelloWorldCSharp.Program::memory
  IL_0006:  ldsfld     int16 BFHelloWorldCSharp.Program::pointer
  IL_000b:  ldelema    [mscorlib]System.Byte
  IL_0010:  dup
  IL_0011:  ldobj      [mscorlib]System.Byte
  IL_0016:  ldc.i4.1
  IL_0017:  add
  IL_0018:  conv.u1
  IL_0019:  stobj      [mscorlib]System.Byte

Let's quickly look at each command before I explain in Detail what is happening:

  1. Push the value of a static field 'memory' onto the evaluation stack.
  2. Push the value of a static field 'pointer' onto the evaluation stack.
  3. Load the address of the array element at a specified array index onto the top of the evaluation stack as type & (managed pointer).
  4. Copy the current topmost value on the evaluation stack, and then pushes the copy onto the evaluation stack.
  5. Copy the value type object pointed to by an address to the top of the evaluation stack.
  6. Push the integer value of 1 onto the evaluation stack as an int32.
  7. Add two values and pushes the result onto the evaluation stack.
  8. Convert the value on top of the evaluation stack to unsigned int8, and extends it to int32.
  9. Copy a value of a specified type from the evaluation stack into a supplied memory address.

That looks more complicated than it really is, especially if you don't know how CPUs usually work or never worked with Assembler. .net/IL is a Stack Based language, and functions work against this stack. For example, the Add instruction at address IL_0016 does not take parameters. Instead, it removes 2 elements from the stack, adds them, and pushes the result back to the stack. Be aware that most functions assume that the objects on the stack have the correct type - trying to push a string and an Int32 and calling Add will crash the CLR. Not all functions require their parameters on the stack, but many do - you can find an Overview of each function in Partition III of the ECMA-335 standard or in the OpCodes Fields.

Anyway, let's dissect the instruction and look at what's happening.
1. Getting memory[pointer]
In order to increment the value at memory[pointer], we first need to get it. This is the job of the ldelema instruction at IL_000a. To quote from the documentation:
Loads the address of the array element at a specified array index onto the top of the evaluation stack as type & (managed pointer).

So this function requires an array and an array index, and it gives us a managed pointer to that element. It expects that we push the array and the index to the stack (in that order). It will then pop (remove) these two elements from the stack and pushes the pointer back to the array.

The ldsfld function pushes the value of a static field onto the stack (pointer and memory were declared static). So we first push the memory array to the stack, and then the value of the pointer variable.

Here is a diagram:

2. Incrementing the value
Now we have a pointer to memory[pointer], but we need to increase it. That is what the next 4 instructions do. The important function is add at IL_0017, which takes two values from the stack and adds them. But at the moment, we only have the managed pointer on the stack. We first need to get the value of memory[pointer] to the stack. This is that ldobj at IL_0011 does: It gets the address of the value from the stack and pushes the actual value onto it. Note that this is now just a byte - it has no connection to the memory array, it is simply a number.

Before that, you might wonder what the dup instruction does: It simply creates a copy of the topmost value on the stack, effectively duplicating it. This might seem useless for the addition, but it is required later. For now, just ignore it.

Now that we have the current value of the memory[pointer], we need the second number to perform the addition. As ++ is just a shorthand for +1, we need to push the number 1 to the stack. To push an Int32 value to the stack, you can use the ldc.i4 instruction which pushes the supplied number to the stack. However, there are also shorthand commands for some numbers, and the command ldc.i4.1 pushes the number 1 to the stack. (I do not know if these commands exist for performance or for code size reasons, also I strongly believe the latter. ldc.i4.1 is a 1-byte command, whereas ldc.i4 requires 5 bytes: 1 for the command and 4 bytes for the Int32 value to push).

So now we have the current value of memory[pointer] and the number 1 on the stack, and the Add function pops them off, performs the addition and pushes the result.

A diagram for this part:

Just a slight note: The 1 at the end is not the same 1 as the one before. Add removes 0 and 1 from the stack, "generates a new 1" through performing 0 + 1 and pushes that to the stack.

3. Storing the value in the array again
We performed the addition, but as a result we simply have the number 1 on the stack now - memory[pointer] is still 0 though! The last two instructions take care of saving it back. Now, the number 1 on the stack is an Int32, but we have an array of Bytes. The first instruction, conv.u1 at IL_0018 gets a value off the stack, converts it to an unsigned 8-bit integer (which is exactly what a Byte in .net is), adds padding to Int32 and pushes it back to the stack. This might seem silly, but the evaluation stack only holds 4- or 8-byte integers (See Partition III, 1.1 Data Types of ECMA-335). The conv instruction makes sure that the actual data is stored in the "correct" bits (the ones later used to read the value), while the other 24 bits are merely padding to make it an Int32.

stobj copies the value to the desired memory location. You may now understand why we had the dup instruction at IL_0010: We need to know where to copy the value to, but the ldobj object removed the address from the stack already at IL_0011! So by duplicating the address, we can give it to both functions (otherwise we would have to do the lookup in the first three lines again).

And that is the memory[pointer]++ C# instruction in IL! I know it looks scary at first, but it's rather simple and logical once you understood how this stuff works internally. Remember, at some point each programming language needs to be translated into very specific CPU instructions. We are not at that level (this is what the CLR and it's JITter does), but we are at a fairly low level. Remember, we have to write a compiler for this, so we need to understand a) what is happening and b) why it is happening. Making those diagrams helped me a lot, especially because I didn't understand the use of the dup instruction at first.

Now that we know how this instruction works, we will look at the other 7 BF Instructions. I can already tell you how memory[pointer]-- works: It is exactly the same function, with the difference of using sub instead of add at IL_0017, so no need for an article of it's own.

The two pointer operations (pointer++ and pointer--) will be described in the next article.

Writing a BF Compiler for .net (Part 1: Explanation of the language and interpreter in C#)

Today I'm starting a small series about how to write a Compiler to .net IL. I wanted to understand IL better and wanted to learn how .net really works. Even though I'm a Web Developer (which usually means working on a much higher level of abstraction), at some point there is this impossible issue that has no cause and no resolution and where Reflector and WinDbg have to come in.

My main focus is understanding IL, not implementing a language. So I picked an extremely simple but otherwise complete language - Brainf**k (abbreviated BF from here on). Yes, it is a very esoteric language, but as said, focus is on the IL Part, not the actual language.

BF only has 8 instructions, represented by a single character each:

> Moves the memory pointer forward
< Moves the memory pointer backwards
+ Increments the value at the current memory pointer
- Decrements the value at the current memory pointer
. Outputs the value at the current memory pointer
, Reads a single byte from some input, storing it at the memory pointer
[ Go to the next instruction if the byte at the memory pointer is not 0, otherwise move it past the matching ] instruction
] Go to the instruction after the matching [ if the byte at the memory pointer is not 0, else move it past the ]

So basically with BF you have three objects: Some Memory, a Pointer to a byte in the Memory and an instruction pointer. The memory can be implemented as a simple byte array and the memory pointer as an index into it.

Let's say we have 4 bytes of memory. At the beginning, everything is initialized to 0 (P = Memory Pointer):
Memory: 00 00 00 00 P:0

Now we execute the following code:
++>+++>++++>+<-

The result is this:
Memory: 02 03 03 01 P:2

The [ and ] instructions are more complicated, but essentially they form a while loop - while(memory[pointer] != 0) { // instructions between the brackets }. One example is to increase the value of a register without having many + signs. Let's say we want to increase memory[1] to 25. For that, we use memory[0] as a counter:
+++++[>+++++<-]

After running this, the memory will look like this:
Memory: 00 19 00 00 P:0
(Remember that this is hex, so 19h is 25 decimal)

What did we do? We increased memory[0] to 5. Then we entered a while-block. First, we move the memory pointer to memory[1] and increase it 5 times, to 5. Then we move back to memory[0] and decrease it to 4. The ] then checks if memory[0] is 0, which it isn't, so we return to the instruction after the [, which means we move again to memory[1], increase it 5 more times, move back to memory[0], decrease it to 3 and repeat until memory[0] is 0.

As said, not terribly complicated once you understood how the brackets and the memory pointer work. Let's look at Hello World in BF:
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
(Note that this needs 5 bytes of memory instead of 4)

This looks scary, but is also quite simple if you are familiar with ASCII Encoding. The . command outputs the value at the memory pointer to the console and the console then needs to decide what to do with it. Again, everything in a PC is essentially just a stream of bytes, and a byte can mean different things to different applications. The Console tries to display the byte by looking up which character maps to a byte. In ASCII, the letter H is mapped to byte 48h or 72 decimal. So if you want to display the letter H, you need to increase the value 72 times.

The Hello World example uses the while loop to save a lot of +. Also instead of using 13 Bytes (The Letters H, e, l, l, o, [space], W, o, r, l, d, !, [line break]), the example only uses 5 bytes by reusing existing memory locations. Look up the values for each letter in the ASCII table and it should become clear how this works. The Wikipedia Article contains a lot more explanations.

Now, before I started building a real compiler, I first wanted to see how this would work and could be implemented, so I started with an interpreter in C# that reads text from one TextBox and outputs results into another one (no support for the , instruction here though). The code for that is somewhat naive, but it helps understanding how this could work:

byte[] memory = new byte[Int16.MaxValue];
Int16 memoryPointer = 0;

private void Execute(char[] code)
{
    int instructionPointer = 0;
    while (instructionPointer < code.Length)
    {
        var currentCommand = code[instructionPointer];
        instructionPointer++;
        switch (currentCommand)
        {
            case '>':
                memoryPointer++;
                break;
            case '<':
                memoryPointer--;
                break;
            case '+':
                memory[memoryPointer]++;
                break;
            case '-':
                memory[memoryPointer]--;
                break;
            case '.':
                // Weird casting because AppendText wants a string, but we have to
                // convert the byte to a char first.
                textBox2.AppendText(((char)memory[memoryPointer]).ToString());
                break;
            case ',':
                // read 1 byte of input - not implemented
                break;
            case '[':
                int currentIndex = instructionPointer;
                int bracketCounter = 1;
                while (bracketCounter > 0 &&
                       instructionPointer < code.Length &&
                       code[instructionPointer] > 0)
                {
                    if (code[instructionPointer] == '[') bracketCounter++;
                    if (code[instructionPointer] == ']') bracketCounter--;
                    instructionPointer++;
                }
                if (bracketCounter == 0)
                {
                    // Change previous ] to \0 so that the recursive call
                    // returns at the end of the block
                    code[instructionPointer - 1] = '\0';
                    while(memory[memoryPointer] > 0) {
                       Execute(code.Skip(currentIndex).ToArray());
                    }
                    code[instructionPointer - 1] = ']';
                    break;
                }
                break;
            case ']':
                // The ] bracket is normally handled as part of the [ case above
                throw new InvalidOperationException("Unbalanced Brackets");
                break;
            case '\0':
                return;
        }
    }
}

First, we declare our memory. As this is .net and I didn't want to use unsafe code/pointers, I'm declaring an Array of Bytes. The total size is 32767 bytes. Then I declare a memoryPointer which is just an index into the memory array (it's not a pointer in the traditional sense, I just called it that).

Then we have the Execute method, which takes an array of bytes - the BF code. We iterate through this array and perform the appropriate action for each instruction. This should be really straight-forward, except for the [, ] and \0 cases. If we encounter a [, we go all the way to the end in order to find until the instruction pointer hits the end of the code or a 0-byte. The bracketCounter is there for us to make sure our brackets are balanced - you can nest brackets, but they have to be balanced. [++[++]++ is illegal code as the first [ does not have a matching ]. As soon as our bracketCounter hits 0 again, we stop this loop. The important side effect of this: The instructionPointer is now behind the ].

We replace the ] with a 0-byte and recursively call the Execute function with the fraction of the code after the [ (that's the job of the currentIndex variable: Remember where the [ was). The recursive call will return as soon as it hits the 0-byte. The while-loop then repeats this recursive call until the memory at the pointer is 0 and re-inserts the ].

If you execute this in a WinForms application, your textBox2 should display Hello World!.

Phew, I hope you could follow me all the way down here and got an understanding of how BF works. Tomorrow we will translate the BF code into C# code and look at the generated IL, and then I will post articles explaining what each of these instructions do, how IL OpCodes work, what the fact that .net is Stack based actually means, and then we will have a real BF -> .net Compiler.

Expect a fun little series :)

Do we need an open source alternative to Reflector?

I just started .net Reflector and got the update prompt:

Nothing special here, except that both options are bad. Clicking Yes gives me this window:

And clicking No prevents Reflector from starting. So basically I can't use Reflector unless I download a new version manually or disconnect my PC from the internet or somehow else suppress the update check. This got me thinking about the Status of .net Reflector.

It's an excellent tool, to the point it's almost essential for many developers. Lutz Roeder did a fantastic job with it, and Red Gate is continuing to improve it. But the tool is not free, and the message above just had me realize again that it can be shut down any time. Of course, Red Gate has promised to keep a free version and I have no reason to think they are not truthful, but at the same time I've seen many companies shutting down free products for whatever reason. This would be absolutely their right, but it got me thinking: Is .net Reflector "too big to fail"?

Should there be an Open Source Alternative? Or am I too heavily biased (being a SharePoint developer) and overestimate the importance of it?

Experimental Async support for RestSharp

I have just commited a change to my ASync branch on my fork of RestSharp on GitHub. I want to do some more testing first before I ask John Sheehan to pull in the change. The new files are IAsyncRestClient.cs and AsyncRestClient.cs which derive from their normal (synchronous) implementations. Edit: This change was merged into the official RestSharp source code and is documented on the RestSharp Wiki. There was a major bug fix to the functionality on April 18, 2010.

Anyway, here are the examples on how to use it. I assume you know what the state parameter on an Async call does, if not just set it to null.

var client = new AsyncRestClient(serviceUrl);
var request = new RestRequest(Method.GET);

// Synchronous Execution still works - AsyncRestClient derives from RestClient
var SyncResponse = client.Execute(request);
Console.WriteLine(response.Content);

// But Asynchronous Execution is much nicer!
// Method 1: Waiting for an Asynchronous Call with EndExecute
var asyncRes1 = client.BeginExecute(request, null, "some state, can be null");
var responseAsync1 = client.EndExecute(asyncRes1);
Console.WriteLine(responseAsync1.Content);

// Method 2: Polling for Asynchronous Call Completion
var asyncRes2 = client.BeginExecute(request, null, "some state");
while (!asyncRes2.IsCompleted)
{
    Console.Write(".");
    System.Threading.Thread.Sleep(100);
}
var responseAsync2 = client.EndExecute(asyncRes2);
Console.WriteLine(responseAsync2.Content);

// Method 3: Waiting for an Asynchronous Call with WaitHandle
var asyncRes3 = client.BeginExecute(request, null, "some state");
asyncRes3.AsyncWaitHandle.WaitOne();
var responseAsync3 = client.EndExecute(asyncRes3);
Console.WriteLine(responseAsync3.Content);

// Method 4: Using a Callback Method
static void Main(string[] args)
{
    var asyncRes4 = client.BeginExecute(request, EndResponse, "some state");
    Console.ReadLine();
}

static void EndResponse(IAsyncResult res)
{
    var result = (AsyncResult)res;
    var caller = (RequestExecuteCaller)result.AsyncDelegate;
    var response = caller.EndInvoke(res);
    Console.WriteLine("AsyncResult: "+response.Content);
}

// This also works for the Execute<T> method, without callback...
var asyncResT = client.BeginExecute<MyDTOClass>(request, null, "some state");
var responseAsyncT = client.EndExecute<MyDTOClass>(asyncResT);
Console.WriteLine(responseAsyncT.Data.SomePropertyInMyClass);

// ...and with Callback
static void Main(string[] args)
{
    var asyncResTC = client.BeginExecute<MyDTOClass>(request, EndResponse, "some state");
    Console.ReadLine();
}

static void EndResponse(IAsyncResult res)
{
    var result = (AsyncResult)res;
    var caller = (RequestExecuteCaller<MyDTOClass>)result.AsyncDelegate;
    var response = caller.EndInvoke(res);
    Console.WriteLine("AsyncResult: "+response.Data.SomePropertyInMyClass);
}

If you update your BCS Model, you may need to restart the OSearch14 service

I'm experimenting with the SharePoint 2010 BCS at the moment, using custom .net code to implement the IdEnumerator and SpecificFinder methods, to be used in the Search Crawler.

As this is backed by a .net class, I had some issues getting changes to it done. I've added a new field, updated the .bdcm file, redeployed and started a Full Crawl, only to get error messages in the Crawl saying that the Property that I've added cannot be found in the backing class.

Solution is as simple as logical: Reset the SharePoint Server Search 14 service (OSearch14). As this holds the Assembly in memory and as the Sharepoint deployment tools do only recycle the IIS process, there will be a mismatch between the .bdcm file and the (outdated, but still loaded) assembly.

Also, you might have to re-create the Content Source. And don't forget to increase the version number if you edit the .bdcm file manually.

A slight Nitpick/Improvement for Tekpubs Video about ASP.net MVC Routing

Tekpubs series about ASP.net MVC 2 is really useful and has tons and tons of great tips and code samples, and the episode about Routing (#12) is no exception. However, there is one thing I really didn't like about it.

Around 30 Minutes in when discussing SEO Optimizations, they show a great tip how to enforce canonical URLs. Unfortunately, they do it by modifying the Response object directly. Basically the code looks like

// This snippet doesn't make much sense if you haven't
// watched the Episode, sorry.
public ViewResult Details(int id)
{
    //snipped some parts
    EnforceCanonicalUrl(jobAd.DetailsRouteValues());
    return View(jobAd);
}

private void EnforceCanonicalUrl(RouteValueDictionary routeValues)
{
    if(someCheckFails){
        Response.RedirectLocation = someLocation;
        Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
        Response.End();
    }
}

This doesn't follow the MVC pattern, and the speaker even acknowledges it in the video as it makes unit testing hard. The proper way is to return an ActionResult from the ControllerAction instead. As I happen to have already written a PermanentRedirectResult, my suggestion to improve this code is this:

public ActionResult Details(int id)
{
    //snipped some parts
    var enforcedResult = EnforceCanonicalUrl(jobAd.DetailsRouteValues());
    // As View and enforcedResult have different types, I cannot use the ?? operator
    if(enforcedResult != null) return enforcedResult;
    return View(jobAd);
}

private PermanentRedirectResult EnforceCanonicalUrl(RouteValueDictionary routeValues)
{
    if(someCheckFails){
        return new PermanentRedirectResult(canonicalPathAndQuery);
    } else {
       return null;
    }
}

So I changed the return type of the controller action to ActionResult instead of ViewResult, and I gave EnforceCanonicalUrl a return type. This function now either returns null or a PermanentRedirectResult, and the controller action now checks for it. There are of course more ways to refine it, but it is in line with the ASP.net MVC pattern of having Controller Actions return ActionResults instead of modifying the Response directly.

Project Server 2010 Beta: “Failed to provision site PWA with error”

I've installed Project Server 2010 Beta on my SharePoint 2010 Beta server and got this error when provisioning the PWA site:

Failed to provision site PWA with error: Microsoft.SharePoint.SPException: The template you have chosen is invalid or cannot be found. ---> System.Runtime.InteropServices.COMException (0x81071E44): 0x81071e44The template you have chosen is invalid or cannot be found.
at Microsoft.SharePoint.Library.SPRequestInternalClass.ApplyWebTemplate(String bstrUrl, String bstrWebTemplateContent, String& bstrWebTemplate, Int32& plWebTemplateId)
at Microsoft.SharePoint.Library.SPRequest.ApplyWebTemplate(String bstrUrl, String bstrWebTemplateContent, String& bstrWebTemplate, Int32& plWebTemplateId)
--- End of inner exception stack trace ---
at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)
[...]

The solution turned out to be simple: Just wait. Seems like there is an additional Timer Job for provisioning site templates and stuff. No idea when it runs as I had a weekend between failure and success, but it works now.