|
Jan
12
Written by:
Jeremiah Morrill
1/12/2010 10:38 PM
No matter how you feel about Silverlight 4’s COM interop, it does add an escape hatch out the sandbox when you are running an out-of-browser application. I don’t see many .NET folks getting excited about having to wipe off the dust on their C++ compiler and learn COM all over again for the first time. Here I want to show a way of making a COM object in pure desktop .NET and using it from Silverlight. This stuff has been around since the dawn of .NET, but it may be a little more obscure to others who have never done it. Step 1: Create a new Silverlight 4 application and add a desktop .NET class library (show here as the “COMObject” project) Step 2: Setup your Silverlight application to support out-of-browser support AND don’t forget about the elevated trust! Step 3: Get your desktop .NET class library ready for COM. In the project settings of the class library set your stuff up like this: This setting bellow is NOT required, but is there for convenience so you don’t have to manually run “regasm” to register your assembly. YOU MUST RUN VS2010 WITH ADMINISTRATOR RIGHTS IF YOU HAVE THIS OPTION! Step 4: In your desktop .NET class library add a class like this: [ProgId("SilverlightCOM.Example")] [ClassInterface(ClassInterfaceType.AutoDual)] [Guid("5267F53A-0E5F-490A-A891-FFE8B3840D72")] public class ComClass { [ComVisible(true)] public void RunMe() { System.Windows.Forms.MessageBox.Show("Hello from .NET via COM!"); } }
Make sure to give it your own GUID and ProgId!
Step 5
In your Silverlight project add some code like this:
dynamic comClass = ComAutomationFactory.CreateObject("SilverlightCOM.Example"); comClass.RunMe();
Step 6:
Run your Silverlight application and install it out-of-browser and you are done!
Conclusion:
Yeah its that easy. If you need to manually register your assembly manually, use regasm!
The source code
Tags:
23 comment(s) so far...
Re: Silverlight and Desktop .NET Integration
Great post :-)
I wanted to add that if you're building an application with Silverlight it's perhaps because you wanted the _web_ like qualities that it has such as simplicity of deployment/update, minimal dependencies, security sandboxing and cross-platform support.
As soon as you make this step out through the COM interop layer you're taking a dependency on a component that relies on the full .NET Framework so you (potentially, depending on how you go about it) make your app;
1) Windows only 2) Dependent on an installation of the full .NET Framework 3) Harder to write as you have to code through dynamic dispatch with no static typing, IntelliSense, etc.
Now...there's definitely cases where it makes sense to make that leap but there's also perhaps a line beyond which it'd make more sense to look at using WPF for the UI to avoid having to jump through hoops just to get to the functionality that the desktop .NET Framework already offers you.
Just my 2p & I appreciate that you were writing a post about "how" and not "why" here.
Mike.
By Mike on
1/13/2010 1:42 AM
|
Re: Silverlight and Desktop .NET Integration
I totally agree with you Mike. Also, you totally break the deployment model when you have to do things like register assemblies. I think the only reason I would ever do something like this is on a pretty sharp edge case.
Like almost all the stuff on my blog...it's experimental nuggets that'd you'd be crazy to use in production ;)
-Jer
By Jeremiah Morrill on
1/13/2010 3:48 AM
|
Re: Silverlight and Desktop .NET Integration
Good eye Mike. :)
Jeremiah, great post (so easy!), and I like the term you used: "escape hatch". That's something typically used only when there is no choice but to abandon ship eh.
While it does seem a little odd to use Silverlight and your own COM object (dependent on .NET no less!), I can think of at least a few reasons off the top of my head to go that route over native WPF or WinForms:
- Your app needs to be cross-platform but also needs to do more than the sandbox currently allows when it is technically possible (the "light up on Windows" scenario as Mike put it on his blog)
- You want the convenient deployment and update model for SL OOB apps, and your COM object changes very infrequently (or never)
- You want and expect your app to be 100% SL eventually, but SL's capabilities just aren't there yet, and the deficiency can be overcome via this method. It might be easier in the long run than trying to go WPF then port to SL, or maintain parallel WPF + SL versions.
Given the expected convergence of WPF and SL going into the future, I could see it being attractive to developers of certain kinds of apps to try to choose the easiest (least costly) path to get there, even if it means some extra trickiness in the short term.
In any case, I would think real hard before going this route. Certainly it's got its downsides. One thing I am 100% sure about: this WILL be used in some production apps! :)
By Tom on
1/13/2010 6:57 AM
|
Re: Silverlight and Desktop .NET Integration
This is also useful if you want to have your RIA interop with a windows only application, Like maybe your a credit vendor and want to send back a credit report to a popular desktop loan application.
COM can greatly simplify this task for you.
By Aaron Fischer on
1/15/2010 11:09 AM
|
Re: Silverlight and Desktop .NET Integration
This is the BEST way to go for intranet applications where you know what's on every machine.
By Fallon Massey on
1/15/2010 12:26 PM
|
Re: Silverlight and Desktop .NET Integration
The source code link is broken, well, actually, it's not even there.
By Fallon Massey on
1/15/2010 6:09 PM
|
Re: Silverlight and Desktop .NET Integration
Sorry Fallon. It's fixed now.
By Jeremiah Morrill on
1/15/2010 6:12 PM
|
Re: Silverlight and Desktop .NET Integration
What I been trying to figure out is how to get the trust elevation w/o requiring user interaction (out of browser , OOB). I have been experimenting with hosting silverlight based on the Microsoft C++ sample. The reasons is for large deployments to multi-user workstations, it is not acceptable to have to require each user to right click and install OOB. Plus using my own shell/host I can not easily do registryless COM. It is rare that my code can registry COM controls because the workstation logons are locked down (non admin). This is becoming very typicall in large enterprises. Your codeplex project looks very interesting. You didn't implement IControlHost2. It has a method to pass back a custom app domain to Silverlight. I have no idea what it really does for you but, may you can get elevated trust this way some how. In looking at the the install OOB apps I can see some interesting clues in the "metadata" file. The other approach may be to fake the install of an OOB app to get it working. Any thoughts?
By WillSmith on
1/28/2010 8:58 AM
|
|
|
|
Re: Silverlight and Desktop .NET Integration
I agree with you, there are some things that you would love: gucci shoes gucci handbags gucci shoes for men jordan shoes cheap jordan shoes air jordans
By uggs sale on
3/10/2010 9:40 PM
|
Re: Silverlight and Desktop .NET Integration
In the nike air max running shoes , Michael Jordan and Nike has jordan shoes outlet,played a marvelours role. Air Jordan cheap nike air max shoes brand is produced by Nike. air max shoes for sale As Air Jordan shoes fans, we wholesale nike air max shoes try to provide you the history and wholesale jordan shoes best information of cheap jordan shoes ,on the website, so that you can enjoy a culture feast when you go shopping in our store VIP Jordan Shoes. What’s more, you can find all the different Nike Air Jordan shoes in our store, some of which may be difficult to get in other’s stores. So, enjoy yourself in our store VIP Jordan Shoes. nike air jordan shoes
By cheap jordan shoes on
3/11/2010 1:41 AM
|
Re: Silverlight and Desktop .NET Integration
thank you for your great post, and I did want to share this. I hope you will also like it, if you think it is spamming, please kindly delete it. thank you!abercrombie see what is this? abercrombie and fitch uk abercrombie and fitch abercrombie fitch uk abercrombie mens abercrombie london
By abercrombie on
3/11/2010 6:54 AM
|
Re: Silverlight and Desktop .NET Integration
In the nike air max running shoes , Michael Jordan and Nike has jordan shoes outlet,played a marvelours role. Air Jordan cheap nike air max shoes brand is produced by Nike. air max shoes for sale As Air Jordan shoes fans, we wholesale nike air max shoes try to provide you the history and wholesale jordan shoes best information of cheap jordan shoes ,on the website, so that you can enjoy a culture feast when you go shopping in our store VIP Jordan Shoes. What’s more, you can find all the different Nike Air Jordan shoes in our store, some of which may be difficult to get in other’s stores. So, enjoy yourself in our store VIP Jordan Shoes. nike air jordan shoes
By air max 1 shoes on
3/13/2010 4:20 AM
|
Re: Silverlight and Desktop .NET Integration
In the air max 90 , Michael Jordan and Nike has jordan shoes outlet,played a marvelours role. Air Jordan air max shoes brand is produced by Nike. air max 1 As Air Jordan shoes fans, we wholesale nike air max shoes try to provide you the history and wholesale jordan shoes best information of cheap jordan shoes ,on the website, so that you can enjoy a culture feast when you go shopping in our store VIP Jordan Shoes. What’s more, you can find all the different Nike Air Jordan shoes in our store, some of which may be difficult to get in other’s stores. So, enjoy yourself in our store VIP Jordan Shoes. nike air jordan shoes
By air max 1 shoes on
3/13/2010 4:22 AM
|
|
|
ed hardy
ED Hardy Hoodies ED Hardy Hoodies Ed Hardy Swimwear Ed Hardy Swimwear ed hardy tops ed hardy tops ed hardy shoes ed hardy shoes ed hardy t shirt ed hardy t shirt ed hardy shirts ed hardy shirts christian audigier christian audigier ed hardy kids ed hardy kids Ed Hardy Sunglasses Ed Hardy Sunglasses ED Hardy belts ED Hardy belts ed hardy Bags ed hardy Bags ed hardy purse ed hardy purse ed hardy mens ed hardy mens ed hardy womens d hardy womens ed hardy mens shirts ed hardy mens shirts ed hardy mens tops ed hardy mens tops ed hardy mens hoodies d hardy mens hoodies ed hardy mens swim trunks ed hardy mens swim trunks ed hardy mens shoes ed hardy mens shoes ed hardy womens swimwear ed hardy womens swimwear ed hardy womens t shirt ed hardy womens t shirt ed hardy womens tops ed hardy womens tops ed hardy womens pants ed hardy womens pants ed hardy womens hoodies ed hardy womens hoodies ed hardy womens shoes ed hardy womens shoes ed hardy womens clothing ed hardy womens clothing
By ed hardy on
3/15/2010 9:37 PM
|
http://www.vipuggstores.com/
I more or less agree with you. [url=http://www.vipuggstores.com/]classic ugg boots[/url] have become very popular among women and men all over the world. As a fashion statement, [url=http://www.vipuggstores.com/ugg-5818-classic-tall-boots.html ]ugg boots uk[/url] is a great choice for people who want to give their boots an edge.
you have my vote. [url=http://www.chiflatiron4u.com]chi flat iron[/url] is the Best For Hair Straightening and it will make your hair most good-looking and you will be high-handed of.
Oh, I agree with you. A [url=http://www.topghdmart.com]ghd hair iron[/url] Can Make It Easy To Style Your Hair. [url=http://www.topghdmart.com/special-ghd-hair-iron/ghd-kiss-pink-mk-iv-hair-straightener-and-lip-glosses-set-limited-edition-for-2008.html ]ghd hair straightener[/url] will make your hair get it looking its best.
By ugg boots sale on
3/15/2010 10:39 PM
|
cheap nike shox
There are wide variety of Nike Shox for both men and women's selection.The Nike Shox Sale are best selling nowadays.Get your own Cheap Nike Shox now.It's definitely time for you to buy womens nike shox and wens nike shox, Wearing them will keep you in a perfect condition.
By nike shox on
3/16/2010 10:45 PM
|
|
2010 AT Nike's language is the language of movement. Three years later, the company has committed to create every opportunity to show themselves. Nike know: Only the use of advanced technology to produce the best product. Nike's first <a href="htt
2010 AT Nike's language is the language of movement. Three years later, the company has committed to create every opportunity to show themselves. Nike know: Only the use of advanced technology to produce the best product. Nike's first Air Max 90 technology to bring about a revolution in the sports sector. The use of this technology to produce sports shoes can protect the athlete's body very well, especially ankle and knee, to prevent its movement as severe sprains, to reduce the impact on the knees and wear. Athletic nike air max 2009 with air cushion technology been introduced very popular. Consumers and professional athletes love it. In 2001, the Air Max 95 shoes technology in the developed and later introduced a new type called Shox cushioning technology. Produced using this technology as the popular sport shoes, sales have risen steadily. In addition to other sports shoes, Nike Shox apparel, sometimes innovative.
By Nike Shoes on
9/25/2010 2:20 AM
|
|
|
|