Simple WSE TCP Web Service in C#.

There are many WSE examples out there.  However, you can never seem to find a really small and simple example to get you started or remind you how to do one when you forget.  For this reason, I post here a very simple WSE service using TCP as transport so you don’t require IIS to host it.  I could have used a console app to show this, but used a Windows Form app instead.  It is basically two classes.  The Service.cs is the WSE service that get registered and listens for requests.  The ServiceClient.cs is the proxy client that calls the service.  The Form1.cs just strings the two together in a GUI to show example how to start a service and call a service.  HTH

–William Stacey [MVP] 

// Service.cs – WSE Web Service class.
using System;
using System.Collections;

using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Addressing;
using Microsoft.Web.Services2.Messaging;

namespace WSESimpleTCP
{
 //
 // This class inheritits from SoapService and represents
 // a specific implementation of a Web service.
 //
 //[SoapActor("soap://service.contoso.com/MyServiceV1")]
 public class Service : SoapService
 {
  private const string host = "wjs.mvptools.com"; // Note: does not work with "." at end.
  private const string viaString = "soap.tcp://" + host + "/MyService";
  private const string addrString = "soap://service.contoso.com/MyServiceV1";
  private static bool started = false;

  [SoapMethod("Ping")]
  public string Ping(string msg)
  {
   return msg;
  }

  [SoapMethod("GetDateString")]
  public string GetDateString()
  {
   return DateTime.Now.ToString();
  }

  public static void Start()
  {         
   //
   // Add the System.Type of the service (Service) to the SoapReceivers collection.
   // This will start a TCP-based listener if there isn’t one already started.
   //
   // Because we need to use Uri’s the Uri’s need to be in a valid Uri format
   // so we can’t just use something like "MyServiceV1" for address.
   // Also the address Uri must match the Classes SoapActor attribute if supplied
   // on the class.
   if ( started )
    return;
   EndpointReference endpoint = new EndpointReference(new Uri(addrString), new Uri(viaString));
   SoapReceivers.Add( endpoint, typeof(Service) );
   started = true;
   Console.WriteLine( "Listening at transport: " + endpoint.TransportAddress.ToString());
   Console.WriteLine( "Listening for messages at: " + endpoint.Address.Value );
  }

  public static void Stop()
  {
   if ( ! started )
    return;
   EndpointReference endpoint = new EndpointReference(new Uri(addrString), new Uri(viaString));
   SoapReceivers.Remove(endpoint);
   started = false;
  }
 }
}

// ServiceClient.cs
using System;

using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Addressing;
using Microsoft.Web.Services2.Messaging;

namespace WSESimpleTCP
{
 //
 // This class derives from SoapClient and is a specific implementation
 // of a client of the Service Web service.
 //
 public class ServiceClient : SoapClient
 {
  public ServiceClient( EndpointReference endpoint ) : base( endpoint ){}

  [SoapMethod("Ping")]
  public string Ping(string msg)
  {
   return (string)base.SendRequestResponse("Ping", msg).GetBodyObject(typeof(string));
  }

  [SoapMethod("GetDateString")]
  public string GetDateString()
  {
   return (string)base.SendRequestResponse("GetDateString", "").GetBodyObject(typeof(string));
  }
 }
}

// Form1.cs – Simple Windows Form to host both the server and client to Demo
// the simple web service
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;

using Microsoft.Web.Services2;
using Microsoft.Web.Services2.Addressing;
using Microsoft.Web.Services2.Messaging;

namespace WSESimpleTCP
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private const string host = "wjs.mvptools.com";
  private ServiceClient client = null;

  private System.Windows.Forms.GroupBox groupBox1;
  private System.Windows.Forms.GroupBox groupBox2;
  private System.Windows.Forms.Button btnStart;
  private System.Windows.Forms.Button btnStop;
  private System.Windows.Forms.Button btnGetDate;
  private System.Windows.Forms.Button btnPing;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  public ServiceClient Client
  {
   get
   {
    if ( this.client == null )
    {
     // via is the network endpoint describing host, transport, and port.
     // address is the WSE endpoint.
     Uri via = new Uri("soap.tcp://" + host + "/MyService");
     Uri address = new Uri("soap://service.contoso.com/MyServiceV1");
     this.client = new ServiceClient(new EndpointReference(address, via));
    }
    return this.client;
   }
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support – do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.groupBox1 = new System.Windows.Forms.GroupBox();
   this.btnStop = new System.Windows.Forms.Button();
   this.btnStart = new System.Windows.Forms.Button();
   this.groupBox2 = new System.Windows.Forms.GroupBox();
   this.btnGetDate = new System.Windows.Forms.Button();
   this.btnPing = new System.Windows.Forms.Button();
   this.groupBox1.SuspendLayout();
   this.groupBox2.SuspendLayout();
   this.SuspendLayout();
   //
   // groupBox1
   //
   this.groupBox1.Controls.Add(this.btnStop);
   this.groupBox1.Controls.Add(this.btnStart);
   this.groupBox1.Location = new System.Drawing.Point(264, 16);
   this.groupBox1.Name = "groupBox1";
   this.groupBox1.Size = new System.Drawing.Size(200, 120);
   this.groupBox1.TabIndex = 0;
   this.groupBox1.TabStop = false;
   this.groupBox1.Text = "WSE Service";
   //
   // btnStop
   //
   this.btnStop.Location = new System.Drawing.Point(104, 32);
   this.btnStop.Name = "btnStop";
   this.btnStop.TabIndex = 1;
   this.btnStop.Text = "Stop";
   this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
   //
   // btnStart
   //
   this.btnStart.Location = new System.Drawing.Point(16, 32);
   this.btnStart.Name = "btnStart";
   this.btnStart.TabIndex = 0;
   this.btnStart.Text = "Start";
   this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
   //
   // groupBox2
   //
   this.groupBox2.Controls.Add(this.btnGetDate);
   this.groupBox2.Controls.Add(this.btnPing);
   this.groupBox2.Location = new System.Drawing.Point(16, 16);
   this.groupBox2.Name = "groupBox2";
   this.groupBox2.Size = new System.Drawing.Size(232, 120);
   this.groupBox2.TabIndex = 1;
   this.groupBox2.TabStop = false;
   this.groupBox2.Text = "WSE Client Proxy Methods:";
   //
   // btnGetDate
   //
   this.btnGetDate.Location = new System.Drawing.Point(120, 40);
   this.btnGetDate.Name = "btnGetDate";
   this.btnGetDate.TabIndex = 1;
   this.btnGetDate.Text = "GetDate";
   this.btnGetDate.Click += new System.EventHandler(this.btnGetDate_Click);
   //
   // btnPing
   //
   this.btnPing.Location = new System.Drawing.Point(24, 40);
   this.btnPing.Name = "btnPing";
   this.btnPing.TabIndex = 0;
   this.btnPing.Text = "Ping";
   this.btnPing.Click += new System.EventHandler(this.btnPing_Click);
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(480, 165);
   this.Controls.Add(this.groupBox2);
   this.Controls.Add(this.groupBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.groupBox1.ResumeLayout(false);
   this.groupBox2.ResumeLayout(false);
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void btnStart_Click(object sender, System.EventArgs e)
  {
   Service.Start();
   this.btnStart.Enabled = false;
   this.btnStop.Enabled = true;
  }

  private void btnPing_Click(object sender, System.EventArgs e)
  {
   Console.WriteLine("Calling {0}", Client.Destination.Address.Value);
   string reply = Client.Ping("Hello");
   Console.WriteLine(reply);
  }

  private void btnStop_Click(object sender, System.EventArgs e)
  {
   Service.Stop();
   this.btnStart.Enabled = true;
   this.btnStop.Enabled = false;
  }

  private void btnGetDate_Click(object sender, System.EventArgs e)
  {
   Console.WriteLine("Calling {0}", Client.Destination.Address.Value);
   string reply = Client.GetDateString();
   Console.WriteLine(reply);
  }
 }
}

Advertisements
This entry was posted in C#. Bookmark the permalink.

20 Responses to Simple WSE TCP Web Service in C#.

  1. Unknown says:

    Serena Williams and the season of her contentwow gold,world of warcraft gold,buy wow gold,wow power leveling,cheap wow gold,world of warcraft power
    leveling,world of warcraft gold,buy wow
    gold,ffxi gil,buy
    wow gold,wow power
    leveling,ffxi gil,world
    of warcraft power leveling,sell wow gold,wow power
    level,wow gold for
    sale,power leveling,wow
    power level,wow power level,power leveling,wow gold for
    sale,buy cheap wow gold.Indian brides confront gold bears, again

Comments are closed.