Recently we released a Web service version of the
WWWCoder.com Resource Directory.
Basically this module allows you to insert our directory into your DotNetNuke site.
In this article we're going to cover some basic concepts on how to expose modules
you have written as a Web service in DotNetNuke. Our entire directory service took
around 8 hours to complete. That's pretty quick considering we released a private
assembly and fully functional directory with keyword searching, paging, categorical
listings, etc. Ok enough of our bragging, lets move on to the code.
We're going to assume for this article that you're familiar with writing modules
for DotNetNuke. So you understand what a Private Assembly is, stored procedures,
code behind pages, and your moduleDB.vb class for calling your stored procedures.
The first thing you need to do is create a Web service class as part of your module
project you want to expose as a Web service. You could also create a new project
explicitly for the Web service class if you want. Once you create your WebService.asmx.vb
class, you need to create wrapper methods to expose your moduleDB.vb class files
to the world.
Creating a Web Service Class
Let's go over the basics of our Web service class and some DNN considerations:
Imports System.Web.Services
::
::
Namespace DotNetNuke
'here is where we declare our namespace so clients can _
'create a reference to the Web service.
<System.Web.Services.WebService(Namespace:="http://tempuri.org//MyWebService")> _
Public Class MyWebService
Inherits System.Web.Services.WebService
::
::
::
End Class
End Namespace
Now that we have our class structure and we're have our DotNetNuke namespace,
we'll expose some of our methods we created for our module's moduleDB.vb class
file.
<WebMethod()> _
Public Function GetCategories(ByVal inParentID As Integer) As DataSet
Dim objCategories As New ASPSearchDB
Dim dsCategories As DataSet = _
ConvertDataReaderToDataSet(objCategories.GetCategories(thisModule, _
inParentID, True))
Return dsCategories
End Function
The previous method is a method that is contained in our DB class file for
our existing module. Now if you've done some module development in DNN, you've
probably used mostly SQLDataReaders to bind to a Data List or Data Grid. With
Web services though you can't return a Data Reader, the great thing however,
is DNN provides you with a method to convert your readers to Data Sets. Nice
hunh? So all we did in the previous method was call our existing method to
return our category listings just as it does in our regular DNN module, and then
wrap that call with the DNN method ConvertDataReaderToDataSet, then return the
converted Data Set to the requesting client via our exposed Web method.
Believe it or not it really is that simple to create a Web service in DNN.
Some caveats we ran into was to hard code our module IDs and other values to
pass to our Web service, but these could very well be exposed by your Web
service class and then let the client pass these values to ensure you're
requesting the correct module data from DNN.
On the Client
As for the client piece it is just as easy. Another thing I want you to
consider is, now that you have exposed your DNN module as a Web service, don't
limit yourself to exposing DNN data to just other DNN portals. We have plans to
expose to IBuySpy, and any .Net or SOAP enabled client. This way our directory running
under DNN gets exposed as a service to anyone requesting it. Web services allow
you to share your data with any application. Can you imagine, DNN modules
running in Perl on a Linux box?
To consume the service, in our example we created a new DNN module and
compiled it into a private assembly. To set up the project we added a new
project to our DNN project, and then added a Web reference to our Web service.
You do this in Visual Studio, by right clicking on your project references and
select Web Reference. You'll then be prompted to enter in a URL of your Web
service class file. Once the reference is created you can then work with it in
Visual Studio like any other object. Intellisense and all.
In our DNN module class we then create a reference to the Web service like
so:
Dim objWS As New com.wwwcoder.www.MyWebService
Now that the MyWebService is instantiated in our class we can refer to the
previous method GetCategories and bind the resulting Data Set to a Data List
like so:
::
::
DataList1.DataSource = objWS.GetCategories(CType(viewstate("parentid"), _
Integer))
DataList1.DataBind()
That's all there is to it, then proceed to write your module as you would any
other module.
By: Patrick Santry, Microsoft MVP (ASP/ASP.NET), developer of this site, author of books on Web technologies, and member of the DotNetNuke core development team. If you're interested in the services provided by Patrick, visit his company Website at Santry.com.