Udostępnij za pośrednictwem


How to use on demand download Satellite assemblies for localization?

Working on localization in WPF led me to get more familiar with satellite assemblies. Localization in WPF requires having multiple resource files for every language that the application supports. Having said that, there will be an issue when you get to ClickOnce deployment. Very first question would be can I choose what resource file to download to the client machine? Or all resource files will be downloaded to every client machine? The answer is on demand Satellite assembly. What does it mean? Well, that means you need to create a satellite assembly for every resource file that you have and then provide a mechanism to download it on demand based on the client machine’s settings. How that is possible is what I am going to talk about here.

Step 1: Create a resource file:

Visual Studio provides a simple way to do this, just select new item and then select resource file as a type. Insert your strings and values using IDE designer. Result is a file like Resource1.resx.

Step2: Generate a binary .reource file from your original .resx file:

resgen command provided by .Net SDK is the easiest way to do this step, here is an example

C:\Windows\Microsoft.Net\Framework\v2.0.50727> resgen.exe Resource1.resx

A file with .resource extension will be created like Resource1.resource

Step3: Generate Satellite assembly file from .resource:

Again .Net SDK provides a command to easily create a satellite assembly out of a file, here is an example

C:\Windows\Microsoft.Net\Framework\v2.0.50727> al.exe /embed: Resource1.resources /out:Resource1.dll

Step4: Setting up Satellite Assemblies to be downloaded on Demand:

After adding your satellite assemblies as a reference to your project, open project manifest then select Publish tab. Satellite assemblies will be displayed here in Application files. Insert a new group name for the ones that need to be downloaded on demand. By doing this step you are specifying that ClickOnce won’t download them as part of its deployment process and it will be up to the next step to where and when to download them.

Step5: Place code where you want to download assembly on demand:

To do this step you need to be familiar with ClickOnce Deployment APIs in .Net, here is a sample code to download Reosurce1.dll (satellite Assembly) on application startup based on the user machine’s culture.

using System.Deployment.Application;

using System.Threading;

using System.Globalization;

public partial class App : System.Windows.Application

{

   protected override void OnStartup(StartupEventArgs e)

   {

        base.OnStartup(e);

        Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

    GetSatelliteAssemblies(Thread.CurrentThread.CurrentUICulture.ToString());

   }

   static void GetSatelliteAssemblies(string groupName)

   {

        if (ApplicationDeployment.IsNetworkDeployed)

        {

           ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;

           if (deploy.IsFirstRun)

           {

                try

                {

                   deploy.DownloadFileGroup(groupName);

                }

                catch (DeploymentException de)

         {

                    // Log error. Do not report error to the user, as there may not be a satellite

                 }

             }

         }

     }

}