Easily access information from any database and publish it via a web service – Part 2 – The Web Service
After we have configured the connection to our Database and created our model (see the last post for more details), we will jump now to the creation of our new Web Service.
Create a Web Service
Lets save our changes and add a new item. As mentioned already in my last post, I personally prefer the “older” asmx style web services most of the time. So we choose “Web Service” template for now (if you can’t find it easily, simply search for it) and give it a name. I’ll use MDT.asmx for this demo.
Now we see the source code of that newly created web service (Yes, it’s VB.Net, see my last post for more information about that) and it contains only one function called “HelloWorld”.
Lets simply rename it to “GetRoles”, as we want it to return a list of Roles, and specify what it returns exactly. In our case it’s a List of RoleIdentity.
Now we need to connect to the database. We create a simple object for that and call it “context”.
Finally we just use the context to get a all the Roles and convert them into the list that we want to return.
Now we can debug/run the web service, call this new function and voila, have a list of Roles with just two lines of code.
OK, the XML result is not really great to look at and probably doesn’t cause a wave of excitement, but as we know, it’s quite easy consumable by our scripts. More importantly, we are now able to create web service functions for all information that we would like to publish like let’s say the Roles of a computer based on it’s Mac Address
or call the Stored procedure and return a list of Packages
And a lot more. The nice thing about this way is, that you have a lot of control about what happens exactly. You can filter out unwanted stuff, do some manipulation/calculation on the items etc. This kind of web service is also a lot easier to use from a scripted environment like MDT. The “bad” thing about it is, that it’s a lot of typing. So lets see how to use a more automatic way for this.
Create a WCF Web Service
Michael Niehaus posted a quite similar example some time ago in his post using the MDT Database from a web service without writing code. Have a look in his post for some more background information and to see how we can make more use of it.
First we add a new item to our project. This time we need to choose the “WCF Data Service” template with a nice name. Let’s use MDTWCF.svc
What we see in the source now are two “TODOs”.
The first one is to specify which model to use. Well, we want to use the one we created in our last post. The next one is to specify what we would like to publish and what access we want to give via this web service. For now let’s give Read-Only access to all entities from the model. That can be achieved with a one-liner
Running this example now and pointing it to the list of Roles again, gives a slightly different look, but still the same content
The beauty of this way is, that it also supports things like filtering, paging etc. out of the box. Let’s just get the first 5 computers with “MMS” in the name and order it by MacAddress. The URL for this looks like: http://YourWebServer/MDTWCF.svc/ComputerIdentities?$filter=startswith(Description, ‚MMS‘)&$top=5&$orderby=MacAddress
and the result
Again, it looks a bit strange, but as said already, it’s not meant to be consumed by a human. It’s human readable to ease troubleshooting but is typically consumed by a script, application, etc. For a working example, that makes use of such a web service have a look at another post from Michael Niehaus about MDT 2010 wizard example role selection. You might also check out OData URI Conventions – Query String Options or Accessing Data Service Resources to get a list of the possible query strings you can use querying this WCF based Data Services.
I personally like those WCF based web services mainly for giving fast access to a data source and have all those nice features like filtering, sorting, etc. included. However if I have some restrictions on what to show or want to manipulate the data, I still prefer the “old style” asmx web services as they give me more possibilities with less code.
Rather long post and there is probably a ton more to say about this. But that should be enough to give you an idea. Again, find the full source code on CodePlex with some sample functions. Feel free to add and change whatever you like, just make sure you adjust the connection (point to the right SQL Server/Database in the connection String of the web.config as show in the post Implementing a very simple Maintenance Mode for MDT LiteTouch). If you created something really fancy/useful with it, please share it again with the community.
It’s all about contributing and giving something back.
As always, feel free to contact me with any problem or feedback you have.