metadevblog

Windows Phone 7 App Development

Using Silverlight with a Windows Service – Part 1

leave a comment »

One of the big projects I have been involved in over the last couple of years had a requirement to support   tasks that could last for many hours with connections to other services that had to be kept open for the task duration.

While there are any number of possible solutions, the technology I chose to implement the application in was based around using Windows Services.  Windows Services are both very powerful but also pretty simple and while they provide a defined interface for installation and for being started and stopped they don’t have any other out of the box interfaces which can make communicating with them somewhat problematic… 

While the ability to execute very long running tasks concurrently was a primary requirement the application had to also undertake a lot of other operations and I needed an interface to expose these services.  Again there are a whole raft of different approaches that could be used but I chose WCF and specifically HTTP due to its ubiquity.

The final solution has a Windows Service based application installed on all the servers in the farm and provides a very resilient, distributed network that is scaled out simply adding additional servers running the application.   The services communicate with each other using the HTTP SOAP interface which is largely transparent to my application code as it is managed by WCF.

While most of the application is automated there is a need for administrative services that require a UI and for this I wrote a simple Windows Form application which had a WCF Service Reference to the exposed HTTP services. 

The principle UI for users was implemented using ASP.NET within SharePoint 2007.  With ASP the browser client is primarily responsible for UI and rendering while the server side code can access data and make service calls.

Requirements for a second version of the application are being put together and it is clear that the existing UI is going to need significant extensions to cover new features and while ASP.NET can handle the task I find it quite slow to author complex UI compared to Windows Forms or Silverlight.  However it has become apparent very quickly that there is an issue with Silverlight, a simple test application threw the following exception while attempting to make a service call to the Windows Service HTTP endpoint:

"An error occurred while trying to make a request to URI ‘http://server:8000/application’. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details."

This diagram shows what is being attempted.  The Silverlight XAP file is being loaded from a web server that is running on one port and then attempting to access a HTTP service running on a different port.  Note that Silverlight treats requests on different ports on the same server as being in different domains.

image

 

Silverlight has inbuilt security to prevent it making unauthorised connections to remote services which could be used by malicious programmers to gain access to secure information or launch hacking attacks.  An explanation of the security process can be found here:

http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx

In order to support cross domain access Silverlight has to request one of two policy files that have to be present on the remote server.  Silverlight will first of all request and parse its policy file: clientaccesspolicy.xml and if this is not present or is invalid it will fall back and request the Flash policy file: crossdomain.xml if this is also not present or invalid then access to the service is denied.

For conventional web servers the provision of these policy files is relatively simple.  All that has to be done is create the correct policy file and drop it at the right location on the server (at the domain root).  Silverlight will then request and parse the file.

Another option is to avoid the cross domain issue altogether by hosting the Silverlight XAP file on the same domain as the HTTP web service:

image

The problem is that WCF on Windows Service does not have the capability to serve up this file automatically and it is necessary to write a specific service to provide it and the service has to be configured correctly so that the browser can request the silverlight xap file.

Having now completed the proof of concept it turns out that both solutions can be implemented with the same code.  I’ll post some details of this in a while…

Written by metadevblog

March 22, 2011 at 4:48 pm

Posted in Silverlight

Tagged with ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: