My Cart (0)  |  My Orders  |  My Downloads  |  My Auction  |  My Account  |  Help


Login |Register        Search

Dnn 6.2 Services Framework : Getting Started

                Print      Add To Favorite     Add To Watch List     Contact Author

Creator: host   6/4/2012 4:04:05 AM    Author: Scott S   Source: http://www.dotnetnuke.com/Resources/Blogs/EntryId/3327/Getting-Started-with-DotNetNuke-Services-Framework.aspx   Views: 3284    0    0  
Tags:
Dnn 6.2 Services Framework

by Scott S on Monday, March 19, 2012 11:34 PM  (May 22, 2012 updated to reflect the RTM of 6.2.0)

   


In 6.2 DotNetNuke is beginning a framework for creating web services that are tightly integrated in the DotNetNuke platform.  Below is a Hello World example using this new framework.  Keep watching this blog for a deeper look at the features of the Services Framework.

Creating a web service with the Services Framework is very easy.  Before you start, ensure you have installed the 6.2.0 RC or the final release when it is available.

The next step is to setup a new class library project with references to all the appropriate libraries.

  1. In Visual Studio create a new class library project for .Net Framework 3.5.0
  2. Add references to the following libraries in your installation (Browse to the /bin folder of your install using the Add Reference dialog box)
    1. DotNetNuke.dll
    2. DotNetNuke.Web.dll
    3. System.Web.Mvc.dll
  3. Add references to the following standard .Net libraries (Use the .Net tab of the Add Reference dialog box)
    1. System.Web.Abstractions
    2. System.Web.Routing
  4. Set the output path of your project to the /bin folder of you 6.2.0 installation.
  5. Delete the default Class1.cs file

Now you are ready to write your own controller, add a new class with the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
using DotNetNuke.Web.Services;
 
namespace HelloWorldServices
{
    public class WelcomeController : DnnController
    {
        [DnnAuthorize(AllowAnonymous = true)]
        public string HelloWorld()
        {
            return "Hello World!";
        }
    }
}

The first important detail in this code snippet is the class name.  ASP.Net MVC requires that the name of all controllers end with the word Controller hence the name of our class WelcomeController.  Without the word Controller at the end of the name the service will not work.

The next important detail is that we have inherited from DnnController.  This is very similar to the Asp.Net MVC Controller class but adds the various DotNetNuke framework integrations such as authentication, and access to the portal settings.

The final important detail is the use of the DnnAuthorize attribute.  Authorization is one area where the DotNetNuke Services Framework deviates substantially from ASP.Net MVC.  A standard ASP.Net MVC application essentially leaves all methods open to anonymous access by default, and then various Auth filters are applied to tighten that access where needed.  DotNetNuke has taken the opposite approach, by default all methods require Host level authorization, and then various Auth filters are appiled to loosen that access where needed.  In this case we want to allow anonymous access to our HelloWorld service.

The service itself is pretty simple, the only remaining question is what URL to use to call this service.  Those familiar with ASP.Net MVC will know that you must setup a route to make the methods on your controllers accessible to the web.  Create another class and insert the following code:

1
using DotNetNuke.Web.Services;
1
2
3
4
5
6
7
8
9
10
namespace HelloWorldServices
{
    public class MyServicesRouteMapper : IServiceRouteMapper
    {
        public void RegisterRoutes(IMapRoute routeManager)
        {
            routeManager.MapRoute("MyServices", "{controller}/{action}", new[] {"HelloWorldServices"});
        }
    }
}

Traditionally routing is done inside Global.asax. It would be very messy to update Global.asax for every service installed in a DotNetNuke site so instead we created the IServiceRoutMapper interface.  This interface allows you to register routes in a fashion very similar to ASP.Net MVC.  All routes for the services framework will be mapped to the following structure:

~/DesktopModules/<moduleFolderName>/API/<url>

Here is a break down of each of the parameters passed to IMapRoute.MapRoute:

“MyServices” – string moduleFolderName: Unique to DNN this parameter should be a name unique to your module/service, and will typically match the name of the DesktopModules folder in which your module is installed.  It is important to use a name unique to your service to avoid routing conflicts with other services that may be installed.

“{controller}/{action}” – string url: Standard ASP.Net MVC parameter that defines the unique characteristics of your route.

new[] {“HelloWorldServices”} – string[] namespaces: This is an optional ASP.Net MVC parameter that is required for DotNetNuke Services Framework.  An array of namespaces to search for controllers for this route.  This parameter helps prevent conflict between services built by different developers.

If you have been paying close attention you will notice that we no longer include a name parameter in the MapRoute method.  In some configurations it is necessary to create several RouteTable entries for each logical route, making it impossible to use the name parameter exactly as specified.  Instead the route name is based on the moduleFolderName parameter.  In the rare instance that the actual name for a route is needed, it can be found in the DataTokens dictionary on the returned Route object by using the key “name”.

Now compile the code and all that remains to do is to test your service.  There is no installation or registration required for services framework, as long as the .dll is present in the /bin folder the service will be setup when the site starts.  See step 4 above if the .dll is not in the website’s /bin folder after compilation.

To test the service simply open a browser to ~/DesktopModules/MyServices/API/Welcome/HelloWorld and your browser should load a page that says "Hello World!". Because service routes are only mapped when DotNetNuke starts up you may need to recycle the application pool if your site was already running before you compiled your service.

That is all there is to building a basic service using the DotNetNuke Services Framework.

In 6.2.0 we have focused on providing tight integration with the DotNetNuke authentication and module permissions which allow you to easily create the services to power an Ajax based UI.  Stay tuned for more posts with all the details...


Rating People: 11   Average Rating:     

     DnnModule.com is built to provide DNN quality modules and DNN skins, some of them are free, some not. We wish these stuffs (free or not ) can be useful to you.

     Besides that, we also provide a full range of professional services, ranging from web site build, seo, system management, administration, support, senior consultancy and security services. We act as if your development project or network was ours, with care and respect. We are not satisfied until it works the way you want it to, and we don't silently ignore found issues as somebody else's problem.