DNN Community Blog

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 community@dnnsoftware.com.

 


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


August 2014 (10)
July 2014 (17)
June 2014 (10)
May 2014 (6)
April 2014 (9)
March 2014 (3)
February 2014 (4)
January 2014 (8)
December 2013 (5)
November 2013 (2)
October 2013 (9)
September 2013 (10)
August 2013 (8)
July 2013 (4)
June 2013 (8)
May 2013 (13)
April 2013 (2)
March 2013 (7)
February 2013 (7)
January 2013 (10)
December 2012 (6)
November 2012 (20)
October 2012 (12)
September 2012 (27)
August 2012 (29)
July 2012 (22)
June 2012 (17)
May 2012 (23)
April 2012 (24)
March 2012 (27)
February 2012 (21)
January 2012 (12)
December 2011 (18)
November 2011 (20)
October 2011 (27)
September 2011 (17)
August 2011 (18)
July 2011 (45)
June 2011 (22)
May 2011 (23)
April 2011 (19)
March 2011 (36)
February 2011 (19)
January 2011 (22)
December 2010 (29)
November 2010 (37)
October 2010 (32)
September 2010 (43)
August 2010 (46)
July 2010 (37)
June 2010 (46)
May 2010 (29)
April 2010 (38)
March 2010 (27)
February 2010 (33)
January 2010 (34)
December 2009 (13)
November 2009 (20)
October 2009 (29)
September 2009 (18)
August 2009 (29)
July 2009 (19)
June 2009 (18)
May 2009 (23)
April 2009 (16)
March 2009 (13)
February 2009 (20)
January 2009 (25)
December 2008 (25)
November 2008 (29)
October 2008 (34)
September 2008 (33)
August 2008 (36)
July 2008 (31)
June 2008 (25)
May 2008 (26)
April 2008 (33)
March 2008 (31)
February 2008 (24)
January 2008 (18)
December 2007 (27)
November 2007 (51)
October 2007 (24)
September 2007 (32)
August 2007 (24)
July 2007 (20)
June 2007 (28)
May 2007 (27)
April 2007 (24)
March 2007 (47)
February 2007 (21)
January 2007 (41)
December 2006 (21)
November 2006 (16)
October 2006 (24)
September 2006 (36)
August 2006 (30)
July 2006 (31)
June 2006 (37)
May 2006 (13)
April 2006 (13)
March 2006 (18)
February 2006 (20)
January 2006 (13)
December 2005 (6)
November 2005 (15)
October 2005 (15)
September 2005 (16)
August 2005 (7)
April 2005 (1)
March 2004 (4)
February 2004 (6)
January 2004 (1)
November 2003 (4)
October 2003 (22)
September 2003 (22)
August 2003 (15)
July 2003 (14)

Copyright 2014 by DNN Corp | Terms of Use | Privacy | Design by Parker Moore Design