When Visual Studio .NET generates proxy classes for Web services, it names them by taking a Web service name and appending the "Binding" string to it. For example, the name of the proxy class for invoking the vzaenvm
service will be vzaenvmBinding
; t
he filer
service will have a proxy class named filerBinding
, etc.
Instantiating a proxy class (creating an object from it) is not as straightforward as creating an ordinary object in your C# program. In this section, we will create a sample class that will provide methods for creating an object from a proxy class. In addition, the methods of the class will also set up and populate the header portion of the Agent request message that will be sent to Agent. Agent request message header contains parameters that provide information on how the request should be handled on the server side. The most important of those are:
Parameter Name |
Description |
|
Agent session ID. The session is established on the server side after successful login and the session ID is returned to the client program. Each subsequent Agent request sent form the client must include this ID in order to be recognized and approved by Agent. |
|
The name of the Agent operator to which this request should be sent for processing. Each Web service has a target operator. Both the Web service and the corresponding operator have the same name. As you already know from the beginning of this section, the first part of the proxy class name is the name of the Web service, so it is the name of the target operator. For example, when invoking the Note: There's a single Web service that is an exception to this rule. The |
|
This parameter is used to specify the Server ID of the Virtuozzo Container to which the request should be routed. The parameter should be used with some of the Web services and it should be ignored with the others. We will talk in detail about request routing and will provide examples later in the tutorial. The parameter will be ignored in the beginning sections of the tutorial. |
Sample Class:
/// <summary>
/// Sample class Binder.
/// Provides methods to create the specified binding object
/// and to populate the Agent message header.
/// </summary>
public class Binder
{
string URL; // Agent server URL.
string session; // Agent session ID.
// Constructor. Sets URL and session ID values.
public Binder(string url, string sess)
{
URL = url;
session = sess;
}
/// <summary>
/// Method InitBinding (overloaded).
/// Instantiates a proxy class.
/// <param name="bindingType">
/// The System.Type object for a proxy class.
/// To obtain the object, use the typeof operator
/// with the name of the proxy class as a parameter.
/// </param>
/// <returns>
/// <para>New proxy class object.</para>
/// </returns>
/// </summary>
public System.Object InitBinding(System.Type bindingType)
{
System.Object Binding =
bindingType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
// Set URL.
bindingType.GetProperty("Url").SetValue(Binding, URL, null);
// Create the request message header object.
packet_headerType header = new packet_headerType();
// Set session ID.
header.session = session;
/* Set the "target" parameter in the Agent request
* message header. The parameter must contain the name
* of the corresponding Agent operator.
* The operator name can be obtained from the name of the
* proxy class. It is the substring from the beginning of the name
* followed by the "Binding" substring. For example, the name
* of the corresponding operator for the "filerBinding" class is
* "filer".
* All Agent requests except "system" requests must have the
* target operator value set. System is the only operator that requires
* the omission of the "target" parameter from the header.
*/
if (bindingType != typeof(systemBinding)) {
header.target = new string[1];
header.target[0] = bindingType.Name.Replace("Binding", "");
}
// Set the request message header.
bindingType.GetField("packet_header").SetValue(Binding, header);
return Binding;
}
/// <summary>
/// Method InitBinding (overloaded).
/// Instantiates a proxy class.
/// Allows to set destination Container.
/// </summary>
/// <param name="bindingType">
/// The System.Type object for a proxy class.
/// To obtain the object, use the typeof operator
/// with the name of the proxy class as a parameter.
/// </param>
/// <param name="eid">
/// The Server ID of the destination Container to which to route
/// the request message for processing.
/// </param>
/// <returns>
/// <para>New proxy class object.</para>
/// </returns>
/// </returns>
public System.Object InitBinding(System.Type bindingType, string eid)
{
System.Object Binding =
bindingType.GetConstructor(System.Type.EmptyTypes).Invoke(null);
// Set URL.
bindingType.GetProperty("Url").SetValue(Binding, URL, null);
// Create the request message header object.
packet_headerType header = new packet_headerType();
// Set session ID.
header.session = session;
/* Set the "target" parameter in the Agent request
* message header.
*/
if (bindingType != typeof(systemBinding)) {
header.target = new string[1];
header.target[0] = bindingType.Name.Replace("Binding", "");
}
// Set the destination Server ID.
header.dst.host = eid;
// Set the request message header.
bindingType.GetField("packet_header").SetValue(Binding, header);
return Binding;
}
}