One of Silverlight 4’s most compelling new features is support for out-of-browser applications with elevated permissions. An app running with elevated permissions can perform actions that a normal sandboxed application can not. For example, it can access the local file system, and on Windows boxes, it can interact with COM automation servers. This latter feature—also new to Silverlight 4—is the subject of this blog post.
Silverlight 4’s ComAutomationFactory class provides an API for instantiating COM automation objects and for determining whether COM automation is available. (It’s not available if the app is running without elevated permissions, or if it’s running on a Macintosh.) And C# 4.0’s new dynamic keyword provides a means for interacting with automation objects, which by definition are late-bound (meaning they expose features to clients using an IDispatch interface).
One practical example of what you can do with Silverlight’s COM automation support is sending an e-mail message via Outlook. The following sample does just that:
dynamic outlook = ComAutomationFactory.CreateObject(“Outlook.Application”);
dynamic mail = outlook.CreateItem(0);
mail.Recipients.Add(“webmaster@contoso.com”);
mail.Subject = “Hello, Silverlight”;
mail.Body = “This message was sent from Silverlight 4”;
mail.Save();
mail.Send();
The code is exceedingly simple and it works just fine if Outlook is installed on the client and the user OKs the prompt from Outlook warning that an external application is attempting to use it. For added robustness, you should catch the System.Exception thrown from ComAutomationFactory.CreateObject if object creation fails. That’s exactly what will happen if Outlook isn’t installed on the host PC.
For fun, I wrote a downloadable demo that uses Microsoft’s speech automation server to verbalize error messages. (I definitely wouldn’t recommend using this in a production application because it is quite annoying, but it’s just novel enough to get a few laughs from your friends.) Here’s how the application looks when it’s running:
If you click a button while the application is running inside the browser or outside the browser but without elevated permissions, the error message encapsulated in the ensuing exception is displayed in a message box. However, if you do the same with the application running with elevated permissions outside the browser, a friendly voice reads the error message out loud. Nothing calls attention to a null reference exception like a female voice informing you “Object reference not set to an instance of an object.” For added fun, you could append “I’m sorry, Dave; I’m afraid I can’t do that” to every error message.
When the application starts up, it attempts to instantiate the speech server and assign a reference to a local field:
if (ComAutomationFactory.IsAvailable)
{
try
{
_speech = ComAutomationFactory.CreateObject(“Sapi.SpVoice”);
}
catch (Exception) { }
}
Then, when an exception is thrown, it forwards the exception to a helper method named NotifyException, which verbalizes the error message if possible or displays it in a message box if not:
private void NotifyException(Exception ex)
{
if (_speech != null)
_speech.Speak(ex.Message);
else
MessageBox.Show(ex.Message);
}
If you install the application locally and OK its request for elevated permissions, you should hear (rather than see) the error messages. How’s that for an error UI? 🙂
Take advantage of our free assessment and Azure credits.
We understand that evolving your IT can be costly.
We are offering you a unique opportunity to save you money and get you started faster.
Get $2,500 Azure Credits and a Free Architecture Roadmap
Get up to 5 hours of architecture work and $2,500 worth of Azure credits, free of charge.
Assess and Migrate Your Existing Environment
We right-size your systems and move them into an optimized environment.
Improve Your Information Security and Compliance
We work closely with your team to develop the right roadmap.
Atmosera cited by Gartner as a Representative Vendor in the Market Guide for Cloud Service Providers to Healthcare Delivery Organizations
We help healthcare solution providers make the most of the cloud.
We are a Gold certified Microsoft Azure service provider offering a comprehensive set of managed services.
We excel at working with care providers and their ecosystem of solution providers.
We Know Healthcare:
We are fortunate to count a number of leading care providers and healthcare vendors as our customers. We continue to grow this segment as companies turn to secure and compliant cloud solutions.
Collaborate with Experts
- Plan out your cloud strategy without having to commit all your applications to the public cloud
- Microsoft has the only viable hybrid strategy and expected to surpass AWS in market share by 2019.
- We specialize in engineering, deploying and operating the right solution for your business by combining public and private Azure.
- As one of the world’s five largest Microsoft cloud solution providers (CSP), we help you identify the optimal environment to run each application including your database and storage.
Migrate Environments
- We have expertise which minimizes redevelopment to move applications and data without recoding.
- We leverage Microsoft Azure Site Recovery (ASR) which provides a simple way to move applications and data without having to redevelop the underlying cloud infrastructure.
- We implement your new environment on a public or private cloud depending on your preferences and business requirements.
- We enable access to complete and accurate PHI for better medical care.