这是一个WebService的简单例子,.aspx是一个Web Client,小弟想把它改成一个Windows Application的Client,不知道怎么写?另外,如果希望由这个WebService自动通报所有的Client这个WebService存储的String已经更新了,要用Polling怎么实现呢?.asmx<%@ WebService Language="C#" Class="MathService" %>using System;
using System.Web.Services;public class MathService : WebService {   [WebMethod]
   public float Add(float a, float b)
   {
       return a + b;
   }   [WebMethod]
   public float Subtract(float a, float b)
   {
       return a - b;
   }   [WebMethod]
   public float Multiply(float a, float b)
   {
       return a * b;
   }   [WebMethod]
   public float Divide(float a, float b)
   {
       if (b==0) return -1;
       return a / b;
   }}
.aspx<%@ Import Namespace="MathService" %>
<html>
<script language="C#" runat="server">
  float operand1 = 0;
  float operand2 = 0;  public void Submit_Click(Object sender, EventArgs E)
  {
      try
      {
        operand1 = float.Parse(Operand1.Text);
        operand2 = float.Parse(Operand2.Text);
      }
      catch (Exception) { /* ignored */ }      MathService service = new MathService();
        switch (((Control)sender).ID)
        {
          case "加"      : Result.Text = "<b>结果</b> = " + service.Add(operand1, operand2).ToString(); break;
          case "减" : Result.Text = "<b>结果</b> = " + service.Subtract(operand1, operand2).ToString(); break;
          case "乘" : Result.Text = "<b>结果</b> = " + service.Multiply(operand1, operand2).ToString(); break;
          case "除"   : Result.Text = "<b>结果</b> = " + service.Divide(operand1, operand2).ToString(); break;
        }
  }
</script><body style="font: 10.5pt 宋体">  <h4>使用简单的数学服务 </h4>  <form runat="server">  <div style="padding:15,15,15,15;background-color:beige;width:300;border-color:black;border-width:1;border-style:solid">    操作数 1:<br><asp:TextBox id="Operand1" Text="15" runat="server"/><br>
    操作数 2:<br><asp:TextBox id="Operand2" Text="5" runat="server"/><p>    <input type="submit" id="加" value="加" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="减" value="减" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="乘" value="乘" OnServerClick="Submit_Click" runat="server">
    <input type="submit" id="除" value="除" OnServerClick="Submit_Click" runat="server">    <p>    <asp:Label id="Result" runat="server"/>  </div>  </form></body>
</html>

解决方案 »

  1.   

    把控件一个个加上。把code拷过去
      

  2.   

    怎么改?没人理我啊
    .aspx<%@ Import Namespace="MathService" %>
    <html>
    <script language="C#" runat="server">
      float operand1 = 0;
      float operand2 = 0;  public void Submit_Click(Object sender, EventArgs E)
      {
          try
          {
            operand1 = float.Parse(Operand1.Text);
            operand2 = float.Parse(Operand2.Text);
          }
          catch (Exception) { /* ignored */ }      MathService service = new MathService();
            switch (((Control)sender).ID)
            {
              case "加"      : Result.Text = "<b>结果</b> = " + service.Add(operand1, operand2).ToString(); break;
              case "减" : Result.Text = "<b>结果</b> = " + service.Subtract(operand1, operand2).ToString(); break;
              case "乘" : Result.Text = "<b>结果</b> = " + service.Multiply(operand1, operand2).ToString(); break;
              case "除"   : Result.Text = "<b>结果</b> = " + service.Divide(operand1, operand2).ToString(); break;
            }
      }
    </script><body style="font: 10.5pt 宋体">  <h4>使用简单的数学服务 </h4>  <form runat="server">  <div style="padding:15,15,15,15;background-color:beige;width:300;border-color:black;border-width:1;border-style:solid">    操作数 1:<br><asp:TextBox id="Operand1" Text="15" runat="server"/><br>
        操作数 2:<br><asp:TextBox id="Operand2" Text="5" runat="server"/><p>    <input type="submit" id="加" value="加" OnServerClick="Submit_Click" runat="server">
        <input type="submit" id="减" value="减" OnServerClick="Submit_Click" runat="server">
        <input type="submit" id="乘" value="乘" OnServerClick="Submit_Click" runat="server">
        <input type="submit" id="除" value="除" OnServerClick="Submit_Click" runat="server">    <p>    <asp:Label id="Result" runat="server"/>  </div>  </form></body>
    </html>
      

  3.   

    小弟写了一个,编译通过了,但是为什么运行时,点击相应的按钮后,不显示结果呢,把代码贴出来,帮我改改吧
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace WindowsApplication2
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.TextBox textBox2;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.Button button2;
    private System.Windows.Forms.Button button3;
    private System.Windows.Forms.Button button4;
    private System.Windows.Forms.Label label1; private  MathService1 service= new MathService1();
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.textBox2 = new System.Windows.Forms.TextBox();
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.button3 = new System.Windows.Forms.Button();
    this.button4 = new System.Windows.Forms.Button();
    this.label1 = new System.Windows.Forms.Label();
    this.SuspendLayout();
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(48, 32);
    this.textBox1.Name = "textBox1";
    this.textBox1.Size = new System.Drawing.Size(136, 21);
    this.textBox1.TabIndex = 0;
    this.textBox1.Text = "Operand1";
    // 
    // textBox2
    // 
    this.textBox2.Location = new System.Drawing.Point(48, 88);
    this.textBox2.Name = "textBox2";
    this.textBox2.Size = new System.Drawing.Size(136, 21);
    this.textBox2.TabIndex = 1;
    this.textBox2.Text = "Operand2";
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(48, 136);
    this.button1.Name = "button1";
    this.button1.Size = new System.Drawing.Size(72, 24);
    this.button1.TabIndex = 2;
    this.button1.Text = "Plus";
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(144, 136);
    this.button2.Name = "button2";
    this.button2.Size = new System.Drawing.Size(72, 24);
    this.button2.TabIndex = 3;
    this.button2.Text = "Subtract";
    // 
    // button3
    // 
    this.button3.Location = new System.Drawing.Point(48, 168);
    this.button3.Name = "button3";
    this.button3.Size = new System.Drawing.Size(72, 24);
    this.button3.TabIndex = 4;
    this.button3.Text = "Mutilply";
    // 
    // button4
    // 
    this.button4.Location = new System.Drawing.Point(144, 168);
    this.button4.Name = "button4";
    this.button4.Size = new System.Drawing.Size(72, 24);
    this.button4.TabIndex = 5;
    this.button4.Text = "Divide";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(48, 208);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(168, 32);
    this.label1.TabIndex = 6;
    this.label1.Text = "Result";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(292, 266);
    this.Controls.Add(this.label1);
    this.Controls.Add(this.button4);
    this.Controls.Add(this.button3);
    this.Controls.Add(this.button2);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.textBox2);
    this.Controls.Add(this.textBox1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    public void Submit_Click(object sender, EventArgs E)
    {
    float operand1 = 0;
    float operand2 = 0;
    try
    {
    operand1 = float.Parse(textBox1.Text);
    operand2 = float.Parse(textBox2.Text);

    switch (((Control)sender).Text)
    {
    case "button1.Text" : label1.Text = "Result  " +     service.Add(operand1, operand2).ToString(); 
    break;
    case "subtract" : label1.Text = "Result  "  + service.Subtract(operand1, operand2).ToString(); 
    break;
    case "mutiply" : label1.Text = "Result  "  + service.Multiply(operand1, operand2).ToString(); 
    break;
    case "divide" : label1.Text = "Result  " + service.Divide(operand1, operand2).ToString(); 
    break;
    }
    }
    catch (Exception) { /* ignored */ }
    }
    }
    }