Http handlers are very interesting for several scenarios:
- Security: when you want to check custom authentication or authorization;
- Logging: when you want to trace requests to a specific page, resource, …;
In a Silverlight project based on Composite Application Guidance for Silverlight (also known as Prism) you can take advantage of ModuleCatalog and HttpHandler.
I’ll to explain you the advantage of using those 2 technologies in a dummy example.
For example, my application is built on Composite Application Guidance for Silverlight. When a client connect to the Silverlight application I really want him to get just the code (code in xap ) he needs. For example, if the client belongs to the “user” role, I don’t want him to download the xaps needed by “administrator” role.
In Composite Application Guidance for Silverlight, you can take advantage of Module Catalog to load xap files from different locations.
In fact, in the Ref attribute of ModuleInfo element, you can put an other location,
for example “../secured/ModuleZ.Silverlight.xap”. Instead of getting it from ClientBin folder, you will get the xap from “secured” directory.
For more information on Module Catalog.
Step 1 : Creation of the HttpHandler
In a web application, insert a new class named for example MyHandler. This class have to implement IHttpHandler interface.
This interface contains 2 methods :
ProcessRequest : method containing your logic;
IsReusable : method informing that the pooling is enabled;
Step 2 : Implementation of your logic
You just have to develop the logic.
In the ProcessRequest method, first of all, get the name of the desired xap.
Depending on the user role, define the correct path and than return the xap to the client application.
“C:\MyAplication\Admin\” -> folder containing admin xap
“C:\MyAplication\User\” -> folder containing user xap
Do not forget to set the ContentType to "application/x-silverlight-2".
Of course you can optimize the xap with a caching mechanism, but you have to define a mechanism to refresh it. It can be interesting when you deploy a new xap 😉
Step 3 : Register your http handler in your web application & Test
In the web.config file you just need to register your handler (sample for IIS 7.0 running in Integrated mode):
In this case, the handler will be executed for each verb and every time I’ll request a xap in Secured directory. No need to create the Secured folder.
For other IIS, I suggest you to read this article http://msdn.microsoft.com/en-us/library/ms228090(VS.100).aspx.
To test your handler, you just have to open IE and to execute your request:
Finally, you just have to run your Silverlight application. 🙂
This scenario can be solve with several other methods. My goal is just to present you the collaboration of those 2 technos.