Using Google Gears to find Montreal's Bus Stops

By Jay at August 31, 2008 20:08 Tags: , ,

Cet article est également disponible en francais ici.

It's been a while since I've posted on this blog. This time, I will not be talking about bluetooth, but still about some .NET powered code :)

I've been busy lately, but I've found some time to work on something that will help me a lot, and I think a lot of Windows Mobile users and mobile users in general.

Montreal's Bus network is somehow large, but its representation in the digital world is quite poor, and inexistent when talking about mobile internet. The web site in question is generating some quite large pages and is not suited for mobile web browsing.

Most of the time, you may want to know the schedule of the next bus, and this is quite hard to get this way.

There's been some effort lately to offer this kind of service on the iPhone, but I wanted to give the opportonity to other users to have the same information, with some Geo Localization features.

This is where google gears comes into action, where their latest release offers a Geo-Location API, which approximates a position using the nearest GSM cells location. Unfortunately, it only works on Windows Mobile devices. But don't worry, if you don't have a Gears enable device, it will still work ! You'll only have to type a bit, by entering your streets intersection.

After getting that location, I'm querying a database (using Linq to SQL) to get the nearest Bus Stops and their next schedule. I'm also querying Google Maps to get some markers pointing at the bus stops. That can be helpful since the Geo-Location is only an approximation by nature, because of the GSM 'triangulation'. It can also be used to query the schedule of a specific bus stop, using the number placed at the bottom of the bus stop signs. A small plus here, compared to the original site, is that schedules from the past half hour are still visible, making possible to have determine if a bus has missed its schedule using a great long street.

Anyway, if you're in Montreal and have an internet connected device (or a normal PC), give it a try by connecting to this adress : http://jaylee.org/stm

Any comments or suggestions are welcome !

ILogicalThreadAffinative, again.

By Jay at May 14, 2008 19:12 Tags: , ,

Ce post est également disponible en francais. 

In a previous post, I talked about a feature of the .NET framework that allows to automatically pass information from a thread to any thread it spawns.

It turns out that not only the Call Context flows to the thread pool, but it also flows to any System.Threading.Timer and to IO completion related threads, such as in Sockets.

I was taken off-guard by the fact that in early stages of the server-side remoting pipeline, there were already information in the CallContext before the incoming message was deserialized. But the content was not exactly what I was expecting; it was the data that was in the CallContext when RemotingConfiguration.Configure() was invoked.

This makes sense, all the sockets used by TcpChannel or HttpChannel are created when calling this method, and asynchronous operations are started at this time. So, to avoid having the data to flow to the other side, there are these two methods on the ExecutionContext class to Suppress the flow and Restore it, which can prevent temporarily the Call Context from flowing.

IEnumerable<T>.Any() vs. IEnumerable<T>.Count() != 0

By Jay at May 10, 2008 11:10 Tags: , ,

This post is also available in french here. 

After reading this post from Eric Lippert, and reminding me that in the samples for this post, I'm using IEnumerable<T>.Count() where I'm actually not using the return value, therefore I'm enumerating through the whole collection for almost nothing.

I could have used IEnumerable<T>.Any(), which after looking at the IL code, just starts the enumeration, and stops just after one element if there is one and returns true, or if there isn't, returns false.

This is more effective, even without PLinq :)

A look at Linq to objects and the "let" keyword

By Jerome at April 26, 2008 13:08 Tags: , ,

Cet article est aussi disponible en français ici. 

I've had some time lately to use LINQ a bit more intensively and in particular to use the let keyword.

I had to process a lot of XML files placed in multiple folders, and I wanted to filter them using a regex. First, here's how to get all files from a directory tree :



  var files = from dir in Directory.GetDirectories(rootPath, SearchOption.AllDirectories)
              from file in Directory.GetFiles("", "*.*")
              select new { Path = dir, File = Path.GetFileName(file) };

At this point, I could have omitted the Directory.GetDirectories call because GetFiles can also search recursively. But since the GetFiles method only returns a string array and not an enumerator, it means that all my files would have been returned in one array, which is not memory effective. I'd rather have an iterator based implementation of GetDirectories and GetFiles for that matter, but the finest grained enumeration can only be done this way...

Anyway, having all my files, I now wanted to filter the collection with a specific Regex, as my legitimate files need to observe a specific pattern. So, I updated my query to this :



    Regex match = new Regex(@"(?<value>\d{4}).xml");
    var files2 = from dir in Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories)
                 from file in Directory.GetFiles(dir, "*.xml")
                 let r = match.Match(Path.GetFileName(file))
                 where r.Success
                 select new {
                    Path = dir,
                    File = Path.GetFileName(file),
                    Value = r.Groups["value"].Value
                 };


This time, I've introduced the let keyword. This keyword is very interesting because it allows the creation of a query-local variable that can contain either collections or single objects. The content of this variable can be used in the where clause, as the source of another "from" query, or in the select statement.

In my case, I just wanted to have the result of the Regex match, so I'm just calling Regex.Match to validate the file name, and I'm placing the content of a Regex group in my resulting anonymous type.

Now, with all my files filtered I found that some XML files were not valid because they were not containing a specific node. So I filtered them again using this query :



    var files2 = from dir in Directory.GetDirectories(args[0], "*", SearchOption.AllDirectories)
                 from file in Directory.GetFiles(dir, "*.xml")
                 let r = match.Match(Path.GetFileName(file))
                 let c = XElement.Load(file).XPathSelectElements("//dummy")
                 where r.Success && c.Count() != 0
                 select new {
                    Path = dir,
                    File = Path.GetFileName(file),
                    Value = r.Groups["value"].Value
                 };

I've added a new let clause to add the loading of the file, and make sure there is a node named "dummy" somewhere in the xml document. By the way, if you're looking XPath in XLinq, just look over there.

You may wonder by looking at this query when the load is actually evaluated... Well, it is only evaluated when the c.Count() call is performed, which is only after the regex has matched the file ! This way, I'm not trying to load all the files returned by GetFiles. You need to always remember that queries are evaluated only when enumerated.

In conclusion, Linq is a very interesting piece of technology, definitely NOT reserved to querying databases. What I like the most is that one can write code almost without any loop, therefore reducing side effects.

If you haven't looking at Linq yet, just give it a try, you'll probably like it :)

A bug in VS2008 Code Analysis, Generics normal and nested classes

By Jerome at April 07, 2008 19:12 Tags: , ,

I've found a few days ago a small bug in the former "FxCop" now renamed Code Analysis part of Visual Studio 2008.

While compiling this little piece of code :


public class Dummy<T> where T : IDisposable
    {
        public T Test
        {
            set
            {
                new NestedDummy<T>(default(T));
            }
        }

        class NestedDummy<U>
        {
            public NestedDummy(U item)
            {
                this.Value = item;
            }

            public U Value { get; private set; }
        }
    }

Which is a trimmed down version of the actual code, I saw a lot of errors like this :

MSBUILD : error : CA0001 : Rule=Microsoft.Reliability#CA2001, Target=ConsoleApplication1.Dummy`1+NestedDummy`1.#.ctor(!1) : The following error was encountered while reading module 'ConsoleApplication1': Could not resolve member reference: ConsoleApplication1.Dummy`1<type parameter.T>+NestedDummy`1<type parameter.U>::set_Value.

This means that for some reason, the Code Analysis tool is unable to parse the metadata to check for some analysis rule. This is not a blocking bug since it does not prevent the build from ending properly, but it displays a lot of error messages, which can be disturbing.

To fix to, I found two solutions : Either move the nested class out of its parents class, or remove the generic constraint on the parent class.

I posted the bug on Microsoft Connect, and I was pleasantly surprised to see that it has already been processed and David Kean from Microsoft wrote that the fix will be available in the next Service Pack of Visual Studio 2008.

Not a big issue but still, nice to see that Connect has an impact.

Prevent ASP.NET web.config inheritance, and inheritInChildApplications attribute

By Jerome at March 23, 2008 12:34 Tags: , ,

Since I've changed my top level blog engine, I've had some troubles with YAF, the forum engine I'm using for my Remote Control software.

The forum I'm using is in a child directory, which is a child application defined in IIS as an other application. The BlogEngine.NET disables the use of Sessions, and YAF requires sessions to be enabled, plus BlogEngine.NET adds some custom HTTP handlers, which incidentally are not known but the forum application. This is quite a mess, and to be able to get both applications running without fine tuning each one to work with the other, I had to use the little known attribute inheritInChildApplications.

This attribute prevents an application from passing its configuration as a default to child applications. Using this attribute is a little tricky, and has to be used this way :


<!-- Root web.config file -->

<?xml version="1.0"?>

<configuration>

  <location path="." inheritInChildApplications="false">

    <system.web>

      <compilation debug="false" />

      <!-- other configuration attributes -->

    </system.web>

  </location>

</configuration>

This way, any child application defined below this application will not use the current configuration. There's some mystery around the inheritInChildApplications attribute; it is not defined in the dotnetconfig.xsd file and it still is a rather helpful configuration option...

Visual Studio 2008 Solution Tree Items Collapse

By Jerome at March 17, 2008 19:26 Tags: ,

Even though there is a way to expand all nodes in the solution tree of Visual Studio 2008, there is no way to do the opposite, which is collapse all. Not collapse all top level nodes, but collapse all child nodes, one by one.

There's a bug in VS2005/2008 that prevents a node from being collapsed properly for some obscure reason. The Expanded property keeps on being "true" even if set to "false".

Fortunately, a fix by Scott Kuhl which was working with Visual Studio 2005 is also working with Visual Studio 2008. The script is doing some trick to simulate a DefaultAction each node, which seems to collapse a node without using the Expanded property. 

Nice trick :) It avoids me the burden of hitting the minus and enter keys numerous times...

Blog Update

By Jerome at March 16, 2008 17:54 Tags: , ,

Last Monday, I attended a GUVSM meeting with Rod Paddock and it was a great presentation. Rod is an interesting speaker, and he seems to have some sort of an independant view on Microsoft technologies. I'm also joining Guy Barette to thank Rod for his long trip visit in Montréal !

He's been talking about MIX'08 and all the Silverlight buzz. He was also here to talk about 10 Open Source tools worth looking at, and he's mentionned BlogEngine.NET. This blog engine kept my attention, not because it's particularly easy to install, but because it's able to use an XML datasource to store its data, that it has a pretty small foot print and that it is extensible.

Back at home, I gave it a look, and decided to switch to this engine. There's a bunch of features that can be a pain to develop and stabilize, like PingBack and TrackBack protocols. It's nice to have it out of the box.

I'm just hoping that it is going to increase the visibility of this blog. We'll see :)

.NET Threads, CallContext and ILogicalThreadAffinative

By Jerome at February 10, 2008 21:53 Tags: , ,

I've recently been looking for a way to automatically pass information from a thread's current call context to any thread that's been spawned from this thread. This can be useful for many reasons, and sometimes having some TLS information like the current user, or some custom context information.

This can be done by wrapping any thread entry point around some custom code that will effectively pass the context to the new thread. This is a bit annoying, especially if this is information that is generated internally by a framework, because it requires the user of the framework to always use the wrapping method.

There's a way around this by using the CallContext class, and particularly GetData/SetData methods. Problem is, if you set some data in the CallContext, it will not pass onto a spawned thread. Actually, it will if the type you are placing in the CallContext implements the ILogicalThreadAffinative interface.

This is a marker interface that is used to avoid context data that is not meant to "flow" through each spawned thread.

It's also interesting to know that ILogicalThreadAffinative flagged types will also be passed along to threads spawned by the thread pool, and incidentally to delegates enqueued via the BeginInvoke compiler generated method.

Finally, in case of a remoting call, any ILogicalThreadAffinative flagged type will also serialized to the remote context and be serialized back to the local context.

Being able to step through the Framework's code has been somehow a time saver to better understand this :)

Visual Studio 2008 Presentation in Montréal

By Jerome at December 11, 2007 21:46 Tags: ,

This last Monday, there's been a presentation for the GUVSM of Visual Studio 2008 at Microsoft Montréal by Guy Barrette, a Microsoft Regional Directory. The room was a bit crowded but there was a pretty good ambience. This was my first time there and I met a former distant colleague of mine from Winwise, Laurent Duveau.

On the menu, an overview of only the tooling that can be found in Visual Studio 2008. Not that C# 3.0 and .NET 3.5 are not interesting, far from it, but this will probably be the point of another session.

Multi Targeting

VS2005 forced the migration to .NET 2.0 when coming from VS2003. VS2008 does not enforce the migration to .NET 3.5, and leaves a choice of targeting .NET 2.0, 3.0 and 3.5. Solution and Project files don't change much, which means a smoother transition from VS2005 to VS2008. Some developers can run VS2005 and some others VS2008 without any difficulties. The only downside about the multi-targeting is that Microsoft dropped the support of .NET CF 1.0 and forces the conversion of projects to .NET CF 2.0.
The target framework can also be changed afterward, upgrade or downgrade. At that point, this is a good feature but if you have a solution that contains 100+ projects, this can be a repetitive task to migrate each and every one of them. I'll discuss that in an other post, but I wrote a small tool that converts every csproj file it can find to a .NET 3.5 project. That gave me the opportunity to test XLinq, by the way :)

VSTO (Visual Studio Tools for Office)

There is now no more the need to install add-ins to create VSTO addins, support for Office 2003 and 2007 is now included out of the box. Guy showed us how to create a simple plugin that creates a task pane which opens via a custom Excel Ribbon button, fills up from a database, and then fills the selected cell with some info. Pretty interesting stuff and easy to setup. It seems that now the deployment of VSTO addins has been greatly enhanced, compared to what could be done with VSTO and VS2005.

WCF

WCF now has an integrated webservice testing tool, that allows to call your WCF webservice without having to create the little testing project in Console that calls the webservice to test it. I'm guessing that it'll be useful only in the first few minutes of the webservice development, but after that... Maybe for the demos :)

Javascript

Javascript has a better support, especially for debugging. It is now possible to step into methods defined in javascript in the actual files, not in their memory representation. It is also possible to have custom help for javascript methods ! This is a hack using xml comments like in the ones found in C#, but that's helpful. I kind of hate the javascript development partly for the lack of debugging features and intellisense, but with this, I might hate it a little bit less...

SQL Server CE

It is a port of the SQL CE engine found on Mobile Devices. It's a small database engine similar to Jet could have been back in its days, but is compatible with SQL server to some extents. There's no stored procedures, no security, but it's a cheap way to store data instead of maybe using XML files to store data.

Database Cache

which is a out of the box component that is able to create a snapshot of the database, and handle the synchronization of the snapshot later in time.

Mobile Development

And finally a few words on mobile side, for which it is now possible to test security scenarios, with third party executable signature enabled, for instance.

This was a pretty rich session, with too much to say probably, but I learnt a few things along the way. I'll definitely attend the next one in january !

About me

My name is Jerome Laban, I am a Software Architect, C# MVP and .NET enthustiast from Montréal, QC. You will find my blog on this site, where I'm adding my thoughts on current events, or the things I'm working on, such as the Remote Control for Windows Phone.