Developing a Provider for MPS Part 2
OK Now that you have set up your environment (if you have not check out my previous post) let’s get some code done.
On the MPS Engine Server where you installed Visual Studio open the Visual Studio IDE.
Select File/New and you should see a new type of C# project there called "MPF .NET Provider".
Type in a name for the Provider and select a location where to put it. For this exercise I will call it HelloMPF and store it in C:\MPFSDK\Providers.
Note Visual Studio will create the Folders if they are missing. Your built DLL will end up in C:\MPFSDK\Providers\HelloMPF\bin\ debug or release depending on how you build it.
When you select the Class1.cs File in the Solution Explorer you a base provider built on the Base class so that it can do all the things you expect from a MPF Provider such as receiving XML <executeData> and returning the <response> XML.
So the only thing we now need to add is the code to grab any input data, work with the data and return it back as the response.
In the Code Window expand the Public Provider Methods and uncomment or type this block of sample code
[ProviderMethodAttribute("Say Hello",
ProviderHandlerType.process,
Description="Sample C# Provider Method",
AccessType=ProviderAccessType.publicAccess)]
public void MySampleMethod(IXMLDOMNode node)
{
// get the executeData node
IXMLDOMNode executeDataNode = node.selectSingleNode("/executeXml/executeData");
// get the required elements if any
string greeting = this.GetRequiredElementValue(executeDataNode, "greeting");
Now you have the input data from the request in your provider code. Let's create the output XML with the return values
The output will return the string "MPF says " and then insert the value we retrieved from the greeting input node.
Uncomment or type this block of code
// first we need to get the owning document for creating additional elements/nodes
IXMLDOMDocument parentXML = executeDataNode.ownerDocument;
// you can create a new node but be aware that the nodes may already exist due to optional input parameters
IXMLDOMElement newRootNode = (IXMLDOMElement)executeDataNode.selectSingleNode("helloWorld");
if (newRootNode == null)
{
newRootNode = parentXML.createElement("greeting");
executeDataNode.appendChild(newRootNode);
}
// add the <greeting/> node
this.AddOrUpdateNode(parentXML, newRootNode, "greeting", "MPF says " + greeting);
Ensure that you have your closing } at the end of the function.
There are some things in the code sample that comes in the Provider Wizard that need to be changed see highlighted text in the code blocks.
Now we are ready to build the provider Hit Ctrl+Shift+B or select Build Solution from the Menu Bar.
Now we have to create the XML Description of the provider which tells MPF what the provider can do. To do that click on the ProviderNS.XML File in the solution explorer and edit it so it looks like this
<?xml version="1.0" encoding="utf-8"?>
<namespace name="HelloMPF.ProviderNS" providerSource="HelloMPF.Class1" xmlns="http://schemas.microsoft.com/MPF" xmlns:xsl=http://www.w3.org/1999/XSL/Transform
description="Say Hello">
<procedure name="Say Hello" type="read" access="public" description="Say Hello to the caller"/>
</namespace>
Save the file.
We need to register the Provider DLL for COM, open a command prompt and CD into the C:\MPSSDK\Providers\HelloMPF Folder type the following command
C:\MPFSDK\Providers\HelloMPF>\WINDOWS\Microsoft.NET\Framework\v1.1.4322\RegAsm.e
xe /codebase .\bin\Debug\HelloMPF.dll
You can safely ignore the warning now. In the 3rd installment I will talk about how to sign and strongly type the provider so that it will not throw that warning.
Now open the Provisioning Manager from the Start Menu and right click on the Namespaces Node select import Namespace and specify the path
C:\MPFSDK\Providers\HelloMPF\ProviderNS.xml
And click OK. You should now see the HelloMPF.ProviderNS on the right hand side, when you select it the Say Hello Method should be visible in the left hand window.
Last but not least we will have to create a request to call the new provider, to do that go back to the IDE and select File/New/File ... under the General Category you will find "MPF Request" select that and hit Open
Edit the Template so it looks like this
<?xml version="1.0" encoding="utf-8" ?>
<!-- Documentation on the Request XML Schema can be found at "C:\Program Files\Microsoft Hosting\Provisioning\Development Tools\Help" -->
<request xmlns="http://schemas.microsoft.com/MPF" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<data>
<greeting>hello to you</greeting>
</data>
<procedure>
<execute namespace="HelloMPF.ProviderNS" procedure="Say Hello">
<before source="data" destination="executeData" mode="merge"/>
<after source="executeData" destination="data" mode="insert"/>
</execute>
</procedure>
</request>
Back to our command prompt we are now ready to submit the request. Type the following command
C:\MPFSDK\Providers\HelloMPF>provtest /x2 Request1.xml
You should get a response that looks somewhat like this
<response>
<data xmlns="http://schemas.microsoft.com/MPF">
<greeting>hello to you</greeting>
<executeData xmlns="">
<greeting xmlns="http://schemas.microsoft.com/MPF">hello to you</greeting>
<greeting>
<greeting>MPF says hello to you</greeting>
</greeting>
</executeData>
</data>
</response>
If you get an error double-check the code and compare it to the code in the ZIP file that is attached to the post. If you are still stuck please post a reply here or better on the Forums in asp.net (see link in the links section)
I hope that this is valuable so far. In the third installment of this we will create a more useful provider, one that will let us send e-mail through MPS.
Until then happy coding
Mike