Quantcast
Channel: hsscore Wiki Rss Feed
Viewing all articles
Browse latest Browse all 19

Updated Wiki: VirtualServices

$
0
0

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.

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

Web Site setup

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

Project bin folder
Once you install the HSS Framework, just add a reference to the HSS.ServiceModel (copy local == true)

vservice_6.png

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.

vservice_5.png


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
Framework.
vservice_1.png

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

Here are some good example of linking files:

VirtualService

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

Authentication
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.

AuthorizationAttrribute
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.

BypassAuthenticationAttrribute
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.

vservice_8.png
vservice_2.png

VirtualChannel

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

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.

vservice_3.png


Silverlight Consumer

Finally, you need to initialize a VirtualChannel for your service using your client side interface. The VirtualChannel has three basic methods to execute your desired operation:
  1. An action, with no response or result. Kind of like an async void (InvokeAsync)
  2. An action with a response, but still no 'result'. Similar to a syncronous void, we receive positive confirmation the execution completed (InvokeActionAsync)
  3. Finally, a func, which is a response that also has a result (such as a collection of objects etc.) that we may later want to bind to (InvokeFuncAsync)
Note:
  • VirtualChannel<IYourService>.Initialize uses any matching virtual service path registered in the Web App_Start, the Full Type Name of your Virtual WCF Service, and then the shared token.
  • For demonstration purposes the 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 should consolidate all VirtualChannel initialization in App.Startup

vservice_4.png


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.

Viewing all articles
Browse latest Browse all 19

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>