Products

Solutions

Resources

Partners

Community

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.


Getting Started with DotNetNuke Services Framework

(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:

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:

using DotNetNuke.Web.Services;
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...

Comments

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (22)
Alec Whittington (11)
Alessandra Daniels (3)
Alex Shirley (10)
Andrew Hoefling (3)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (37)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Bogdan Litescu (1)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (213)
Chris Paterra (55)
Clint Patterson (108)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (181)
Daniel Valadas (48)
Dave Buckner (2)
David Poindexter (12)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (80)
Francisco Perez Andres (17)
Geoff Barlow (12)
George Alatrash (12)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (274)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Kelly Ford (4)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matt Rutledge (2)
Matthias Schlomann (16)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (40)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Oliver Hine (1)
Patricio F. Salinas (1)
Patrick Ryan (1)
Peter Donker (54)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Sacha Trauwaen (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott Schlesier (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Steven Fisher (1)
Tony Henrich (3)
Torsten Weggen (3)
Tycho de Waard (4)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (180)
William Severance (5)
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out