PrimordialCode is now powered by the open source Dexter Blog Engine

Print Content | More

It's time for another new beginning.

Over a month ago I joined the Dexter's Developers Team, cause I felt that the project was indeed good and I liked the idea to participate in developing something I could also have used.

When I entered the project it was missing some features I considered vital for me to switch over:

  • The ability to import all my previous data from Wordpress.
  • The support for multiple categories for every post.
  • A better integration with Windows Live Writer (the primary tool I always used to make my posts) to support hierarchical categories, tags and slugs.

I've worked on all these in my spare time during the last month and now that all of them are implemented (there's still some work to do to improve the import section and integrate it in the blog engine instead of using an external tool), I see no reason to not switch over.

So let's say good-bye to my Wordpress version of the blog that accompanied me during this almost two years of blogging experience and say welcome to the new PrimordialCode powered by Dexter (the actual skin is kindly stolen from Ugo’s blog).

There's still more room for improvements and we have a lots of new features on the horizon to implement.

If you are curious about Dexter go check our feature list and download the source code from our
project page on CodePlex: http://dexterblogengine.codeplex.com/

A big thank to all the guys of the team for making this possible.


Dexter

2 comments

JQuery, WCF and the JSON DateTime serialization

Print Content | More

Days ago I blogged about how to call a WCF service from a jQuery application to retrieve and send data to the server to realize a small interactive chatting application. Everything was working fine until it came to format any DateTime data passed from the server to the client.

We ended up having a call like this:

var msg3 = { "msg": { "Id": "2", "Sender": "Webpage", "Text": "Sended Text" }, "id": "1" };
 
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: serviceUrl + "TestMessageModifyWithSuppliedContent",
        // data: "{ \"msg\" : " + JSON.stringify(msg) + ", \"id\" : \"4\"}",
        data: JSON.stringify(msg3),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            //debugger;
            var text = data.d.Timestamp + " - " + data.d.Sender + " - " + data.d.Text;
            alert(text);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("Error Occured!");
        }
    });
});

the output of this code is a Message Box with the following text:

"/Date(1267695086938+0100)/ - Webpage - 3 - Second parameter passed: 1"

Looking at fiddler for the request and the response we have:

POST http://localhost.:58817/Services/ChatService.svc/TestMessageModifyWithSuppliedContent HTTP/1.1
...Request plumbing goes here...
Pragma: no-cache
 
{"msg":{"Id":"2","Sender":"Webpage","Text":"Sended Text"},"id":"1"}
HTTP/1.1 200 OK
...Response plumbing goes here...
Content-Type: application/json; charset=utf-8
Content-Length: 160
Connection: Close
 
{"d":{"__type":"Message:#LiveAssistance.Entities","Id":2,"Sender":"Webpage","Text":"3 - Second parameter passed: 1","Timestamp":"\/Date(1267694873788+0100)\/"}}

I was surprised at fist, then a quick research show that this is the way WCF serializes the DateTime object in JSON. In short the first number represent the number of milliseconds in the GMT time zone, regular (non-daylight savings) time since midnight, January 1, 1970. The number may be negative to represent earlier times. The second is the time zone (more info here: Stand-Alone JSON Serialization).

So we have to do something to convert this string representation in a JavaScript Date() object. To do so we can use the ‘dataFilter’ feature of the jQuery ajax() call that allows us to modify and alter the returned JSON string representation before it’s passed on to the parser.

We basically want to change the string “/Date(1267695086938+0100)/” to
“new Date(Date(1267695086938+0100)”; to do so we can rewrite our ajax call like this:

   1: $(document).ready(function() {
   2:         $.ajax({
   3:             type: "POST",
   4:             url: serviceUrl + "TestMessageModifyWithSuppliedContent",
   5:             // data: "{ \"msg\" : " + JSON.stringify(msg) + ", \"id\" : \"4\"}",
   6:             data: JSON.stringify(msg3),
   7:             contentType: "application/json; charset=utf-8",
   8:             dataType: "json",
   9:             dataFilter: function(data, type) {
  10:                 var d = data.replace(/"\\\/(Date\(.*?\))\\\/"/gi, 'new $1');
  11:                 return d;
  12:             },
  13:             success: function(data) {
  14:                 //debugger;
  15:             var text = data.d.Timestamp.format("yyyy/mm/dd - HH:MM:ss") + " - " + data.d.Sender + " - " + data.d.Text;
  16:                 alert(text);
  17:             },
  18:             error: function(XMLHttpRequest, textStatus, errorThrown) {
  19:                 debugger;
  20:                 alert("Error Occured!");
  21:             }
  22:         });
  23:     });

Lines 9 - 12 shows our filtering function with the Regex we use to convert the returned JSON string representation to this one:

FROM:
 
"{"d":{"__type":"Message:#Entities","Id":2,"Sender":"Webpage","Text":"3 - Second parameter passed: 1","Timestamp":"\/Date(1267696301237+0100)\/"}}"
 
TO:
 
"{"d":{"__type":"Message:#Entities","Id":2,"Sender":"Webpage","Text":"3 - Second parameter passed: 1","Timestamp":new Date(1267696301237+0100)}}"

Everthing is now working and the Timestamp field contains a Date object...if you use jQuery 1.3.x...

If (like me) you use jQuery 1.4.x you will get an error from the JSON serializer with an ‘invalid JSON format’ message...and guess...he’s right because “new Date(something)” isn’t a valid representation...so why the hell it all worked before?

It turned out that jQuery 1.3.x used the JavaScript eval() function to internally deserialize objects (so using the method above we used in reality a trick), jQuery 1.4.x relies on the browser capabilities to deserialize JSON streams (in particular it uses the window.JSON object if the browser has support for it).

It does this way mainly for performances and security reasons. So we have 2 ways now to get a Date object from our string representation:

1- process each object with a function that (using the previously pointed regex and the eval() function) convert each date field to the corresponding object.

2- change the way the data are parsed (client-side) and do our own JSON deserialization using eval(), to act this way we need to change the ‘dataType’ returned from ‘json’ to ‘text’ - this way we disable the automatic deserialization - then we have to call the eval() function on the returned and modified data stream:

   1: $(document).ready(function() {
   2:         $.ajax({
   3:             type: "POST",
   4:             url: serviceUrl + "TestMessageModifyWithSuppliedContent",
   5:             // data: "{ \"msg\" : " + JSON.stringify(msg) + ", \"id\" : \"4\"}",
   6:             data: JSON.stringify(msg3),
   7:             contentType: "application/json; charset=utf-8",
   8:             dataType: "text",
   9:             dataFilter: function(data, type) {
  10:                 var d = data.replace(/"\\\/(Date\(.*?\))\\\/"/gi, 'new $1');
  11:                 return d;
  12:             },
  13:             success: function(data) {
  14:                 //debugger;
  15:                 data = eval('(' + data + ')');
  16:                 var text = data.d.Timestamp.format("yyyy/mm/dd - HH:MM:ss") + " - " + data.d.Sender + " - " + data.d.Text;
  17:                 alert(text);
  18:             },
  19:             error: function(XMLHttpRequest, textStatus, errorThrown) {
  20:                 debugger;
  21:                 alert("Error Occured!");
  22:             }
  23:         });
  24:     });

Lines 8, 15 and 16 shows the modifications we made. As you can see in line 16 now you can use the Timestamp field as a data object and format it using any JavaScript DateTime formatting library you like.

As a note: using this second method you are obviously loosing in performance and security, cause eval() is slower than the native JSON deserialization, and even worse you are subject to code injection attacks.


Datetime, Jquery, Json, Wcf

1 comments

Working on Dexter

Print Content | More

I’m not writing many posts in the last days and the main reason is I’ve actively joined the development of Dexter a new open source blog engine written in ASP.NET MVC, you can find it on Codeplex at this link: http://dexterblogengine.codeplex.com/

When my friend Andrea Balducci, talked to me about this project I was so excited I decided to give it a try...and it was good (there’s always room for improvement, but it was good!).

I do love Wordpress, it’s a great product and works very well, why working on another blog engine then? well the answer is short: I’m a developer and I don’t know how to put my hands on the WP engine...simply cause I hate php and never spent more than a couple of hours trying to learn it.

With Dexter is all another story...C#, ASP.NET MVC, NHibernate, Castle etc etc I feel home and (even better) I have the freedom to make experiments on the core engine too.

Since I joined I focused on looking at the implementation and adding the features I needed first. So what I was working on the past days:

  • Improve the import engine to support Wordpress (I’m very close to fix all that I need), you can see a live version of my blog using Dexter at this link: http://dexter.primordialcode.com/
  • Adding a better support for Windows Live Writer improving the different Metaweblog APIs support.
    I just added the hierarchical visualization of category (no more flattening out) and the ability to post the tags related to each post using WLW (no more need to add them at the body of the message).

Here’s a screenshot of the thing:

DexterLive

What I’m working on now:

  • Adding the ability to tie a single Post to more than a single category.
  • Improving the import routines.

All the changes are actually in my private Branch and will be merged to the main trunk once all the developers have reviewed them.


Asp net mvc, Blog engine, Dexter

2 comments

Passing JSON serialized objects to a WCF service with jQuery

Print Content | More

You can find a lot of posts on the web on the subject, why writing another one then? Simply because despite all the documentation I’ve already found on the web, while trying to put it in action I’ve spent a couple of hours in making that work, so I’ll use this post to recap all the action I made as a reference guide for my future implementations.

The scenario is: we have a WCF service that is able to accept call to methods with complex parameters (like Messages object in a simple web chat project) and we want to be able to use those methods with jQuery with the minimal impact on the service implementation.

WCF Service setup

We consider a very simple implementation, you don’t have to use any ‘strange’ or unusual attribute here:

The message class we are sending back and forth:

/// <summary>
/// a single message in a conversation
/// </summary>
[DataContract]
public class Message : Entity<int>
{
    /// <summary>
    /// the nickname of who is sending the message
    /// </summary>
    [DataMember]
    public virtual string Sender { get; set; }
 
    /// <summary>
    /// this will be assigned when the object get persisted into the storage, not before
    /// once assigned it's likely to not be modified again
    /// </summary>
    [DataMember]
    public virtual DateTime Timestamp { get; set; }
 
    [DataMember]
    public virtual string Text { get; set; }
}

A test service implementation

/// <summary>
/// service used to serialize/desierialize the data in JSon for JQuery
/// </summary>
[ServiceContract]
public interface IChatService
{
    /// <summary>
    /// return a simple message
    /// </summary>
    /// <returns></returns>
    [OperationContract]
    Message TestMessage();
 
    /// <summary>
    /// a test function that takes a message and returns it modified
    /// </summary>
    /// <param name="msg"></param>
    /// <returns></returns>
    [OperationContract]
    Message TestMessageModify(Message msg);
 
    /// <summary>
    /// a test function that get multiple parameters and return a message
    /// modified
    /// </summary>
    /// <param name="msg"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    [OperationContract]
    Message TestMessageModifyWithSuppliedContent(Message msg, int id);
}
[AspNetCompatibilityRequirements( RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed )]
public class ChatService : IChatService
{
    public Message TestMessage()
    {
        return new Message() { Id = 1, Sender = "Test", Text = "1 - Text message.", Timestamp = DateTime.Now };
    }
 
    public Message TestMessageModify(Message msg)
    {
        msg.Text = "2 - Modified message";
        msg.Timestamp = DateTime.Now;
        return msg;
    }
 
    public Message TestMessageModifyWithSuppliedContent(Message msg, int id)
    {
        msg.Text = "3 - Second parameter passed: " + id;
        msg.Timestamp = DateTime.Now;
        return msg;
    }
}

The only thing needed is the ‘AspNetCompatibilityRequirements’ attribute.

All the magic is done in the WCF configuration section:

   1: <system.serviceModel>
   2:     <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
   3:     <services>
   4:         <service name="RemoteAssistance.Services.Impl.ChatService"
   5:                  behaviorConfiguration="DebugJSonBehavior">
   6:             <endpoint address="" 
   7:                       binding="webHttpBinding" 
   8:                       behaviorConfiguration="DebugJSonBehavior"
   9:                       contract="RemoteAssistance.Services.IChatService">
  10:                 <identity>
  11:                     <dns value="localhost" />
  12:                 </identity>
  13:             </endpoint>
  14:             <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  15:         </service>
  16:     </services>
  17:     <behaviors>
  18:         <endpointBehaviors>
  19:             <behavior name="DebugJSonBehavior">
  20:                 <enableWebScript />
  21:             </behavior>
  22:         </endpointBehaviors>
  23:         <serviceBehaviors>
  24:             <behavior name="DebugJSonBehavior">
  25:                 <serviceMetadata httpGetEnabled="true" />
  26:                 <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
  27:             </behavior>
  28:         </serviceBehaviors>
  29:     </behaviors>
  30: </system.serviceModel>

To activate JSON serialization and deserialization for an end point we have to supply some information:

  • in line 7 we set the binding to ‘webHttpBinding’, this is the main actor the inject the JSON DataContract Serializer into the WCF stack.
  • in line 8 we set a specific endpoint behavior for this end point
  • lines 18-22 enable the endpoint behavior that makes it possible to consume the service from ASP.NET AJAX web pages.

Consuming the web service is the real ‘tricky’ point, calling a function without parameter is extremely easy, you can use code like this:

   1: $(document).ready(function() {
   2:     $.ajax({
   3:         type: "POST",
   4:         url: "http://localhost:58829/ChatService.svc/TestMessage",
   5:         data: "{}",
   6:         contentType: "application/json; charset=utf-8",
   7:         dataType: "json",
   8:         success: function(data) {
   9:             //debugger;
  10:             alert(data.d.Text);
  11:         },
  12:         error: function(XMLHttpRequest, textStatus, errorThrown) {
  13:                 debugger;
  14:            alert("Error Occured!");
  15:         }
  16:     });
  17: });

Things get a bit ‘complicated’ when you actually have to pass parameters (special tanks here go to my fellow Alkampfer for helping me find the solution). To pass a parameter successfully you have to supply as the data argument (line 5) a string that is the JSON representation of a JS object which properties matches the parameter name of the function you are calling, the parameters’ values are the actual values that will be sent to the server. If the parameter itself is a complex object (that is a class) you need to supply the JSON representation of the object, Let’s use an example to clarify:

if we want to call the TestMessageModify(Message msg) function we need to supply ad data parameter a JS object made like this:

   1: var msg2 = { "msg": { "Id": "2", "Sender": "Webpage", "Text": "Sended Text"} };

We’re defining an object that contains a ‘msg’ property (same name of the function parameter) which in turn contains another object (defined by the same properties that our Message object have, to allow JSON deserialization to work). What we need to pass to the ‘data’ argument of the function call is the string representation of object:

'{"msg":{"Id":"2","Sender":"Webpage","Text":"Sended Text"}}'

To have the JSON representation of JavaScript object we can use an external library like this one: http://www.JSON.org/json2.js

So, to call TestMessageModify(Message msg) and TestMessageModifyWithSuppliedContent(Message msg, int id), we can use the following code:

var msg2 = { "msg": { "Id": "2", "Sender": "Webpage", "Text": "Sended Text"} };
 
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "http://localhost:58829/ChatService.svc/TestMessageModify",
        data: JSON.stringify(msg2),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            alert(data.d.Text);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("Error Occured!");
        }
    });
});
 
var msg3 = { "msg": { "Id": "2", "Sender": "Webpage", "Text": "Sended Text"}, "id" : "1" };
 
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "http://localhost:58829/ChatService.svc/TestMessageModifyWithSuppliedContent",
        data: JSON.stringify(msg3),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(data) {
            alert(data.d.Text);
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            debugger;
            alert("Error Occured!");
        }
    });
});

It ended up being a quite long post for something that should have been much more simple.

Javascript, Jquery, Json, Wcf, Wcf service

4 comments

Castle Windsor enabling XML configuration files in Silverlight

Print Content | More

When a version of Castle Windsor able to run in Silverlight was released I started to play with it, basically because I have a lot of code that use it inside my line of business infrastructure framework. I’m working on a WPF/Silverlight solution and I’ve developed a framework that work in both the environments.

One of the biggest thing I miss in every ‘Silverlight-enables’ IoC system I tried is the ability to read and parse XML configuration files.

The current release does not allow Silverlight to parse XML configuration file, because all the parser is based on classes that are in the System.Xml namespace and that weren’t included in the Silverlight runtime.

The good news is Silverlight has a very good support for Linq To Xml, so I decided to do a ‘test of feasibility’ and switched the current implementation from using the XmlDom to Linq To Xml.

I have to admit it was more complex than I thought at start (mainly because I had to understand how the parser was written), here are the steps I followed:

  1. Download the ‘Inversion of control’ portion the Castle Windsor trunk an check the project to find the classes to modify: the good news is: all of them are inside a single folder ‘Castle.Windsor\Configuration\Interpreters’
  2. Start with the .Net solution and replace the usages of the ‘old’ XmlDom classes (XmlDocument, XmlNode, etc...) with the new one from System.Xml.Linq (XDocument, XElement, XNode...). Beware it’s not that easy!
    Both these systems act in slightly and sometime subtle different ways so I had to make ‘heavy’ modifications to all the class of the parser.
  3. Keep modifying and fixing the XmlInterpreter and all the other classes until all the tests for the .Net solution pass. At this stage the major modification were the introduction of a LinqToXmlConfigurationDeserializer to replace the standard one and a LinqExtensions class that contains extensions methods to mimic functionalities present in System.Xml.* classes and not directly exposed by Linq classes.
  4. Open up the Silverlight solution, include the new files and verifies it compiles...here some more minor modification were needed to exclude a couple of portions of code that really aren’t available in Silverlight.
    Good! It compiles!
  5. Let’s see an overview of all the files that were modified:
    CastleSilverlightInterpreters All the files we touched are inside the Configuration/Interpreters folders, no other section was modified.
  6. Let’s create a very simple Silverlight Test Application and check it on the ‘Field of Glory’:
    [TestClass]
    public class Tests
    {
        private string config = "<configuration><components>" +
                                "<component id=\"child\" " +
                                           "service=\"SilverlightTest.Domain.IChild, SilverlightTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" " +
                                           "type=\"SilverlightTest.Domain.Child, SilverlightTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" />" +
                                "<component id=\"parent\" " +
                                           "service=\"SilverlightTest.Domain.IParent, SilverlightTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" " +
                                           "type=\"SilverlightTest.Domain.Parent, SilverlightTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null\" />" +
                                "</components></configuration>";
     
        [TestMethod]
        public void ComponentRegistrationString()
        {
            WindsorContainer container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(config)));
            // resolve the object
            IParent p = container.Resolve<IParent>();
            Assert.IsNotNull(p);
            Assert.IsInstanceOfType(p, typeof(Parent));
            Assert.IsNotNull(p.ChildConstructorDependency);
            Assert.IsInstanceOfType(p.ChildConstructorDependency, typeof(Child));
            Assert.IsNotNull(p.ChildProperty);
            Assert.IsInstanceOfType(p.ChildProperty, typeof(Child));
        }
    }

and let’s run it:

CastleSilverlightXmlFileResult

Cool! It works: you can now feed up the configuration even from an XML file to the Silverlight version of Castle Windsor too (you can his file from a database a web service or download it directly..that’s up to you).

What’s left to do: as I said before this whole thing started as an experiment and the code is not clean, not optimized nor checked for memory leaks. Plus actually I modified the parser classes and ‘lost’ the previous implementation (I was lazy and I didn’t wanted to write some new tests...I just reused the old ones :D). Some more work is needed to create a full new implementation (parallel to the old one) and write some additional tests for it.

Actually I see only a potential problem and that is how to deal with configuration file inclusion: we should be able to inject an external handler that tells to the processor how to retrieve the resource (we cannot have access to physical files directly (except for those on the Isolated Storage)...so this feature still need some more thinking.

Plus the actual code it’s not very clean and contains a couple of hacks I’m not very proud of and would like to get rid of :D

I think that a good move can be to create a new ‘Castle.Windsor.Configuration’ project and move the configuration code there.

Here is the full source code of this sample if you want to download and try it, it contains the full trunk portion of the Inversion Of Control section in Castle Windsor:

I will submit this code to the Castle Dev Team and see what they think about it.


Castle windsor, Configuration, File, Silverlight, Xml

3 comments

Castle DynamicProxy - a dirty trick to call invocation Proceed() multiple times in an interceptor

Print Content | More

This is a typical scenario: you have a remote service (a database a web service...anything) which can have connection problems; obviously you don’t want your application to crash the desired behavior can be to retry the operation for a couple of times and then ask the user what to do (with a message box maybe).

Implementing this in Spring.Net is quite easy, here is some ugly code that build up a skeleton for the feature:

[System.CLSCompliant(false)]
public class ExceptionHandlerDaoAroundAdvice : IMethodInterceptor
{
    
    public object Invoke(IMethodInvocation invocation)
    {
        // endless loop to let the user retry the operation in case of db error
        
        int tries = 0;
        while (true) {
            try {
                tries += 1;
                
                object o = invocation.Proceed();
                return o;
            }
            catch (SqlClient.SqlException ex) {
                HandleDbException(ex, tries);
            }
            catch (NHibernate.ADOException ex) {
                HandleDbException(ex, tries);
            }
        }
    }
    
    // log the error and ask user if they want to retry, if not abort program
    private void HandleDbException(Exception ex, int tries)
    {
        // examine the exception and
        // in case of connection problems
        // use your session or connection manager to close any pending connection, thay are not valid anymore;
        if (tries < 4) {
            // let's wait a bit before retry
            Threading.Thread.Sleep(2000);
        }
        else {
            // todo: call here the service that have to ask the user what to do: wait more and retry,
            // contact the network administrator or close the application for example
        }
    }    
}

to wire it to a proxy generated around your object you can write something like:

ProxyFactory factory = new ProxyFactory(YOUROBJECT);
factory.AddAdvice(new ExceptionHandlerDaoAroundAdvice());
return factory.GetProxy();

But since I already use Castle Windsor as my IoC system and DynamicProxy for the lazy loading with NHibernate, in order to reduce the number of assemblies and external library my application uses, inspired by this great series of articles by Krzysztof Kozmic I decided to swap out Spring.Aop and use Castle’s Dynamic proxies.

The first implementation was more or less a plain translation of the code shown above and surprisingly I got an InvalidOperationException for calling invocation.Proceed() multiple times inside the Intercept() method of the interceptor.

To verify this I setup a simple test case with an adhoc interceptor that called invocation.Proceed() multiple times:

public interface ITestClass
{
    void TestMethod();
}
 
public class TestClass : ITestClass
{
    public void TestMethod()
    {
        Console.WriteLine("Test Method Called");
    }
}
 
public class FailingRetryInterceptor : IInterceptor
{
    #region IInterceptor Members
 
    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();
 
        // we want to call proceed multiple times to simulate maybe something
        // like retrying a connection to a database or a remote service
        // after having informed the user
        
        // let's try to call the proced again, we expect a big time
        // failure here cause the standard implementation does
        // not allow the proceed method to be called multiple times
 
        invocation.Proceed();
    }
 
    #endregion
}
 
[TestFixture]
public class InterceptorsTest
{
    readonly ProxyGenerator _generator = new ProxyGenerator();
 
    [Test]
    public void TestFailingRetryInterceptor()
    {
        Assert.Throws(
            typeof (InvalidOperationException),
            () =>
                {
                    ITestClass sut =
                        _generator.CreateInterfaceProxyWithTarget<ITestClass>(new TestClass(),
                                                                              new FailingRetryInterceptor());
                    sut.TestMethod();
                });
 
    }
}

If you run this test it passes, which means we get the InvalidOperationException. This operation is perfectly legal in Spring while it’s not allowed in Castle, to investigate the issue I got the source code for DynamicProxy and looked at the implementation of the Proceed() method inside the AbstractInvocation class:

   1: public void Proceed()
   2: {
   3:     if (interceptors == null)
   4:         // not yet fully initialized? probably, an intercepted method is called while we are being deserialized
   5:     {
   6:         InvokeMethodOnTarget();
   7:         return;
   8:     }
   9:  
  10:     execIndex++;
  11:  
  12:     if (execIndex == interceptors.Length)
  13:     {
  14:         InvokeMethodOnTarget();
  15:     }
  16:     else if (execIndex > interceptors.Length)
  17:     {
  18:         string interceptorsCount;
  19:         if (interceptors.Length > 1)
  20:         {
  21:             interceptorsCount = " each one of " + interceptors.Length + " interceptors";
  22:         }
  23:         else
  24:         {
  25:             interceptorsCount = " interceptor";
  26:         }
  27:  
  28:         var message = "This is a DynamicProxy2 error: invocation.Proceed() has been called more times than expected." +
  29:                       "This usually signifies a bug in the calling code. Make sure that" + interceptorsCount +
  30:                       " selected for this method '" + Method + "'" +
  31:                       "calls invocation.Proceed() at most once.";
  32:         throw new InvalidOperationException(message);
  33:     }
  34:     else
  35:     {
  36:         interceptors[execIndex].Intercept(this);
  37:     }
  38: }

Looking at the implementation is pretty easy to understand why we cannot call the proceed more than one time per interceptor: if we do the we increment the execIndex outside the bound of the interceptors array.

In my application I have full control over which interceptors get injected on each proxy, so a simple hack would be to allow me to reset the ‘execIndex’ variable to -1; doing this way the next time you call invocation.Proceed() from an interceptor the whole chain start from the beginning allowing you to recover and retry the operation.

I don’t like to modify the library code, cause at every update I have to reapply the path again, the Hack here is to use some reflection and write an extension method for the IInvocation interface:

public static class IInvocationExtensions
{
    private static FieldInfo _resetInvocationInterceptorsCall;
 
    public static void Reset(this IInvocation invocation)
    {
        if (_resetInvocationInterceptorsCall == null)
        {
            Type invoc = FindBaseType(invocation.GetType(), typeof(AbstractInvocation));
            if (invoc == null)
                throw new InvalidOperationException("IInvocationExtensions - Cannot find AbstractInvocation as base class.");
            _resetInvocationInterceptorsCall = invoc.GetField("execIndex", BindingFlags.Instance |
                                                                                              BindingFlags.NonPublic);
        }
        // reinitialize the index of execution, so when we call Proceed() again
        // the whole chain of interceptors start again from the first element
        _resetInvocationInterceptorsCall.SetValue(invocation, -1);
    }
 
    private static Type FindBaseType(Type src, Type lookingFor)
    {
        while (!(src == typeof(object)) && (src != lookingFor))
        {
            src = src.BaseType;
        }
        if (src == lookingFor)
            return src;
        return null;
    }
}

We can now update our test and write another interceptor that call the newly created Reset() method to allow us to recover and start the execution again:

public class SuccessRetryInterceptor : IInterceptor
{
    #region IInterceptor Members
 
    private bool firstcall = true;
 
    public void Intercept(IInvocation invocation)
    {
        invocation.Proceed();
 
        if (firstcall)
            {
                firstcall = false;
                // we want to call proceed multiple times to simulate maybe something
                // like retrying a connection to a database or a remote service
                // after having informed the user
 
                // we ask to reset the current call and start it again from the beginning of the
                // interceptors chain
                invocation.Reset();
 
                // let's try to call the proced again, we expect a big failure here cause the standard implementation does
                // not allow the proceed method to be called multiple times
                invocation.Proceed();
 
                // note that in this test we used the firstcall variable to stop the recursive calls
                // of this interceptor.
            }
    }
 
    #endregion
}
 
[TestFixture]
public class InterceptorsTest
{
    readonly ProxyGenerator _generator = new ProxyGenerator();
 
    [Test]
    public void TestSuccessRetryInterceptor()
    {
        Assert.DoesNotThrow(() =>
        {
            ITestClass sut = _generator.CreateInterfaceProxyWithTarget<ITestClass>(new TestClass(), new SuccessRetryInterceptor());
            sut.TestMethod();
        });
 
    }
}

If you execute this test you can see that it passes and that the TestMethod() of the test class gets called two times.

CastleInterceptorsProceed

BEWARE! This is a dangerous technique and you have to be absolutely aware of what you are doing, cause you are executing the whole interceptors’ chain from the beginning..so if some of your interceptors modify the data or perform some advanced operations you have to know exactly what they do in order to not alter/invalidate the data of your application.


Castle windsor, Dynamicproxy, Multiple, Proceed

0 comments