Uncategorized

Showing branch name (using colors) on windows subsystem for linux (wsl)


Hello,

The problem:

The default WSL is using bash and even though it supports coloring and customization, the default one is very simple. Since I work with git and I’d like to know which branch I am working on, I’d like to see how to show it in the bash prompt.

The solution (Please notice that I am assuming your installation is using Bash as shell, other shells may be supported, but required some variations in the instructions. )

1- As obvious that it’d sound, first install Windows Subsystem for Linux (WSL)
2- Install Git into the WSL, normally it’s about executing the following command:
sudo apt-get install git

3- Open a terminal Window, and create a file at home. It can be any name. We will use ,git-prompt.sh the content of this file can be download from https://github.com/git/git/blob/master/contrib/completion/git-prompt.sh I tried adding the file content here, but it does not render well ūüėČ

The if statement is optional, however we assume that the file might not be present.
5- Now if you took a look at the file, in the header there is an explanation about how to update a bash or zsh. We will be using bash since we also will support some coloring.

The original script suggest:
PS1='[\u@\h \W$(__git_ps1 " (%s)")]\$ '

However it’s possible in Bash to add support for colors. IF you want to understand better bash command and feature, type the following command in you bash terminal
man bash

Here is a sample of the updated PS1
PS1='\[\033]0;$TITLEPREFIX:$PWD\007\]\n\[\033[32m\]\u@\h \[\033[35m\]$MSYSTEM \[\033[33m\]\w\[\033[36m\]$(__git_ps1 " (%s)")\[\033[0m\]\n$'

The final result will show, the

username@hotname directory (branch)

in the bash promt

 

Advertisements

MSBuild Zip File, SVN Revision after Web Publish


As part of an automation part for deploying an application I’ve spent some time creating MSBuild scripts for simple tasks. The following are the tasks I’ll be providing

  • Create a Zip file from the output
  • Read the SVN Revision from the repository

The code will be using inline tasks, since they can be created almost at any time. The inline tasks, lets the user create tasks using C# (and compile against .NET 4.0), at least by the time I am creating this blog post.

Reading the Revision Number using MSBuild

For this particular MSBuild task, I am depending on a third party assembly, SharpSvn. It is a SVN client implement in C#. The tricky part in this script is about loading external assemblies and calling its functions, all the work is doing thru reflection.

<UsingTask TaskName="SVNRevisionReader"               TaskFactory="CodeTaskFactory"               AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll" >
    <ParameterGroup>
      <SvnPath ParameterType="System.String" Required="true" />
	  <SharpSvnUIDllFilePath ParameterType="System.String" Required="false" />
	  <SvnOutputDirectory ParameterType="System.String" Required="false" />
	  <LastChangeRevision ParameterType="System.Int32" Output="true" />
      <Revision ParameterType="System.Int32" Output="true" />
    </ParameterGroup>
    <Task>
		<Reference Include="Microsoft.CSharp" />
		<Using Namespace="System"/>
		<Using Namespace="System.Diagnostics"/>
		<Using Namespace="System.IO"/>
		<Using Namespace="System.Reflection"/>
		<Code Type="Fragment" Language="cs">
<![CDATA[
            //========================= BEGIN: In-line Task
            Func<string, Assembly> loadAssembly = (string assemblyFilePath) =>
            {
                Assembly assembly = null;
				Log.LogMessage("Loading assembly from :" + assemblyFilePath, MessageImportance.High);
                if (!string.IsNullOrEmpty(assemblyFilePath) && System.IO.File.Exists(assemblyFilePath))
                {
                    assembly = Assembly.UnsafeLoadFrom(assemblyFilePath);
                }
                else
                {
                    Log.LogError("Assembly file path:" + assemblyFilePath + " does not exist.", MessageImportance.High);
                }

                return assembly;
            };

            var sharpSvnAssembly = loadAssembly(@"..\MSBuild\SharpSvn.1.9-x86.1.9004.3913.141\lib\net40\SharpSvn.dll");
            //var sharpSvnUIAssembly = loadAssembly(@"..\MSBuild\SharpSvn.1.9-x86.1.9004.3913.141\lib\net40\SharpSvn.UI.dll");
            var typeOfSharpSvnSvnClient = sharpSvnAssembly.GetType("SharpSvn.SvnClient");
            var typeOfSharpSvnSvnTarget = sharpSvnAssembly.GetType("SharpSvn.SvnTarget");
            var typeOfSharpSvnSvnInfoEventArgs = sharpSvnAssembly.GetType("SharpSvn.SvnInfoEventArgs");

            LastChangeRevision = 0;
            Revision = 0;
            //using (var client = new SharpSvn.SvnClient())
            using (dynamic client = Activator.CreateInstance(typeOfSharpSvnSvnClient))
            {
                //SharpSvn.SvnInfoEventArgs info;
                try
                {
                    //client.GetInfo(SharpSvn.SvnTarget.FromString(SvnPath), out info);
                    dynamic svnTarget = sharpSvnAssembly
                            .GetType(@"SharpSvn.SvnTarget")
                            .GetMethod(@"FromString", BindingFlags.Public | BindingFlags.Static, Type.DefaultBinder, new[] { typeof(string) }, null)
                            .Invoke(null, new object[] { SvnPath });
                    var getInfoArgs = new object[] { svnTarget, null };
                    typeOfSharpSvnSvnClient
                        .GetMethod(@"GetInfo", new[] { typeOfSharpSvnSvnTarget, typeOfSharpSvnSvnInfoEventArgs.MakeByRefType() })
                        .Invoke(client, getInfoArgs);
                    dynamic info = getInfoArgs[1];

                    LastChangeRevision = Convert.ToInt32(info.LastChangeRevision.ToString());
                    Revision = Convert.ToInt32(info.Revision.ToString());
                }
                //catch (SharpSvn.SvnInvalidNodeKindException svnInvalidNodeKindException)
                catch (System.Exception exception)
                {
                    Log.LogError(exception.Message);
                }
            }

			if (!string.IsNullOrEmpty(SvnOutputDirectory) && System.IO.Directory.Exists(SvnOutputDirectory))
			{
				System.IO.File.WriteAllLines(System.IO.Path.Combine(SvnOutputDirectory, "LastChangeRevision.svn"), new[] { LastChangeRevision.ToString() });
				System.IO.File.WriteAllLines(System.IO.Path.Combine(SvnOutputDirectory, "Revision.svn"), new[] { Revision.ToString() });
			}

            Log.LogMessage(string.Concat("LastChangeRevision: ", LastChangeRevision, " Revision: ", Revision), MessageImportance.High);
            //========================= END: In-line Task
]]>
      </Code>
    </Task>
  </UsingTask>

Compressing a directory into a Zip file

The next MSBuild script is about using compressing a directory (normally the output directory after the app has been built).

<UsingTask TaskName="ZipDirectory" TaskFactory="CodeTaskFactory" AssemblyFile="$(MSBuildToolsPath)\Microsoft.Build.Tasks.v4.0.dll">
  <ParameterGroup>
	<InputDirectory ParameterType="System.String" Required="true" />
    <OutputFilename ParameterType="System.String" Required="true" />
  </ParameterGroup>
  <Task>
    <Reference Include="System.IO.Compression" />
    <Using Namespace="System.IO.Compression" />
    <Code Type="Fragment" Language="cs">
    <![CDATA[       try       { 		Log.LogMessage(string.Concat("Compressing: ", InputDirectory, " To File: ", OutputFilename), MessageImportance.High); 		 		using (Stream zipStream = new FileStream(Path.GetFullPath(OutputFilename), FileMode.Create, FileAccess.Write)) 		using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create)) 		{ 			foreach(var filePath in System.IO.Directory.GetFiles(InputDirectory,"*.*",System.IO.SearchOption.AllDirectories)) 			{ 				var relativePath = filePath.Replace(InputDirectory,string.Empty); 				using (Stream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))                 using (Stream fileStreamInZip = archive.CreateEntry(relativePath).Open())                     fileStream.CopyTo(fileStreamInZip); 			} 		} 		 		//System.IO.Compression.ZipFile.CreateFromDirectory(InputDirectory, OutputFilename, System.IO.Compression.CompressionLevel.Fastest, true);         return true;       }       catch (Exception ex)       {         Log.LogErrorFromException(ex);         return false;       }     ]]>
    </Code>
  </Task>
</UsingTask>

The previous code snippets are good enough to get the tasks ready for being used. Depending on the type of project, or the needs the MSBuild tasks can be invoked on different times.
 

Calling the SVN Reader before starting the build process starts

In order call your Task before the build process starts, it can be done by specifying the BeforeTargets attribute, and setting it to: PrepareForBuild

<Target Name="EnsureSVNRevision" BeforeTargets="PrepareForBuild">
    <SVNRevisionReader SvnPath="$(SolutionDir)" SvnOutputDirectory="$(MSBuildProjectDirectory)">
      <Output PropertyName="SvnLastChangeRevision" TaskParameter="LastChangeRevision" />
      <Output PropertyName="SvnRevision" TaskParameter="Revision" />
    </SVNRevisionReader>
  </Target>

 

Calling the Directory Zip Compressor after the Build is over

In the case trying to execute the zip compression after building, normally it can be called in the Target AfterBuild, e.g.:

  <Target Name="AfterBuild"
		  Condition="'$(Configuration)' == 'Development' Or '$(Configuration)' == 'Stage'"
	>
    <MakeDir Directories="$(SolutionDir).Deployments/$(Configuration)/" Condition="!Exists('$(SolutionDir).Deployments/$(Configuration)/')" />
    <Delete Files="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" Condition="Exists('$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip')" />
    <ZipDirectory OutputFilename="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" InputDirectory="$(TargetDir)\" />
  </Target>

 

However if you are creating a Web Project, normally you don’t take the output directory because it’s slightly different from the published files (which take few more processing). Web Projects are published, In this case, it’s assumed that it’s published to the local file system (where the MSBuild has access to it). Thus, The AfterTargets can be used to detect when files are ready

<Target Name="AfterWebPublish" AfterTargets="WebPublish"
          Condition="'$(Configuration)' == 'Development' Or '$(Configuration)' == 'Stage'"
  >
    <MakeDir Directories="$(SolutionDir).Deployments/$(Configuration)/" Condition="!Exists('$(SolutionDir).Deployments/$(Configuration)/')" />
    <Delete Files="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" Condition="Exists('$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip')" />
    <ZipDirectory OutputFilename="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName).zip" InputDirectory="$(MSBuildProjectDirectory)\$(publishUrl)\" />
    <RemoveDir Directories="$(SolutionDir).Deployments/$(Configuration)/$(ProjectName)/" Condition="Exists('$(SolutionDir).Deployments/$(Configuration)/$(ProjectName)/')" />
  </Target>

With the previous snippets it is possible to customize more in detail when the Tasks should be executed to get the work done.

Cheers,
Herb

How embed fonts in Windows Phone 8 using a WebBrowser control


Hello,

I hope I will be sharing some ideas from my last project. This in particular was difficult.

The problem

I have a font I want to embed into my Windows Phone Application, and I want to use in a WebBrowser Control. I have managed to embed the font and use it with standard textblocks control, however I haven’t get it working with the WebBrowser control.

The Solution

Using Data Url Scheme for embedding the font ( http://en.wikipedia.org/wiki/Data_URI_scheme ). Then let the WebBrowser control to consume the font.

First things I learned it’s that a Data Url can contains basically data (e.g.images, fonts, more files. it’s really handy). Moreover, I learned that CSS can consume this data, which makes a good option to embed the font and use it off line. There may be few bumps, such as using Base64 encoding, but one we get the rhythm it won’t hurt more.

1) In your window phone project select add the font file. Here is a remark, make sure the file format is compatible with Windows Phone (TTF files) or the Windows Phone Web Browser (WOFF). TTF may require some work to get them working , for example enabling the flag for allow the file to be embedded, or making sure that its internal file structure is compatible with WP platform.

2) When embedding the file, make sure that the file properties are set: Build Action to Content and Copy to Output Directory to Copy if newer.

3) Prepare some HTML for the setting up the font. In my project sample, I’ll be using Navigate to String method and to keep HTML code apart from my C# code, I’ll be loading an external file from the XAP package, and applying some text replacements with Regular Expressions.

4) In out sample check MainPage.xaml.cs to see how the FileManager to apply the custom font.

I haven’t tried, but I think this approach could be extended to inject a custom font to an external page. Here is the output:

Image

The code sample is at

https://github.com/hmadrigal/playground-dotnet/tree/master/MsWinPhone.EmbedFont

I hope you could find this handy,

Cheers,

Herb

After moving some files my Windows Phone Project Throws a XamlParseException


I am not sure if this is an known issue for Microsoft, but at least I am aware of this. However this is the first time I am gonna document it. Since it can be very frustrating

The Problem

I want to reorganize the files in my Windows Phone Project in order to follow our implementation of MVVM, moreover we have decided that resx file will live in their own assembly project. However, once I move the Resource file to an external assembly the application just thrown an XamlParseException even though the reference in XAML is totally correct.

REMARKS
XamlParseExceptions may be thrown by other reasons. It is important to realize that I knew I moved the RESX file to a different assembly and before that everything was working. Certainly I updated the reference to the new assembly, and the exception was still being thrown. That is why this is a tricky issue.

The solution

It took some time to me to noticed, but somehow the project that contains the RESX file cannot contain a dot (.) character in its assembly name. If it happens then XAML parse will throw a XamlParserException even though the reference to the assembly is correct. The error message may contain something like this:

System.Windows.Markup.XamlParseException occurred
  HResult=-2146233087
  Message=Unknown parser error: Scanner 2147500037. [Line: 10 Position: 126]
  Source=System.Windows
  LineNumber=10
  LinePosition=126
  StackTrace:
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at MyApp.App.InitializeComponent()
       at MyApp.App..ctor()
  InnerException: 

Taking a look at the location where I was loading the Resource it was this:

<Application
    x:Class="MyApp.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:MyApp;assembly=MyApp.WindowsPhone8.Resources" x:Key="LocalizedStrings"/>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

The line one of my projects generates an assembly named: ‘MyApp.WindowsPhone8.Resources’, in order to resolve the issue I only have to update the generated assembly name to be ‘MyApp_WindowsPhone8_Resources’ and then update the proper reference in the XAML. For example:

        <local:LocalizedStrings xmlns:local="clr-namespace:MyApp;assembly=MyApp_WindowsPhone8_Resources" x:Key="LocalizedStrings"/>

After performing this change your app should work normally.

Custom Configuration files in .NET


Hi,

I would like to share some ideas about how to deal with configuration files on .NET. I am sure that there should be many options for implementing configuration files. Some are more likely to better than others and others may depend on the platform (e.g. mobile, desktop, cloud, etc..) ¬†In particular I’ll explain an approach that should be working for your Web Application and for your Desktop application.

The problem:

We want to quickly create a configuration file, since we foresee that our application will have a good amount of settings.

Solution:

I’ll take advantage of the default .NET mechanism for configuration files. It’s plenty flexible and quite extensible. The major drawback is that you’ll have to write code, and sometimes a not-so-easy code. Alternatively you can relay on the existent section handlers, and try to use them when possible.

A brief talk about configuration files

.NET configuration files supports hierarchies and they are extensible. Almost all the cases with defaults is fairly enough, for creating custom sections there I will be explaining three approaches, but certainly there are more. The decision of which approach to take it will depend of how much time you have and also if the team has the will and chance for installing at least the Configuration Section Designer. 

1) Create Custom Section Handler with Code Snippets

This is the simplest one, it’s basically get¬†code snippets of how to create sections for the SectionHandler class. Then you are more likely to have many property of similar times (probably primitive types) but it can safe time for producing the class and understand it. I won’t be providing samples for this.

2) Using the Custom Section Designer from CodePlex at https://csd.codeplex.com/

This is my favorite, but unfortunately it demands that you will have to install a Visual Studio Extension for opening configuration section projects. The best of this approach is that you can get:

  • XSD validation + intelisense
  • XSD Documentation
  • Ease to modify content.
  • etc…

First get proper installer for the extension https://csd.codeplex.com/. Then create a configuration section project (these projects outputs a dll), by conversion these projects end with “.Configuration” for example “MyApp.Configuration” will be dll project that loads the configuration section. In our sample it is provided a project called:¬†ConfigurationFileSample.Configuration

Image

The previous screenshot exemplifies a Section in which it has been defined a Element called Mappings, which is a collection of Mapping elements. By following the instructions at https://csd.codeplex.com/wikipage?title=Defining%20new%20types&referringTitle=Usage you will easily create this configuration. The magic arrive when you are typing these values into visual studio (or any XML editor that supports XSD validation property).

Once you are done designing your configuration file. You save and compile. If it success then add a configuration file to your main project, and also add a reference to the configuration file project. for referencing our custom section we do it into the configSections element. For example:


<!-- NOTE:„ÄÄIn here a custom section is specified, this section has been created by writing code--></pre>
<section></section>
<pre>

Visual studio will enable help toltips and XSD validation for you. As you can see in the following screenshot

ConfigurationTooltip

For loading configuration files in code, after adding the reference to the dll, the app can use an static helper of simply use the GetSection method.

For example for loading the section using the default instance:

// NOTE: It is also possible to load the default custom section
Console.WriteLine("\nLoading default Minestrone thru singleton!");
var minestroneSection = Minestrone.MinestroneSection.Instance;
PrintMinestrone(minestroneSection);

Or also you could load a specific section:

// NOTE: You could ask for a given custom section
Console.WriteLine("\nLoading Minestrone by manually specifying a section: minestroneSection");
var minestroneSection = ConfigurationManager.GetSection("minestroneSection") as Minestrone.MinestroneSection;
PrintMinestrone(minestroneSection);

3) Using one of the build in Section Handlers
As I mentioned you also could you the built-in Sectionhandlers from .NET framework There is a list of them at the end of this page: http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx (basically all the subclasses of System.Configuration.ConfigurationSection )

For example for referencing it into the configuration file:


    <!-- NOTEÔľöThis custom section uses .NET framework sections instead, see http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx for a list of the classes available into the framework --></pre>
<section></section>
<pre>

and for loading it from code:

// NOTE: Loads a custom section, but it uses a .NET built-in class
            Console.WriteLine("\nLoading a ApplicationSettings using a built-in .NET type. (more at http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx ) ");
            var myConfigSection = ConfigurationManager.GetSection("myConfigSection") as System.Collections.Specialized.NameValueCollection;
            for (int appSettingIndex = 0; appSettingIndex < myConfigSection.Count; appSettingIndex++)
            {
                Console.WriteLine(string.Format("Key:{0} Value:{1}", myConfigSection.AllKeys[appSettingIndex], myConfigSection[appSettingIndex]));
            }
            Console.WriteLine("\n\n PRESS ANY KEY TO FINISH! ");
            Console.ReadKey();

We are almost there, but WAIT!!!!

I know that at this time I haven’t shown how to specify section. Well basically because you can provide the details of the section¬†inline into the same file or specifcying an external file using the¬†configSource attribute (which is available for any custom section).

For example see how the¬†minestroneSection¬†is defined in an external file, and how¬†myConfigSection¬† is written inline into the configuration file. There are advantages of each approach (e.g. when you want to apply XSLT transformations you may want to apply transformations to a simple file rather than a long and complex XML) o perhaps you may have a config file per environment (e.g. minestrone.debug.config…,)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <!-- NOTE:„ÄÄIn here a custom section is specified, this section has been created by writing code-->
    <section name="minestroneSection" type="Minestrone.MinestroneSection, ConfigurationFileSample.Configuration, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>

    <!-- NOTEÔľöThis custom section uses .NET framework sections instead, see http://msdn.microsoft.com/en-us/library/system.configuration.configurationsection.aspx for a list of the classes available into the framework -->
    <section name="myConfigSection" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

  </configSections>
  <myConfigSection>
    <add key ="ALPHA" value="1|1"/>
    <add key ="BETA" value="1|1"/>
    <add key ="GAMA" value="1|1"/>
  </myConfigSection>

  <!-- NOTE: Loading section from a external file-->
  <minestroneSection configSource="minestroneSection.config" />

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

I hope this is good enough for setting up quickly custom configuration files in your .NET projects.
The sample is at https://github.com/hmadrigal/playground-dotnet/tree/master/MsDotNet.CustomConfigFile and please remember to install the Custom Section Designer if you want to take the option for more detailed configuration file.

Regards,
Herber

Previewing Microsoft Office Documents and PDF in WPF using a Preview Handler Host


Hi,

I had recently to implement a mechanism for showing documents into a WPF application. The very first solution was to start a new process using the path of the selected file. The only condition for this to work it’s that the file extension should have been previously registered to be opened with an specific application. A drawback is that the document is shown by an external application that we are not able to handle (at all or at least easily).

Then the client decided showing documents by embedding them into the application. So, I started researching about how to display Portable documents, Word documents, Excel documents into a WPF application. Finally after some research I got to a nice solution which will display many documents formats with the only condition of having the proper software installed into the PC where the application is gonna run.

The Problem
Showing a office document (XLS, DOC, XLSX, DOCX) or portable document (PDF) into a WPF APP.

Remarks
This is not a bullet proof solution, but it is kind extensible for brave developers. The relays on the installed Preview Handlers. For example for visualizing Microsoft Office documents you will have to have installed Microsoft Office in the computer where the application is going to run. It’s the same case for PDF files, where you need to have a Preview Handler installed.
Moreover, the technology of Preview Handler has been built using native code, in order to embedded into a WPF application the app will be hosting WinForm content, this introduces a different set of restrictions about the interaction of the control once it has been instantiated.

The Solution
Preview Handlers is a technology added into Windows Vista, and it’s meant to readonly quick preview of files. It’s available on Windows Explorer
Enabling Previews in Windows Vista Folders
The Microsoft article [1] about Preview Handlers has an utility application which lists the available preview handlers in your system. Moreover after some research, I found the first implementation in [2] which was using Windows Forms and C# for development. Finally I found a more elaborated solution from Coding4Fun [3]. Since this point the solution for WPF was straightforward because it was only matter of loading a WinForm contro into WPF by using Interop components.

1) Grab the code from http://c4fdevkit.codeplex.com/ project. To use the Preview Handler Host you will need only two projects.
/Preview Handlers/Source/VCS/PreviewHandlerFramework
/Preview Handlers/Source/VCS/PreviewHandlerHost

Compile them and the create a new WPF project. The codeplex project hasn’t been updated in awhile, thus you’re more likely to have to update the solution file to a new version of visual studio.

2) Add to your WPF application (or library) a reference to WindowsFormIntegration library. This will allow the application to host WinForm controls. Also add System.Windows.Forms since it will be required by the Preview Handler libraries.

3) The use the WindowsFormsHost tag to embed the PreviewHandlerHostControl as the follwing XAML shows:


        <button></button>

            <C4F_DevKit_PreviewHandler_PreviewHandlerHost:PreviewHandlerHostControl x:Name="previewHandlerHostControlInstance" />

4) Finally our C# code that for our sample is quite simple. We just select a file and let the PreviewHandlerHost control do the work.

using System;
using System.Windows;

namespace PreviewHandlerWpfApplication
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.OpenFileDialog();
            if (dlg.ShowDialog().Value)
            {
                previewHandlerHostControlInstance.FilePath = dlg.FileName;
            }
        }
    }
}

Here is an screenshot of my mighty document viewer:

Sample.Word.Document.Capture

Finally the code is available at GitHub https://github.com/hmadrigal/playground-dotnet/tree/master/MsDesktop.PreviewHandlers Have fun!

References

 

 

Automatic retries using the Transient Fault Handling from Enterprise libraries (EntLib)


Hello,

I was reading about the¬†Transient Fault Handling at¬†http://msdn.microsoft.com/en-us/library/hh680901(v=pandp.50).aspx¬†This is a block of the Enterprise Libraries which is part of the Enterprise Integration Pack for Windows Azure. I haven’t played much with Azure yet, but one part of the documentation took my attention:

When Should You Use the Transient Fault Handling Application Block?
…… (some explanation about when you are using Azure services) ……
You Are Using a Custom Service
If your application uses a custom service, it can still benefit from using the Transient Fault Handling Application Block. You can author a custom detection strategy for your service that encapsulates your knowledge of which transient exceptions may result from a service invocation. The Transient Fault Handling Application Block then provides you with the framework for defining retry policies and for wrapping your method calls so that the application block applies the retry logic.Source: http://msdn.microsoft.com/en-us/library/hh680901(v=pandp.50).aspx#sec10

So, I thought why do I have to write always the retry logic if this is already done ;-).

The problem

We want to write a generic retry mechanism for our application. Moreover we need to support some level of customization thru configuration files.

The solution
Simple let set up the Transient Fault Handling Block (from now on FHB). First of fall you will have to download the libraries, you could do it using NuGet – which by default adds also references to support Windows Azure -. You can remove the Windows Azure DLL if you’re not going to be using Windows Azure in your app.

The FHB lets you specify a error detection strategy as well as retry strategy when you construct the retry policy. Also, a Retry policy support the execution of Action, Funcdelegates or Actionfor async retries.

As part of the configuration process for FHB, it’s required to indicate which errors should be handled by FHB. A mechanism for doing this is by implementing the interface: ITransientErrorDetectionStrategy. In our sample, we only want to retry when a FileNotFoundException is thrown. Thus we have coded the FileSystemTransientErrorDetectionStrategy:

using System;
using System.IO;
using Microsoft.Practices.TransientFaultHandling;

namespace SampleConsoleApplication.TransientErrors.Strategies
{
    public class FileSystemTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
    {
        #region ITransientErrorDetectionStrategy Members

        public bool IsTransient(Exception ex)
        {
            return ex is FileNotFoundException;
        }

        #endregion
    }
}

Certainly you could implement it by handling any particular exception.
Now we need to define a retry policy. This is done by specifying a retry strategy. The following code example configures a retry policy by providing a retry strategy incremental. Additionally in our sample service.DoSlowAndImportantTask will always fail, so the FHB will retry automatically based on the retry policy.

 private static void RetryPolityUsingCode(IUnityContainer container, IService service, OutputWriterService writer)
        {
            writer.WriteLine("Begin sample: RetryPolityUsingCode");
            // Define your retry strategy: retry 5 times, starting 1 second apart
            // and adding 2 seconds to the interval each retry.
            var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));

            // Define your retry policy using the retry strategy and the Windows Azure storage
            // transient fault detection strategy.
            var retryPolicy = new RetryPolicy(retryStrategy);

            try
            {
                // Do some work that may result in a transient fault.
                retryPolicy.ExecuteAction(service.DoSlowAndImportantTask);
            }
            catch (Exception exception)
            {
                // All the retries failed.
                writer.WriteLine("An Exception has been thrown:\n{0}", exception);
            }
            writer.WriteLine("End sample: RetryPolityUsingCode");
        }

Additionally it’s possible to specify the retry policy in the configuration file, for example:

<!--?xml version="1.0" encoding="utf-8" ?-->

<section name="RetryPolicyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings, Microsoft.Practices.EnterpriseLibrary.WindowsAzure.TransientFaultHandling" requirepermission="true"></section><section name="typeRegistrationProvidersConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Common.Configuration.TypeRegistrationProvidersConfigurationSection, Microsoft.Practices.EnterpriseLibrary.Common"></section>

Please notice that there is a default Retry Strategy indicating that will use “Fixed Interval Retry Strategy”. Our C# code will ask for the RetryManager instance in order to retrieve the Retry Policy and then perform the task.

private static void RetryPolityUsingConfigFile(IUnityContainer container, IService service, OutputWriterService writer)
        {
            writer.WriteLine("Begin sample: RetryPolityUsingConfigFile");
            // Gets the current Retry Manager configuration
            var retryManager = EnterpriseLibraryContainer.Current.GetInstance();

            // Asks for the default Retry Policy. Keep on mind that it's possible to ask for an specific one.
            var retryPolicy = retryManager.GetRetryPolicy();
            try
            {
                // Do some work that may result in a transient fault.
                retryPolicy.ExecuteAction(service.DoSlowAndImportantTask);
            }
            catch (Exception exception)
            {
                // All the retries failed.
                writer.WriteLine("An Exception has been thrown:\n{0}", exception);
            }
            writer.WriteLine("End sample: RetryPolityUsingConfigFile");
        }

The Transient Fault Handling Block a simple and flexible alternative for a well-known task. A automatic retry system, moreover this supports configuration file, so we should be able to change the retry policy just by modifying our configuration file.

Here is an screen shot of the app, which automatically retries calling our mischievous method.

Transient Fault Handling Tries Output Capture

As usual the code sample is at https://github.com/hmadrigal/playground-dotnet/tree/master/MsEntLib.TransientErrorHandling