Solving a small bug in the bridges

As I”ve mentioned yesterday, I was having some problems with bridges. the problem was that every method call returned an error saying that a NullException was thrown. Well, after looking at the generated code from the parsing of the asbx file and doing some debugging, it was clear that the problem was happening in the protected method ConvertToType. The method is inherited from BridgeHandler and looks like this:

protected object ConvertToType(object argValue, Type paramType)
{
   string text1 = Assembly.CreateQualifiedName(“Microsoft.Web.Extensions”, “Microsoft.Web.Script.Serialization.ObjectConverter”);
     return Type.GetType(text1).GetMethod(“ConvertObjectToTypeInternal”, BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { argValue,
                                     paramType,    
                                     new JavaScriptSerializer() });
}

The method reuses the private ObjectConverter class introduced by the Extensions dll to perform the conversion. Initialy, I though that the problem was in the ConvertObjectToTypeInternal: i really thought that since the object I was returning back to the server had some null fields, it was causing the problems. In fact,I really think that the ConvertObjectToType method should be called instead since it performs a quick test of seeing if the current object is already of the desired type. Though the ConvertToType object is not virtual,I could “hide” it by using the new keyword and since the method would be defined in the same class that calls it (recall that the method call is performed from the class which results from the parsing of the asbx file”), then by adding that definition in the code behind file I should be able to intercept the call and redirect it to my method. So, I started by adding the following to the bridge class:

protected new object ConvertToType(object argValue, Type paramType)
{
   string text1 = Assembly.CreateQualifiedName(“Microsoft.Web.Extensions”, “Microsoft.Web.Script.Serialization.ObjectConverter”);
     return Type.GetType(text1).GetMethod(“ConvertObjectToType”, BindingFlags.NonPublic | BindingFlags.Static).Invoke(null, new object[] { argValue,
                                     paramType,    
                                     new JavaScriptSerializer() });
}

Well, i was relaly convinced that it would work…but, it didn”t! damn…however, now i could debug it and fond out exactly where I was getting the exception. See, the problem is that Type.GetType is returning null! Why is that? Well, I”m not a reflection guru but I think that the problem was that the dll is on the GAC and we need to write its full name. So, changing the previous method to this:

protected new object ConvertToType(object argValue, Type paramType)
{
  string text1 = “Microsoft.Web.Script.Serialization.ObjectConverter, Microsoft.Web.Extensions,
     Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35″;
  return Type.GetType(text1).GetMethod(“ConvertObjectToType”, BindingFlags.NonPublic |
     BindingFlags.Static).Invoke(null, new object[] { argValue, paramType, new JavaScriptSerializer() });
}

~ by Luis Abreu on November 9, 2006.

4 Responses to “Solving a small bug in the bridges”

  1. Hi Luis,

    Thanks for the informative post. I posted the following question to your response on the asp.net forum:

    Very interesting… how did you override it? The BridgeHander is generated at compile time, right? Are you editing the generated class in Temporary ASP.NET Files, or doing it some other way?

  2. hello.
    sorry, i”ve lost the frame id so i can”t reply on the forums. well, simple add the method on the partial class maintained in the asbx.cs file

  3. Hmm. We are using the Website Application Project rather than the Atlas Website Project. I do not see an an accompanying .cs file for our .asbx files at all… I was only able to find this file by getting the stack trace from javascript, and finding the generated class in our Temporary ASP.NET Files folder. Is this something you could/should manually add? BTW, forum link here: http://forums.asp.net/2/1459049/ShowThread.aspx#1459049

  4. hello.
    when you create the asbx file you can also add a codebehind file. can”t you do that in the web app project?

Leave a comment