Saturday, September 3, 2011

Silverlight 5 : In browser application for accessing COM objects

I have been working on Silverlight for some time now and particulary silverlight 5, elevated trust in "In-browser" applications. So trying to spread whatever knowledge I have on it

There are 2 ways of achieving COM objects access in your silverlight application
1. If you host your silverlight application on a website, you will get that access automatically.
you should select "host on a website" option while creating silverlight application in Visual studio.

2. If you are not planning to host it on a website and just want an HTML file or just silverlight application running in browser, following are the steps

  • Create a silverlight 5 project without selecting host on a website option
  • Type "regedit" in windows->run , registry editor opens, navigate to HKEY_LOCAL_MACHINE->Software->Microsoft->silverlight Double click on silverlight. You should create a new registry variable now right click and select new->DWORD(32-bit value) . Give name as "AllowElevatedTrustAppsInBrowser" and set value as 1
  • In project->properties, navigate to silverlight tab, check "Enable running application out of browser". Click on out of browser settings, in that check "Require elevated trust when running outside the browser".
  • Project->properties, navigate to Debug tab and select Dynamically create a web page or specific page and choose your HTML file which embeds silverlight application.
  • Project->properties, navigate to Signing, check Sign the XAP file, click on Create test certificate, you will be asked to enter password. After that, click on more details, install the certificate in Trusted publishers and Trusted root certification authorities stores.
Now you can design your interface with XAML and give functionality with C#, Code to access COM objects are as follows.

A reference to Microsoft.Csharp should be added.

using System.Runtime.InteropServices.Automation;

dynamic com=com = AutomationFactory.CreateObject("Word.Application");

word.Visible = true;

dynamic doc = word.Documents.Add();

string insertText = "Hello World from Silverlight 5!";
dynamic range = doc.Range(0, 0);
range.Text = insertText;

This code opens Microsoft word, creates new document and inserts "hello world from silverlight 5!" text.

Now when you run this application, it runs in-browser and performs the operation.

After developing the application, you would like to deploy it in some client system, how do you do it.

1. same step as explained above for "regedit", add that registry value.
2. You should also install certificate, but while developing Visual studio has done certificate creation and signing XAP file just by click of buttons, you have to do this step manually for deploying it to other system.
Steps to create certificate and signing XAP file.

Run Visual studio command line tools as administrator and type the following commands.
  • makecert -r -pe -n "CN=sampleapplication" -ss bharath -sr CurrentUser -a sha1 -sky signature -sv sample.pvk sample.cer
  • pvk2pfx -pvk sample.pvk -spc sample.cer -pfx sample.pfx
  • signtool sign /v /f sample.pfx C:\Users\bharath\Desktop\sample.xap
you will find sample.cer and sample.pfx certificate files C:\windows\system32 folder.
copy those 2 files along with XAP + HTML file to the client system where you would like to run it.

double click on sample.cer file to install it in Trusted root certification authorities store
and double click on sample.pfx file to install it in Trusted Publishers store.

now you can open HTML file in-browser and your application will run fine.

No comments:

Post a Comment