Today is the day to talk about COM possibilities in Silverlight 4. One would question the titles of my post. COM and Cool in the same sentence? Let me proof this title to you.
First, let me take a closer look at COM. In order to access COM, you must install Silverlight application on a local machine. The access to COM is not enabled when a Silverlight application is run in the browser. So, first of all you have to enable this by checking “Enable running application out of browser” checkbox in project properties. Second of all, you have to check “Require elevated trust…” checkbox in “Out-of-browser settings” area in project properties. Now, you are ready to install you application and test COM support. How about Word automation:
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();var doc = word.Documents.Add();var paragraph = doc.Paragraphs.Add();paragraph.Range.Text = "Some text";
Now, let’s talk about database access. Parts of System.Data.SqlClient namespace are not exposed to COM by default. So, to enable database access we must write an assembly that is exposed to COM that wraps database access. To do so, check the property “Register for COM Interop” in project properties for the .NET (NOT Silverlight) based project that will fire off database queries for us. Once the assembly is built, I can just use RegAsm to register my DLL with COM on a machine. Of course, this would be a prerequisite to use Silverlight application for local data access. The other prerequisite is to have .NET Runtime installed on that machine as well. I could write an install project of course to make this process easier.
The next step is to write a Silverlight assembly that would use COM Interop similar to the one above for Word to talk to my database access .NET based assembly.dynamic sqlDB = ComAutomationFactory.CreateObject("COMSQLClient.COMSqlDatabase");
Now the most exciting part. I created and published the project on CodePlex that does exactly what I just talked about. Check out the project and let me know what you think. You can download the source code and look into the implementation details. Here is the link to it: