This section uses the SendMail Sample provider, which is located
by default at <SystemDrive> Program Files\Microsoft
Hosting\Development\Samples\CSharp\1.1\, to introduce how to write
a provider in Visual Studio 2003.
In general, there are three steps to perform within the
Provider.cs file.
- Gather all the input data, if any
- Process the data that has come in, if any
- Process the output data for the end user to see
Near the top of the Provider.cs file is the header, which is
where you must define the name of the method. This name will show
up in MPS as a method which can be selected. In the case of the
SendMail Provider, the name of the method is Send Mail. Other
properties, such as Description and AccessType, can be included in
the header as well:
[ProviderMethodAttribute("Send Mail", ProviderHandlerType.process)]
The provider begins by getting the input data. The executeData
node is defined first, then the subnodes are placed directly under
the executeData node. That section of the SendMail Provider sample
looks like the following:
// Get the executeData node
IXMLDOMNode executeDataNode = node.selectSingleNode("/executeXml/executeData");
// get the required elements
string recipient = this.GetRequiredElementValue(executeDataNode, "recipient");
string sender = this.GetRequiredElementValue(executeDataNode, "sender");
string subject = this.GetRequiredElementValue(executeDataNode, "subject");
string messageText = this.GetRequiredElementValue(executeDataNode, "messageText");
// get the optional elements
string messageFormat = this.GetOptionalElementValue(executeDataNode, "messageFormat");
string messagePriority = this.GetOptionalElementValue(executeDataNode, "messagePriority");
string smtpServer = this.GetOptionalElementValue(executeDataNode, "smtpServer");
string smtpUser = this.GetOptionalElementValue(executeDataNode, "smtpUser");
string smtpPassword = this.GetOptionalElementValue(executeDataNode, "smtpPassword");
The GetRequiredElementValue and GetOptionalElementValue methods
are public helper methods that the user can see in the lower
sections of the code. After grabbing the input data, the next step
is to process the data and assign it to a response object that we
can use for the output. This processing is assigned in the
following statement from our SendMail sample:
string response = this.SendTheMail(sender, recipient, subject, messageText,
messageFormat, messagePriority, smtpServer, smtpUser, smtpPassword);
The SendMail sample calls the SendTheMail method, which processes
the input and returns a simple response. So the only step remaining
for the Provider.cs file is to return the output. The SendMail
sample looks like this:
AddOutputNode(node.ownerDocument, (IXMLDOMElement)executeDataNode, "status",
response);
In this statement, the executeDataNode is sent, then the code
defines the name of the node that will contain the response
(status), and then finally the text of the response is declared.
Lastly, the project is built (CTRL+SHIFT+B).
Prepare the Namespace File
In order for MPS to know what the provider can do, the next step
is to create the XML description of the provider. Open the
Provider_NS.XML file in the SendMail sample project. This
particular file is longer than required because of the schema
information that is included in the sample. The only required part
of the file is the upper header portions.
After the XML file is finished, save it. Open Provisioning
Manager and import the SendMail namespace. After importing the
namespace, the entry "SDK Send Mail Provider" should appear in the
left-hand column. When you clicking SDK Send Mail Provider, a
procedure named Send Mail should appear in the right-hand column.
The last thing to do is create a "SendMail" request that will call
the provider that has been created. In order to start a new
request, in Visual Studio, use one of the new Item Templates
provided in the SDK. Click File, and then click New File, and then
open the template for MPSRequest. A sample request file for the
SendMail provider is shown below:
<namespace name="SDK Send Mail Provider"
providerSource="SendMailProvider.Provider"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
description="">
<procedure name="Send Mail" type="write" access="private" description="Sends a mail message against an SMTP server">
<Schema name="inputSchema">
After saving the request, go to a command prompt and run the
request. The response will be enveloped in the "status" node and
should say, "Your message was sent successfully."