(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.
- In Visual Studio create a new class library project for .Net Framework 3.5.0
- Add references to the following libraries in your installation (Browse to the /bin folder of your install using the Add Reference dialog box)
- DotNetNuke.dll
- DotNetNuke.Web.dll
- System.Web.Mvc.dll
- Add references to the following standard .Net libraries (Use the .Net tab of the Add Reference dialog box)
- System.Web.Abstractions
- System.Web.Routing
- Set the output path of your project to the /bin folder of you 6.2.0 installation.
- 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...