当前位置:编程学习 > C#/ASP.NET >>

站点调用服务做一个计算器——那位大侠能能解决这个难题

服务段代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //如果使用设计的组件,请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public double Add(double x, double y)
    {
        return x + y;
    }

    [WebMethod]
    public double Min(double x, double y)
    {
        return x - y;
    }

    [WebMethod]
    public double Sub(double x, double y)
    {
        return x * y;
    }

    [WebMethod]
    public double Div(double x, double y)
    {
        if (y != 0)
        {
            return x / y;
        }
        else
        {            
            return 0;
        }
    }

}

服务运行正常,是正确的。


站点代码:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    protected void Button_1_Click(object sender, EventArgs e)           //按钮“1”
    {
        TextBox1.Text += 1;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_2_Click(object sender, EventArgs e)           //按钮“2”
    {
        TextBox1.Text += 2;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_3_Click(object sender, EventArgs e)           //按钮“3”
    {
        TextBox1.Text += 3;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_4_Click(object sender, EventArgs e)           //按钮“4”
    {
        TextBox1.Text += 4;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_5_Click(object sender, EventArgs e)           //按钮“5”
    {
        TextBox1.Text += 5;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_6_Click(object sender, EventArgs e)           //按钮“6”
    {
        TextBox1.Text += 6;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_7_Click(object sender, EventArgs e)           //按钮“7”
    {
        TextBox1.Text += 7;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_8_Click(object sender, EventArgs e)           //按钮“8”
    {
        TextBox1.Text += 8;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_9_Click(object sender, EventArgs e)           //按钮“9”
    {
        TextBox1.Text += 9;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_0_Click(object sender, EventArgs e)           //按钮“0”
    {
        TextBox1.Text += 0;
        Session["1"] = Convert.ToDouble(TextBox1.Text);
    }

    protected void Button_Re_Click(object sender, EventArgs e)          //按钮“复位”
    {
        TextBox1.Text = null;
    }

    protected void Button_Add_Click(object sender, EventArgs e)         //按钮“+”
    {
        Session["2"] = Session["1"];
        Session["3"] = "+";
        Session["1"] = null;
        TextBox1.Text = null;
    }

    protected void Button_Min_Click(object sender, EventArgs e)         //按钮“-”
    {
        Session["2"] = Session["1"];
        Session["3"] = "-";
        Session["1"] = null;
        TextBox1.Text = null;
    }

    protected void Button_Sub_Click(object sender, EventArgs e)         //按钮“*”
    {
        Session["2"] = Session["1"];
        Session["3"] = "*";
        Session["1"] = null;
        TextBox1.Text = null;
    }

    protected void Button_Div_Click(object sender, EventArgs e)         //按钮“/”
    {
        Session["2"] = Session["1"];
        Session["3"] = "/";
        Session["1"] = null;
        TextBox1.Text = null;
    }

    protected void Button_OK_Click(object sender, EventArgs e)          //按钮“确定”
    {
        string c =Session["3"].ToString();
        localhost.Service svr = new localhost.Service();
        double  Num_A = Convert.ToDouble(Session["2"]);
        double  Num_B = Convert.ToDouble(Session["1"]);
        switch (c)
        {
            case "+":
                TextBox1.Text = svr.Add(Num_A, Num_B).ToString();
                break;
            case "-":
                TextBox1.Text = svr.Min(Num_A, Num_B).ToString();
                break;
            case "*":
                TextBox1.Text = svr.Sub(Num_A, Num_B).ToString();
                break;
            case "/":
                TextBox1.Text = svr.Div(Num_A, Num_B).ToString();
                break;
        }
    }
}


引用了服务,启动调试时,没有错误、提醒,但是进入操作界面时,比如说2+3,点击“确定”时,系统缓冲一下,
出现错误,如下:
“/WebSite2”应用程序中的服务器错误。
--------------------------------------------------------------------------------

由于目标机器积极拒绝,无法连接。 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.Net.Sockets.SocketException: 由于目标机器积极拒绝,无法连接。

源错误: 


行 102:        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Add", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
行 103:        public double Add(double x, double y) {
行 104:            object[] results = this.Invoke("Add", new object[] {
行 105:                        x,
行 106:                        y});
 

--------------------编程问答-------------------- 把该关的软件关掉。。。 --------------------编程问答-------------------- 只剩下VS2005中的这个站点和服务了,我觉得再没啥可关的了!
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,