EntityPropertyMappingAttribute duplicated between assemblies

I was working on an entity class for an OData endpoint when I ran across the following doozy:

The type ‘System.Data.Services.Common.EntityPropertyMappingAttribute’ exists in both ‘…Microsoft.Data.OData.dll’ and ‘…System.Data.Services.Client.dll’

It looks like Microsoft has duplicated this type (plus a couple of others) between two different assemblies – in this instance I ran across it with the Azure.Storage package.

Thankfully, Jon Skeet to the rescue! To resolve:

  1. Select the System.Data.Services.Client reference and open the properties dialog
  2. Under ‘Aliases’, change ‘global’ to ‘global,SystemDataServicesClient’
  3. Add the following code at the top of the offending entity file:
extern alias SystemDataServicesClient;
using SystemDataServicesClient::System.Data.Services.Common;

You’ll also need to delete your other using System.Data.Services.Common, but at that point you should be compiling again.

Advertisements

Google Analytics Razor Helper

I experimented today with the Razor helper syntax to consolidate generation of the Google Analytics tracking boilerplate (in ~/App_Code/Helpers.cshtml):

@helper GoogleAnalyticsTracker(string analyticsId) 
{
   if (!String.IsNullOrEmpty(analyticsId))
   {
   <script type="text/javascript">
            var _gaq = _gaq || [];
            _gaq.push(['_setAccount', '@analyticsId']);
            _gaq.push(['_trackPageview']);

            (function () {
                var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
                ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
                var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
            })();
    </script>
    }
}

which can then be used in your layout pages with:

@Helpers.GoogleAnalyticsTracker(Model.GoogleAnalyticsId)

It’s a bit unclear where this mechanism should fit between custom HtmlHelper extension methods and Razor partial views. I guess you’d be looking for functionality that’s less generic than an HtmlHelper extension – you can modify without recompiling, and it’s less simple to share between projects – and smaller/more generic than a partial.

The App_Code requirement for sharing between pages is not ideal, particularly as HtmlHelper extension methods aren’t available without workarounds. Hopefully the team get an update out in a reasonably timeframe.