ASpeak IOpine!

First encounter with OData

Posted on May 3, 2010

After installing VS2010 Professional I could not resist my temptation to start spelunking the newer features available to me. I have seen a couple of videos about OData in Mix10 and I thought it would be the easiest to begin with. I didn’t know the very first encounter with creature of this type would demystify a bug in it. I opened up my solution which comprises of various test projects. I added a new Console Application Project targeting the DotNET4ClientFramework. I added reference to the Mix10Live OData Feed and followed as was demonstrated by presenters in the mix video.

Following is the code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ODataTest.Mix10Live;
 
namespace ODataTest
{
    class Program
    {
        static void Main(string[] args)
        {
            EventEntities eventEntities = new EventEntities(new Uri("http://api.visitmix.com/OData.svc"));
            try
            {
                var query = eventEntities.Speakers.Where(x => x.Sessions.Count > 0).Take(10);
                query.ToList().ForEach(x =>
                    {
                        Console.WriteLine(new String('-', 10));
                        Console.WriteLine(x.SpeakerDisplayName);
                        Console.WriteLine(x.Bio);
                        x.Sessions.ToList().ForEach(y => Console.WriteLine("Session: {0}", y.Title));
                        Console.WriteLine(new String('-', 10));
                    });
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }
    }
}

 

The LINQ Query generates following URL:

http://api.visitmix.com/OData.svc/Speakers()?$filter=Sessions/Count gt 0&$top=10

Following the URL leads to an xml output which gives following error

An error occurred while processing this request. Property 'System.Nullable`1[System.DateTime] PublishedDate' is not defined for type 'System.Data.Objects.DataClasses.EntityCollection`1[api.visitmix.com.Session]' System.ArgumentException at System.Linq.Expressions.Expression.Property(Expression expression, PropertyInfo property) at System.Linq.Expressions.Expression.MakeMemberAccess(Expression expression, MemberInfo member) at System.Data.Services.ExpressionVisitor.VisitMemberAccess(MemberExpression m) at System.Data.Services.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.ExpressionVisitor.VisitBinary(BinaryExpression b) at System.Data.Services.ExpressionVisitor.Visit(Expression exp) at System.Data.Services.RequestQueryProcessor.ComposePropertyNavigation(Expression expression, LambdaExpression filterLambda, Boolean propagateNull) at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseMemberAccess(Expression instance) at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseIdentifier() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParsePrimaryStart(ExpressionLexer l) at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParsePrimary() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseUnary() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseMultiplicative() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseAdditive() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseComparison() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseLogicalAnd() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseLogicalOr() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseExpression() at System.Data.Services.Parsing.RequestQueryParser.ExpressionParser.ParseWhere() at System.Data.Services.Parsing.RequestQueryParser.ParseLambdaForWhere(IDataService service, ResourceSetWrapper setForIt, ResourceType typeForIt, Type queryElementType, String expression) at System.Data.Services.Parsing.RequestQueryParser.Where(IDataService service, ResourceSetWrapper setForIt, ResourceType typeForIt, IQueryable source, String predicate) at System.Data.Services.RequestQueryProcessor.ProcessFilter() at System.Data.Services.RequestQueryProcessor.ProcessQuery() at System.Data.Services.RequestQueryProcessor.ProcessQuery(IDataService service, RequestDescription description) at System.Data.Services.RequestUriProcessor.ProcessRequestUri(Uri absoluteRequestUri, IDataService service) at System.Data.Services.DataService`1.ProcessIncomingRequestUri() at System.Data.Services.DataService`1.HandleRequest()

This looks strange as I did not use PublishedDate in my code and the service reference was generated entirely by the IDE. On Twitter, I was told that the “Count” filter is invalid. Now, since the query is generated by Linq2OData, the onus is on either OData or on Linq2OData to provide compatible query or generate error while generating the query itself.

I played around a little more and modified the LINQ query as follows

var query = eventEntities.Speakers.Where(x => !String.IsNullOrWhiteSpace(x.Twitter)).Take(10);

This time it gives me following error

{Error translating Linq expression to URI: The expression Not(IsNullOrWhiteSpace([10007].Twitter)) is not supported.}

So, this is established that the LINQ does validate the linq expression before generating the OData uri and also, not all LINQ operators are compatible with OData operators.

I would like to know where the bug resides, in OData or LINQ.

Tagged as: , , No Comments

Debug installer

Posted on June 17, 2009

The installer created using WIX was failing. I had no option but to perform following on command line:

msiexec -i MYSETUPFILE.msi /leavx log.txt

This will create a log file for the installer when this command is invoked. The failure sequence can be traced from the log file and is relatively simpler to debug the setup issue.

Forcefully delete an assembly from GAC

Posted on April 17, 2009

image 

Very often during development we try to remove an assembly from GAC. However, due to many reasons the assembly might not get deleted and the above screen is displayed. There is no easy way to remove this assembly as it may be locked due to numerous reasons and one might need to break his head identifying the proper reason behind this.

However, there is an easy solution to this problem.

Open “regedit” and navigate to “HKEY_CLASSES_ROOT\Installer\Assemblies\Global”. In this location one can find the list of all the assemblies which are registered in GAC. Delete the appropriate entry from this list. Now you can delete the assembly from GAC as well. This is a simple trick to get rid of that stubborn assembly which was stuck in your GAC.

I hope this simple trick will save many man hours ! :)

Resize Modal Dialog Box dynamically

Posted on April 2, 2009

This has been a pesky issue in our current project. The requirement is to pop a modal dialog box from a web form. This dialog that appears will contain various user controls based on certain conditions. It may have controls like textbox, tree view, list view, combo box and many others. User will have to select or provide appropriate values in the required fields in this box.

The problem was that the size of the dialog box can not be predetermined and must be generated based on the content of the window. Unlike windows, in web forms we can not generate the pixel sizes of each control and the sum up the vertical space acquired by it and assign that size to the window. Even if it exists I am not aware of it and it might take good amount of time for each form display for each and every user which would be a performance lag. I found a very elegant solution to resize the window as per its content dynamically after the loading of the window. Following is the code snippet for the same:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmDyna.aspx.cs" Inherits="frmDyna" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<script language="javascript" type="text/javascript">
    function PerformResize() {
        ht = (document.body.scrollHeight + 32) + 'px'
        wt = (document.body.scrollWidth + 16) + 'px'
        window.dialogHeight = ht;
        window.dialogWidth = wt;
 
    }
</script>
<body onLoad="PerformResize();">
    <form id="form1" runat="server">
    <asp:Panel ID="Panel1" runat="server" BorderStyle="Dotted" 
        Direction="LeftToRight" Wrap="False">    
    </asp:Panel>
    </form>
</body>
</html>

The caller would use following syntax to call the above form as a modal dialog:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
<script language="javascript" type="text/javascript">
    function OpenChild() 
    {
        var WinSettings = "center:yes;resizable:yes;scroll:1;status:0";
        var MyArgs = window.showModalDialog("frmDyna.aspx", null, WinSettings);        
    }
</script>
 
    <form id="form1" runat="server">
    <asp:Panel ID="pnlDyna" runat="server">
        <Button ID="Button1" onclick="OpenChild()"> Button1 </Button>                        
    </asp:Panel>
    </form>
</body>
</html>
Filed under: ASP.NET, DotNET 3 Comments

Lambda based reflection Vs Normal reflection Vs Direct call

Posted on March 25, 2009

One friend of mine sent me the following code sample that really surprised me.

There are three different approaches being used to call a method from a different class. The approaches are -

  1. Direct Call
  2. Call through Lambda based reflection
  3. Call through Normal Reflection

It was really surprising to see the execution speed comparison between three different approaches. The lambda based approach was found to be the fastest by the order of 1000 times.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Reflection;
 
namespace LambdaReflection
{
 
    class MyClass
    {
        public void CallIt()
        {
        }
    }
 
    class Program
    {
        delegate void LocalDel<t>(T mc);
        static void Main(string[] args)
        {
            System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
            MethodInfo minfo = typeof(MyClass).GetMethod("CallIt", BindingFlags.Instance | BindingFlags.Public);
            MyClass obj = new MyClass();
 
            //compile expression tree
            LocalDel<myclass> lambdaExpression = FuncCallIt<myclass>("CallIt");
            sw.Reset();
            sw.Start();
 
            //Direct call by using object
            for (int a = 0; a &lt; 10000; a++)
            {
                obj.CallIt();
            }
            sw.Stop();
            Console.WriteLine(string.Format("Time taken by Direct call {0}", sw.ElapsedTicks));
            sw.Reset();
            sw.Start();
 
            //dynamically compiled lambda call
            for (int a = 0; a &lt; 10000; a++)
            {
                lambdaExpression(obj);
            }
            sw.Stop();
            Console.WriteLine(string.Format("Time taken by lambda-expression compilation {0}", sw.ElapsedTicks));
            sw.Reset();
            sw.Start();
 
            //reflection call
            for (int a = 0; a &lt; 10000; a++)
            {
                minfo.Invoke(obj, null);
            }
            sw.Stop();
            Console.WriteLine(string.Format("Time taken by Reflection call {0}", sw.ElapsedTicks));
            Console.ReadLine();
 
        }
        private static LocalDel<t> FuncCallIt<t>(string MethodName)
        {
            var target = Expression.Parameter(typeof(T), "a");
            MethodInfo minfo = typeof(T).GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Public);
            var methodinvokeExpression = Expression.Call(target, minfo);
            var lambda = Expression.Lambda<localdel><t>&gt;(methodinvokeExpression, new ParameterExpression[] { target });
            return lambda.Compile();
        }
    }
}

Following is the result of the execution on my system:

Time taken by Direct call 2384625

Time taken by lambda-expression compilation 902430

Time taken by Reflection call 518534265

I didn’t expect lambda based call to be faster than direct call but I need to explore as how best I can make use of this knowledge in my real programming scenarios. The compiled lambda demands different compiled lambda expressions for each method being used. However, if performance is the prime objective in the application then I think this tangential approach is worth considering.

What is your opinion on it ?