使用 Web 服务时出错 ASP.NET
Error consuming Web Service ASP.NET
所以,我正在 ASP.NET 学习 Web 服务,我有一个问题。我有两种方法:Add(计算两个数字的总和)和 GetCalculations(通过 EnabledSession 属性 显示最新计算)。当我在浏览器上打开我的 .asmx 时,它会在 xml 文件中显示总和和最新计算,但是,当我在浏览器上打开我的 Web 窗体时,我可以计算总和,但不会显示计算。
这是我的代码:
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CalculatorWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
CalculatorService.CalculatorWebServiceSoap client =
new CalculatorService.CalculatorWebServiceSoapClient();
int result = client.Add(Convert.ToInt32(txtFirstNumber.Text),
Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
gvCalculations.DataSource = client.GetCalculations();
gvCalculations.DataBind();
gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalculatorWebApplication.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table style="font-family:Arial">
<tr>
<td>
<b>First Number</b>
</td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Second Number</b>
</td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Result</b>
</td>
<td>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gvCalculations" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
CalculatorWebService.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace WebServicesDemo
{
/// <summary>
/// Summary description for CalculatorWebService
/// </summary>
[WebService(Namespace = "http://pragimtech.com/webservices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class CalculatorWebService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public int Add(int firstNumber, int secondNumber)
{
List<string> calculations;
if (Session["CALCULATIONS"] == null)
{
calculations = new List<string>();
}
else
{
calculations = (List<string>)Session["CALCULATIONS"];
}
string strRecentCalculation = firstNumber.ToString() + " + "
+ secondNumber.ToString() + " = "
+ (firstNumber + secondNumber).ToString();
calculations.Add(strRecentCalculation);
Session["CALCULATIONS"] = calculations;
return firstNumber + secondNumber;
}
[WebMethod(EnableSession = true)]
public List<string> GetCalculations()
{
if (Session["CALCULATIONS"] == null)
{
List<string> calculations = new List<string>();
calculations.Add("You have not performed any calculations");
return calculations;
}
else
{
return (List<string>)Session["CALCULATIONS"];
}
}
}
}
在服务器端:使用 Web 服务上的 EnabledSession 属性,Web 服务将主会话状态并将先前的计算保持在同一客户端的会话状态中。 Web 服务如何知道它是同一个客户端?饼干.
当您使用浏览器时,cookie 将被使用并存储在您的浏览器中。这允许 Web 服务识别客户端。结果:您会在响应 xml.
中看到之前的计算
当您从网络表单调用它时,您需要额外的步骤以便使用 cookie。
对于 asmx web 服务客户端(如果你添加 WebReference),它将是这样的:
client.CookieContainer = new CookieContainer
对于 wcf 服务客户端(如果您添加 ServiceReference),您可以从配置中启用 cookie:
<system.ServiceModel>
<bindings>
<basicHttpBinding allowCookies="true">
</bindings>
......
</system.ServiceModel>
所以,我正在 ASP.NET 学习 Web 服务,我有一个问题。我有两种方法:Add(计算两个数字的总和)和 GetCalculations(通过 EnabledSession 属性 显示最新计算)。当我在浏览器上打开我的 .asmx 时,它会在 xml 文件中显示总和和最新计算,但是,当我在浏览器上打开我的 Web 窗体时,我可以计算总和,但不会显示计算。
这是我的代码:
WebForm1.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CalculatorWebApplication
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
CalculatorService.CalculatorWebServiceSoap client =
new CalculatorService.CalculatorWebServiceSoapClient();
int result = client.Add(Convert.ToInt32(txtFirstNumber.Text),
Convert.ToInt32(txtSecondNumber.Text));
lblResult.Text = result.ToString();
gvCalculations.DataSource = client.GetCalculations();
gvCalculations.DataBind();
gvCalculations.HeaderRow.Cells[0].Text = "Recent Calculations";
}
}
}
WebForm1.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="CalculatorWebApplication.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table style="font-family:Arial">
<tr>
<td>
<b>First Number</b>
</td>
<td>
<asp:TextBox ID="txtFirstNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Second Number</b>
</td>
<td>
<asp:TextBox ID="txtSecondNumber" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<b>Result</b>
</td>
<td>
<asp:Label ID="lblResult" runat="server"></asp:Label>
</td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
</td>
</tr>
<tr>
<td colspan="2">
<asp:GridView ID="gvCalculations" runat="server">
</asp:GridView>
</td>
</tr>
</table>
</form>
</body>
</html>
CalculatorWebService.asmx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
namespace WebServicesDemo
{
/// <summary>
/// Summary description for CalculatorWebService
/// </summary>
[WebService(Namespace = "http://pragimtech.com/webservices")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class CalculatorWebService : System.Web.Services.WebService
{
[WebMethod(EnableSession = true)]
public int Add(int firstNumber, int secondNumber)
{
List<string> calculations;
if (Session["CALCULATIONS"] == null)
{
calculations = new List<string>();
}
else
{
calculations = (List<string>)Session["CALCULATIONS"];
}
string strRecentCalculation = firstNumber.ToString() + " + "
+ secondNumber.ToString() + " = "
+ (firstNumber + secondNumber).ToString();
calculations.Add(strRecentCalculation);
Session["CALCULATIONS"] = calculations;
return firstNumber + secondNumber;
}
[WebMethod(EnableSession = true)]
public List<string> GetCalculations()
{
if (Session["CALCULATIONS"] == null)
{
List<string> calculations = new List<string>();
calculations.Add("You have not performed any calculations");
return calculations;
}
else
{
return (List<string>)Session["CALCULATIONS"];
}
}
}
}
在服务器端:使用 Web 服务上的 EnabledSession 属性,Web 服务将主会话状态并将先前的计算保持在同一客户端的会话状态中。 Web 服务如何知道它是同一个客户端?饼干.
当您使用浏览器时,cookie 将被使用并存储在您的浏览器中。这允许 Web 服务识别客户端。结果:您会在响应 xml.
中看到之前的计算当您从网络表单调用它时,您需要额外的步骤以便使用 cookie。 对于 asmx web 服务客户端(如果你添加 WebReference),它将是这样的:
client.CookieContainer = new CookieContainer
对于 wcf 服务客户端(如果您添加 ServiceReference),您可以从配置中启用 cookie:
<system.ServiceModel>
<bindings>
<basicHttpBinding allowCookies="true">
</bindings>
......
</system.ServiceModel>