av's blog

Silverlight 4 – Accessing .NET assembly COM AutomationFactory

Posted on: June 8, 2010


I wanted to use a .NET dll which was not built against Silverlight runtime which is not possible as of today’s post date. So I tried to use Silverlight 4 COM support to act as a wrapper and it worked for me. Note that I am no expert so correct me if I am wrong.

  • Create a new C# library project.
  • Add reference to the .NET dll that needs to be accessed from Silverlight.
  • Create methods in one Class (say ‘Prj_Class’) which will call various other methods available in the .NET dll that was added as a reference.
  • Right click on the project and check the Enable COM Interop option.
  • Use SN.exe which is the located in the Microsoft SDK folder under Windows folder to create a ‘StrongNameFile’.
  • Take the .pnk file and place it in the COM project folder and add it to the project in Visual Studio using Add as Link option.
  • Build.
  • Set Regasm.exe folder path in the environment variables or use full path of the location of the file to register the .dll generated in the ‘Release’ folder of project.
  • Use as follows: –>Regasm Project_COM.dll<– and –>Regasm Project_COM.dll /tld<–
  • Regasm should be a compatible version with the target framework.
  • Now the COM is registered. The ProgID for this COM object is now ‘Project_COM.Prj_Class’. Can also go to HKLM\Software\Classes in registry and search for it.
  • If your target dll is one of those which require CAS Policy enabled then Create a sllauncher.exe.config file with the following lines:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <runtime>
 <NetFx40_LegacySecurityPolicy enabled="true"/>
 </runtime>
</configuration>

and save it in the same folder as sllauncher.exe file.

  • Now the COM object that was can be accessed from Silverlight 4 OOB application.
  • For client deployment: Download the dll files from the server and place it in Documents folder of the user and then using WScript.shell, commands can be executed to register the dll files and also copy the config file to Silverlight installed folder.

References:

If your targeted .NET dll simply returns an object then the dynamic keyword in C# will help you out there.

Hope it helps.

4 Responses to "Silverlight 4 – Accessing .NET assembly COM AutomationFactory"

Hi there, I managed to do the same thing but now I am facing a problem which I don’t know how to solve. Basically since the app I am building is Silverlight it will run on client machines that have no .net installed but silverlight 4.0 only. But in order to run my app on clients which have SL 4.0 only I need te register the COM component on the client machine with regasm.exe (the Project_COM.Prj_Clas prog id). But the regasm.exe it doesn’t run unless .net is inastalled.
Do you know how can I register this COM component on the client machine without having the .net installed?

Thank you!
Ovi

Try this: Create a .reg file and execute it on client’s PC to merge it with their registry.
Reference: http://msdn.microsoft.com/en-us/library/tzat5yw6.aspx
Will that work for you?

hmmmm, that might work, but I need to know for sure what registry keys are updating/creating regasm.exe when registering that com component.

When you run regasm on your systems that will create the registry keys, that should give you anidea about what keys are being created. Once you merge your reg file with the user’s registry, try to use the COM component and catch the exception if one is thrown, that should tell you whether the COM object was registered properly or not. Also, consider having different reg files for different OSes.

Leave a comment