This project is read-only.

Silverlight and WCF Virtual Service and Virtual Channel sample application

Below is an example of how to implement a VirtualService and then consume it from Silverlight using a VirtualChannel.
Begin by creating a new Silverlight application, and check to include an ASP.NET Web Site.

  • Silverlight 4.0
  • .NET Framework 4.0
  • Visual Studio 2010

NOTE: April 3rd, 2011

For non-referenced assemblies you must include the assembly name during the VirtualChannel Initialization. Types FullName, a comma, and then the Assembly Name ( Namespace.Type,AssemblyName )

Example: VirtualChannel<IVService1>.Initialize("vservices", "TestVirtualService.VService1,TestVirtualService", "test");

Web Site setup

Web Configuration
all that is necessary in the web config, is to turn on aspnet compatibility

Project bin folder
Once you install the HSS Framework, just add a reference to the HSS.ServiceModel


ASP.NET Global.asax Application_Start:
So here we initialize the VirtualServiceProvider by telling it if we require SSL and what is the shared token required to gain access to a Service.
A good practice here would be to set SSL equal to FormsAuthentication.RequiresSLL property value. Then
we register our virtual folder (virtual url path). You can register more than one to segregate your services uri logically.


Shared Model - Server Side
Here we define our Model class in our server side project. Our data access code only runs if not Silverlight. This functionality could be separated
out into a different file using a partial class. This is where DOMGen (Data Object Model Generator) is very useful and is included with the HSS Core

Shared Model - Client Side
Then we add this Model class to our client side project by linking a copy to the original file.

Here are some good example of linking files:


Service Implementation
So now just create your Interface and implementation as usual with any other standard WCF based service.

By default all calls require an authenticated user. This is built in to the framework and uses the HttpContext user
to provide easy integration into any web application scenario.

You can add an AuthorizationAttribute to specify the required role(s) required to execute a method. You
can place this attrribute on the class to specify a given role is required to execute any method. You can also
apply to a method to further define any additional roles required to execute a specific method.

For method calls where you don't require an authenticated user you can decorate them with the BypassAuthentication
attribute. This can be applied on the class or an individual method. Caution, if you specify at the class level, this will
bypass the Authentication validation and any AuthorizationAttributes.

Your service can be defined directly in the web site or in an external library.
If in an external library the dll must located in the web sites bin folder.

It is also highly recommended to make your services per-call for scalibilty purposes, but may not be necessary for your particular service.



Be sure to add a reference to the HSS.ServiceModel.Silverlight library.

June 2011 - v4.3.200
Client Side Interface (no implementation required, but must be async)
So, now the magic happens here. We expose our WCF Service by simply defining our service contract on the client. Since this is for Silverlight
we must use the async pattern. We do this by defining an asyncronous version of our server side interface. Note the AsyncPattern=true attribute
this is necessary for the internal .NET ChannelFactory.

A client side async interface is no longer required.
You will need to link or duplicate the sync interface in your client application (see file linking above).

Example of new operation calling:
VirtualChannel<ITestService>.Invoke(s => s.InvokeActionAsyncTest());

Silverlight Consumer

Finally, you need to initialize a VirtualChannel for your service interface. The VirtualChannel consists mainly of the initialize method and the invoke method and it's overrides.

  1. Operation is an action (with and without user state)
  2. Operation is an func (with and without user state)

  • VirtualChannel<IYourService>.Initialize uses any matching virtual service path registered, the Full Type Name ( and optionall Assembly Name for non-referenced services) of your Virtual WCF Service, and then the shared token.
  • For demonstration purposes the below call to Initialize is done in the constructor of the main page, but if you have more than one service or a more complex SL Application, you could consolidate all VirtualChannel initialization in App.Startup or BootStrapper.


Build, and then hit F5 and you should be all set...
Please use the discussion or issue list for comments or if you have questions.

Last edited Jun 23, 2011 at 5:32 AM by hyspdrt, version 11


No comments yet.