WebServicesClientProtocol.RequestSoapContext Property

Gets security, routing and other XML Web services architecture specific information associated with a SOAP request. Gets security, routing, and other XML Web services architecture-specific information associated with a SOAP request.

Namespace: Microsoft.Web.Services2
Assembly: Microsoft.Web.Services2 (in microsoft.web.services2.dll)

Usage

'Usage
Dim webServicesClientProtocol1 As WebServicesClientProtocol

Dim returnValue As SoapContext
returnValue = webServicesClientProtocol1.RequestSoapContext

Syntax

'Declaration
Public ReadOnly Property RequestSoapContext() As SoapContext
public SoapContext RequestSoapContext {get;}
public: property SoapContext^ RequestSoapContext{
    SoapContext^ get();
}
public SoapContext get_RequestSoapContext();
public function get RequestSoapContext() : SoapContext;

Property Value

A SoapContext containing XML Web services architecture specific information specific to a SOAP request.

Property Value

A SoapContext containing information about a SOAP request.

Example

The following code example communicates with a Web service using a SOAP message digitally signed with an X.509 certificate.

private void buttonAdd_Click(object sender, System.EventArgs e)
{
   int a =3, b=3;            
            
   // Get the X509 certificate.
   X509SecurityToken token = GetSecurityToken();

   // Verify an X509 certificate was returned.
   if (token == null)
      return;

   // Call the method that communicates with the XML Web service.
   try
   {
      // Create a new instance of the proxy class.
      AddNumbers serviceProxy = new AddNumbers();

      // Get the SoapContext for the SOAP request.
      SoapContext requestContext = serviceProxy.RequestSoapContext;

      // Expire this message one minute after it is sent.
      requestContext.Security.Timestamp.TtlInSeconds = 60;
               
      // Add the X509 certificate to the WS-Security header.
      requestContext.Security.Tokens.Add(token);

      // Sign the message using the X509 certificate.
      requestContext.Security.Elements.Add(new Signature(token));

           
      // Call the method that communicates with the XML Web service.
      int sum = serviceProxy.AddInt(a, b);

      // If we got here, the message was successfully processed.
      string message = string.Format("{0} + {1} = {2}", a, b, sum);
      // Display a MessageBox containing the results from the XML Web service.
      MessageBox.Show(message, "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
   catch (System.Web.Services.Protocols.SoapException se) 
   {
     Error("SOAP Exception caught while invoking an XML Web service.", se);
   }
   catch (Exception ex) 
   {
     Error("Exception caught while invoking an XML Web service.", ex);
     return;
   }
}
 
// Gets the security token for signing messages.
public X509SecurityToken GetSecurityToken()
{            
  if (securityToken == null) 
  {
    X509CertificateStore store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
    bool open = store.OpenRead();

    try 
    {
      // Display dialog for user to select the certificate from the local machine's certificate store.
      X509Certificate cert = store.SelectCertificate(this.Handle, "Select Certificate", "Choose a Certificate below for signing.");
      // Verify user chose a certificate.
      if (cert == null) 
      {
        Error("You chose not to select an X.509 certificate for signing your messages.");
        securityToken = null;
      }
      // Verify user has the private key and the certifcate supports digital signatures.
      else if (!cert.SupportsDigitalSignature || cert.Key == null) 
      {
        Error("The certificate must support digital signatures and have a private key available.");
        securityToken = null;
      }
      else 
      {
        securityToken = new X509SecurityToken(cert);
      }
    } 
    finally 
    {
      // Close the certificate store.
      if (store != null) { store.Close(); }
    }
  }
  return securityToken;            
}

// Displays a MessageBox to the user containing the cause of an exception.
private void Error(string s)
{
   Error(s, null);
}

// Displays a MessageBox to the user containing the cause of an exception.
private void Error(string s, Exception e) 
{
  StringBuilder sb = new StringBuilder();
  sb.Append(s);
  if (e != null) 
  {
    sb.Append("\r\n");
    sb.Append(e.Message);
    sb.Append("\r\n");
    sb.Append(e.StackTrace);
  }
  if (e is System.Web.Services.Protocols.SoapException) 
  {
    System.Web.Services.Protocols.SoapException se = e as System.Web.Services.Protocols.SoapException;
    sb.Append("\r\n");
    sb.Append("SOAP-Fault code: " + se.Code.ToString());
  }
  MessageBox.Show(sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }

The following code example communicates with an XML Web service using a SOAP message digitally signed with an X.509 certificate.

private void buttonAdd_Click(object sender, System.EventArgs e)
{
   int a =3, b=3;            
            
   // Gets the X.509 certificate.
   X509SecurityToken token = GetSecurityToken();

   // Verifies that an X.509 certificate was returned.
   if (token == null)
      return;

   // Calls the method that communicates with the XML Web service.
   try
   {
      // Creates a new instance of the proxy class.
      AddNumbers serviceProxy = new AddNumbers();

      // Gets the SoapContext for the SOAP request.
      SoapContext requestContext = serviceProxy.RequestSoapContext;

      // Sets this message to expire one minute after it is sent.
      requestContext.Security.Timestamp.TtlInSeconds = 60;
               
      // Creates a WS-Security SOAP header.
      Security security = new Security();

      // Adds the X.509 certificate to the WS-Security header.
      security.Tokens.Add(token);

      // Signs the message using the X.509 certificate.
      security.Elements.Add(new Signature(token));

      // Adds the WS-Security header to the SOAP message.
      requestContext.Security.Add(security);
            
      // Calls the method that communicates with the XML Web service.
      int sum = serviceProxy.AddInt(a, b);

      // If we get here, the message was successfully processed.
      string message = string.Format("{0} + {1} = {2}", a, b, sum);
      // Displays a MessageBox containing the results from the XML Web service.
      MessageBox.Show(message, "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
   }
   catch (System.Web.Services.Protocols.SoapException se) 
   {
     Error("SOAP exception caught while invoking a Web service.", se);
   }
   catch (Exception ex) 
   {
     Error("Exception caught while invoking a Web service.", ex);
     return;
   }
}
 
// Gets the security token for signing messages.
public X509SecurityToken GetSecurityToken()
{            
  if (securityToken == null) 
  {
    X509CertificateStore store = X509CertificateStore.LocalMachineStore(X509CertificateStore.MyStore);
    bool open = store.OpenRead();

    try 
    {
      // Displays a dialog box for the user to select the certificate from the local computer's certificate store.
      X509Certificate cert = store.SelectCertificate(this.Handle, "Select Certificate", "Choose a certificate for signing.");
      // Verifies that the user chose a certificate.
      if (cert == null) 
      {
        Error("You chose not to select an X.509 certificate for signing your messages.");
        securityToken = null;
      }
      // Verifies that the user has the private key and the certifcate supports digital signatures.
      else if (!cert.SupportsDigitalSignature || (cert.Key == null )) 
      {
        Error("The certificate must support digital signatures and have a private key available.");
        securityToken = null;
      }
      else 
      {
        securityToken = new X509SecurityToken(cert);
      }
    } 
    finally 
    {
      // Closes the certificate store.
      if (store != null) { store.Close(); }
    }
  }
  return securityToken;            
}

// Displays a MessageBox to the user containing the cause of an exception.
private void Error(string s)
{
   Error(s, null);
}

// Displays a MessageBox to the user containing the cause of an exception.
private void Error(string s, Exception e) 
{
  StringBuilder sb = new StringBuilder();
  sb.Append(s);
  if (e != null) 
  {
    sb.Append("\r\n");
    sb.Append(e.Message);
    sb.Append("\r\n");
    sb.Append(e.StackTrace);
  }
  if (e is System.Web.Services.Protocols.SoapException) 
  {
    System.Web.Services.Protocols.SoapException se = e as System.Web.Services.Protocols.SoapException;
    sb.Append("\r\n");
    sb.Append("SOAP-Fault code: " + se.Code.ToString());
  }
  MessageBox.Show(sb.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
      }

Remarks

XML Web services clients use the RequestSoapContext property to set properties of the SoapContext associated with SOAP requests. The SoapContext provides access to the contents of SOAP headers defined in the WS-Security, WS-Routing, and WS-Timestamp specifications. For instance, the Security property allows a client to provide authentication credentials, XML digital signatures, and encryption details that are sent in SOAP headers defined by WS-Security.

Remarks

XML Web services clients use the RequestSoapContext property to set properties of the SoapContext associated with SOAP requests. The SoapContext provides access to the contents of SOAP headers defined in the WS-Security and WS-Routing specifications. For instance, the Security property allows a client to provide authentication credentials, XML digital signatures, and encryption details that are sent in SOAP headers defined by WS-Security.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Platforms

Development Platforms

Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Longhorn, and Windows 2000

Target Platforms

Windows 2000, Windows 2000 Server, Windows 2000 Advanced Server, Windows XP Home Edition, Windows XP Professional, Windows Server 2003, Windows Longhorn, Pocket PC, Windows CE, Smart Phone

See Also

Reference

WebServicesClientProtocol Class
Microsoft.Web.Services2 Namespace

Other Resources

WebServicesClientProtocol Members