Globalization, Localization And Why My Application Stopped Launching

When I was localizing a Windows Phone application I was developing, I set the argument on the constructor of the AssemblyCultureAttribute for the neutral culture (en-US in this particular case) for my application.

As it was late at night (or early in the dawn Smile) I went to sleep and, on the next day, the application wasn’t launching although it compiled just fine.

I’ll have to confess that it took me a couple of nights to figure out what I had done to my application.

Have you figured out what I did wrong?

The documentation for the AssemblyCultureAttribute states that:

The attribute is used by compilers to distinguish between a main assembly and a satellite assembly. A main assembly contains code and the neutral culture's resources. A satellite assembly contains only resources for a particular culture, as in [assembly:AssemblyCultureAttribute("de")]. Putting this attribute on an assembly and using something other than the empty string ("") for the culture name will make this assembly look like a satellite assembly, rather than a main assembly that contains executable code. Labeling a traditional code library with this attribute will break it, because no other code will be able to find the library's entry points at runtime.

So, what I did was marking the once main assembly as a satellite assembly for the en-US culture which made it impossible to find its entry point.

To set the the neutral culture for the assembly resources I should haveused (and eventually did) the NeutralResourcesLanguageAttribute. According to its documentation:

The NeutralResourcesLanguageAttribute attribute informs the ResourceManager of the application's default culture, and also informs the ResourceManager that the default culture's resources are found in the main application assembly. When looking up resources in the same culture as the default culture, the ResourceManager automatically uses the resources located in the main assembly instead of searching for a satellite assembly. This improves lookup performance for the first resource you load, and can reduce your working set.

Patterns & Practices Symposium Online – April 24, 2012

patterns & practices Symposium Online, April 24, 2012

Microsoft patterns & practices symposiums are held regularly in the U.S. and abroad. Most of the speakers are from the Microsoft patterns & practices (p&p) team. The event is aimed primarily at software architects, developers, and technical managers. patterns & practices develops applied engineering guidelines to help software developers and architects build great solutions on the Microsoft platform.

Read more…

Announcing The Microsoft Roslyn CTP

The Roslyn team has announced general availability of the Roslyn CTP!

The official launch is at http://msdn.com/roslyn, and there were a number of blogs to publicize the availability broadly (soma, ericli, vsteam, vbteam, c#faq) and across twitter.

This release marks a significant step to the new way of thinking of compilers, and the agility that is now possible with language innovation, IDE tooling, and powering the ecosystem. The C# and VB compilers are no longer black boxes – something we put source text into, do some magic on, and get an assembly out.  All that rich information about code is no longer thrown away, but is now exposed as a full-fidelity object model that can be easily consumed by all.  In addition, it was released a preview of the first-ever Interactive window for C# that contains full IDE support – including IntelliSense and even automatically detecting missing using directives.

How to get started:

  • Download the CTP.  The CTP installs on Visual Studio 2010 SP1 and can be safely installed side-by-side with Visual Studio 11.
  • Go to Start -> All Programs ->  Microsoft Codename Roslyn CTP -> Getting Started to launch the entry point into all the documentation, samples, and tools.
  • Read the Roslyn Project Overview for a good overview of the project.
  • Learn from the rich samples included (paste as C#/VB, refactorings, code analysis, and code generation tools).
  • Run the walkthroughs to learn about the Compiler APIs, the Services API, or using the Interactive window.
  • For those of you that aren’t extension writers, download the CTP to try out the Interactive window and use the Copy Paste C#/VB extensions that were built to help with your daily work now!

The release includes the following features:

  • Visual Studio Project Templates
    These project templates help you get started using the Roslyn APIs and building new Visual Studio extensions using the C# or VB APIs.
  • Reference Assemblies
    The Roslyn assemblies can be added to projects via the Add Reference dialog.
  • Interactive Window
    A new tool window called C# Interactive is available in Visual Studio by invoking View -> Other Windows -> C# Interactive from the menu. You can explore by either executing snippets of code in the C# Interactive tool window, or cumulatively building up execution context as you experiment.
  • Script File Editing Support
    C# Script (.csx) files allow top-level statements much like the C# Interactive window. You can create a new C# Script file by invoking File -> New File -> Script -> Visual C# Script from the Visual Studio menu. In addition to typing directly into the tool window, you can also select code in C# and C# Script (.csx) files and invoke "Execute in Interactive" or "Copy to Interactive" from the context menu. C# Script editing features like IntelliSense are powered by the Roslyn Language Service.

Please keep in mind that this is only a technology preview, and it’s not done yet! The primary goal of this CTP is to gather feedback on the public APIs and give an early look at the Interactive window feature. The shape of the APIs are in a fairly stable state, especially the Compiler ones, but there are still a set of known limitations and only a subset of the C# and Visual Basic languages are implemented in the current release.  For a full list of non-implemented language features, see here. The Interactive window is only available for C# at this time, but VB is following shortly.

The Roslyn team looks forward to hearing your feedback on the forums e through Connect.

What Is The .NET Framework 4.5?

According to the .NET Blog, .NET Framework 4.5 is an in-place update that replaces .NET Framework 4.0 (rather than a side-by-side installation). The goal is for .NET 4.5 to be fully backward compatible with applications built for .NET 4.0 (.NET 3.5 and .NET 4.5 will be side-by-side).

One of the first things you’ll notice about .NET 4.5 is the version number of the CLR (4.0.30319) is the same as .NET 4.0; this is the practice used by other in-place updates. (read more)

In fact, if you compare a system with .NET 4.5 with a system with .NET 4.0 you’ll see that 4.5 is just an update to 4.0:

Registry key: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full
Value .NET 4.0 .NET 4.5
Version 4.0.30319 4.5.40805
CBS 1 1
TargetVersion 4.0.0 4.0.0
Install 1 1
Servicing 0 0
Release   368485
InstallPath C:\Windows\Microsoft.NET\Framework64\v4.0.30319\ C:\Windows\Microsoft.NET\Framework64\v4.0.30319\

One of the major additions to the framework is the support for the Task-based Asynchronous Pattern. All classes with asynchronous APIs have be augmented with a Task-based asynchronous API.

Task exception handling has also changed in .NET 4.5 to accommodate the new async keywords in C# and VB. (read more)

Internet Explorer 10 User Agent Strings On Windows 8 64bit

Internet Explorer 10 is the web browser Microsoft is delivering with Windows 8.

According to its different usages and modes, its user agent string is as follows:

Application Environment 32/64 bit User Agent String
Internet Explorer Metro - Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)
Javascript Application Metro - Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0;)
C#/VB Application Metro 32bit Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
C#/VB Application Metro 64bit Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)
Internet Explorer Desktop 32bit
Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)
Internet Explorer Desktop 64bit(1) Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)
WPF Application Desktop 32bit Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; WOW64; Trident/6.0; .NET4.0E; .NET4.0C; Tablet PC 2.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Zune 4.7; InfoPath.3)
WPF Application Desktop 64bit Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.2; Win64; x64; Trident/6.0; .NET4.0E; .NET4.0C; Tablet PC 2.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Zune 4.7; InfoPath.3)
(1) Needs to be enabled for each security zone.

Analyzing the above table I conclude that:

  1. Metro Internet Explorer is always a 64bit application on 64bit Windows 8.
  2. Javascript Metro Style Applications don’t announce if they are 32bit or 64bit.
    1. They look like they are running on a 32bit Windows 8.
  3. Desktop Internet Explorer retains the same behavior introduced with Internet Explorer 8.
  4. Metro Style C#/VB Applications hosting the web browser (WebView control) exhibit the same behavior as Internet Explorer.
  5. Desktop WPF Applications hosting the web browser (WebBrowser control) retain the same behavior introduced with Internet Explorer 8.

BUILD Keynotes | Live and Online

BUILD, the conference that everybody is expecting Microsoft to present the first news about Windows 8, for Developers, is about to begin. The good news is that the Keynotes will be available live online on the site www.buildwindows.com!

Channel9 is also doing extensive cover of the event with online sessions, interviews, discussion panels, etc. (more news here: http://news.cnet.com/8301-10805_3-20103036-75/behind-the-scenes-at-microsofts-channel-9/).

Cleaning HTML With Regular Expressions

While participating in a forum discussion, the need to clean up HTML from "dangerous" constructs came up.

In the present case it was needed to remove SCRIPT, OBJECT, APPLET, EMBBED, FRAMESET, IFRAME, FORM, INPUT, BUTTON and TEXTAREA elements (as far as I can think of) from the HTML source. Every event attribute (ONEVENT) should also be removed keep all other attributes, though.

HTML is very loose and extremely hard to parse. Elements can be defined as a start tag (<element-name>) and an end tag (</element-name>) although some elements don't require the end tag. If XHTML is being parsed, elements without an end tag require the tag to be terminated with /> instead of just >.

Attributes are not easier to parse. By definition, attribute values are required to be delimited by quotes (') or double quotes ("), but some browsers accept attribute values without any delimiter.

We could build a parser, but then it will become costly to add or remove elements or attributes. Using a regular expression to remove unwanted elements and attributes seems like the best option.

First, lets capture all unwanted elements with start and end tags. To capture these elements we must:

  • Capture the begin tag character followed by the element name (for which we will store its name - t): <(?<t>element-name)
  • Capture optional white spaces followed by any character: (\s+.*?)?
  • Capture the end tag character: >
  • Capture optional any characters: .*?
  • Capture the begin tag character followed by closing tag character, the element name (referenced by the name - t) and the end tag character: </\k<t>>
<(?<t>tag-name(\s+.*?)?>.*?</\k<t>>

To capture all unwanted element types, we end up with the following regular expression:

<(?<t>script|object|applet|embbed|frameset|iframe|form|textarea)(\s+.*?)?>.*?</\k<t>>

Next, lets capture all unwanted elements without an end tag. To capture these elements we must:

  • Capture the begin tag character followed by the element name: <element-name
  • Capture optional white spaces followed by any character: (\s+.*?)?
  • Capture an optional closing tag character: /?
  • Capture the end tag character: >
<tag-name(\s+.*?)?/?>

To capture all unwanted element types, we end up with the following regular expression:

<(script|object|applet|embbed|frameset|iframe|form|textarea|input|button)(\s+.*?)?/?>

To remove those unwanted elements from the source HTML, we can combine these two previous regular expressions into one and replace any match with an empty string:

Regex.Replace(
    sourceHtml,
    "|(<(?<t>script|object|applet|embbed|frameset|iframe|form|textarea)(\\s+.*?)?>.*?</\\k<t>>)"
        + "|(<(script|object|applet|embbed|frameset|iframe|form|input|button|textarea)(\\s+.*?)?/?>)"    ,
string.Empty);

And finally, the unwanted attributes. This one is trickier because we want to capture unwanted attributes inside an element's start tag. To achieve that, we need to match an element's opening tag and capture all attribute definitions. To capture these attributes we must:

  • Match but ignore the begin tag character followed by any element name: (?<=<\w+)
  • Match all:
    • Don’t capture mandatory with spaces: (?:\s+)
    • Capture attribute definition:
      • Capture mandatory attribute name: \w+
      • Capture mandatory equals sign: =
      • Capture value specification in one of the forms:
        • Capture double quoted value: "[^"]*"
        • Capture single quoted value: '[^']*'
        • Capture unquoted value: .*?
  • Match but ignore end tag: (?=/?>)
(?<=<\w+)((?:\s+)(\w+=(("[^"]*")|('[^']*')|(.*?)))*(?=/?>)

The problem with the previous regular expression is that it matches the start tag and captures the whole list of attributes and not each unwanted attribute by itself. This prevents us from from replacing each match with a fixed value (empty string).

To solve this, we have to name what we want to capture and use the Replace overload that uses a MatchEvaluator.

We could capture unwanted attributes as we did for the unwanted elements, but then we would need to remove them from the list of all the element’s attributes. Instead, we’ll capture the wanted attributes and build the list of attributes. To identify the wanted attributes, we’ll need to name them (a). The resulting code will be something like this:

Regex.Replace(
    sourceHtml,
    "((?<=<\\w+)((?:\\s+)((?:on\\w+=((\"[^\"]*\")|('[^']*')|(.*?)))|(?<a>(?!on)\\w+=((\"[^\"]*\")|('[^']*')|(.*?)))))*(?=/?>))",
    match =>
    {
        if (!match.Groups["a"].Success)
        {
            return string.Empty;
        }
        
        var attributesBuilder = new StringBuilder();
        
        foreach(Capture capture in match.Groups["a"].Captures)
        {
            attributesBuilder.Append(' ');
            attributesBuilder.Append(capture.Value);
        }
        
        return attributesBuilder.ToString();
    }
);

To avoid parsing the source HTML more than once, we can combine all the regular expressions into a single one.

Because we are still outputting only the wanted attributes, there’s no change to the match evaluator.

A few options (RegexOptions) will also be added to increase functionality and performance:

  • IgnoreCase: For case-insensitive matching.
  • CultureInvariant: For ignoring cultural differences in language.
  • Multiline: For multiline mode.
  • ExplicitCapture: For capturing only named captures.
  • Compiled: For compiling the regular expression into an assembly. Only if the regular expression is to be used many times.

The resulting code will be this:

Regex.Replace(
    sourceHtml,
    "(<(?<t>script|object|applet|embbed|frameset|iframe|form|textarea)(\\s+.*?)?>.*?</\\k<t>>)"
        + "|(<(script|object|applet|embbed|frameset|iframe|form|input|button|textarea)(\\s+.*?)?/?>)"
        + "|((?<=<\\w+)((?:\\s+)((?:on\\w+=((\"[^\"]*\")|('[^']*')|(.*?)))|(?<a>(?!on)\\w+=((\"[^\"]*\")|('[^']*')|(.*?)))))*(?=/?>))",
    match =>
    {
        if (!match.Groups["a"].Success)
        {
            return string.Empty;
        }
        
        var attributesBuilder = new StringBuilder();
        
        foreach(Capture capture in match.Groups["a"].Captures)
        {
            attributesBuilder.Append(' ');
            attributesBuilder.Append(capture.Value);
        }
        
        return attributesBuilder.ToString();
    },
    RegexOptions.IgnoreCase
        | RegexOptions.Multiline
        | RegexOptions.ExplicitCapture
        | RegexOptions.CultureInvariant
        | RegexOptions.Compiled
);

This was not extensively tested and there might be some wanted HTML remove and some unwanted HTML kept, but it’s probably very close to a good solution.

The new MSDN Forums Assistant

Microsoft has launched the MSDN Forum Assistant, which allows you to access MSDN Forums and receive updates directly to your desktop. Features include Ask a Question, Latest Post, Thread Updates, Priority Support, and Forum Q&A. You can download the MSDN Forum Assistant here: http://gallery.live.com/liveItemDetail.aspx?li=822a3787-89e8-4c6d-8fc4-a413437d86e9

You can find more information here:

Visual Studio UserVoice Site

A Visual Studio UserVoice site has been set up to collect community feedback:

For filing bugs, the Visual Studio Connect site is still the place to go:

GUID Generator: My first Windows Phone application is available on the marketplace

In order to complete the registration as a Windows Phone developer an application needs to be submitted.

Following the advise of Caio, I wrote a GUID Generator. It has now been published and you can get it here.

Have you ever felt the need for a freshly generated GUID?

With this application you can have a freshly generated GUID whenever you want.

A GUID (Globally Unique Identifier) is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required. Such an identifier has a very low probability of being duplicated. Do you want o try?

Talking with friends with iPhone and Android phones I became aware that there is no such application for those platforms. How can they live without one?