大家好
我下载了微软的一个WCF示例,它用IIS承载WCF服务,采用双工回调技术,示例中客户端采用控制台应用程序.
我把客户端改成WebForm应用程序后,回调结果显示不出来,但是在调试的时候有时可以得到和显示正确结果.
下面我把Web客户端的前后台代码贴出来,另外提供整个源码下载.
http://www.zhstrong.com/uploadfiles/cs.rar
- HTML code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <div> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:TextBox ID="TextBox1" runat="server" Height="345px" TextMode="MultiLine"></asp:TextBox> <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" /> <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Button" /> <asp:Button ID="Button3" runat="server" onclick="Button3_Click" Text="Button" /> <asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Button" /> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
- C# code
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.ServiceModel; namespace WebApplication1 { public partial class _Default : System.Web.UI.Page,WebClientTest.ICalculatorDuplexCallback { //WebClientTest为引用的WCF服务命名空间 WebClientTest.CalculatorDuplexClient client; double value; protected void Page_Load(object sender, EventArgs e) { InstanceContext instanceContext = new InstanceContext(this); //每次执行Button事件,client都为空. // Create a client if (client == null) { //创建WCF服务的客户端 client = new WebClientTest.CalculatorDuplexClient(instanceContext); } } #region ICalculatorDuplexCallback 成员 /// <summary> /// 实现回调成员 /// </summary> /// <param name="result1"></param> public void Result(double result1) { this.TextBox1.Text += result1.ToString() + "\r\n"; } public void Equation(string eqn) { this.TextBox1.Text += eqn + "\r\n"; } #endregion protected void Button1_Click(object sender, EventArgs e) { // Call the AddTo service operation. //调用WCF服务 value = 100.00D; client.AddTo(value); } protected void Button2_Click(object sender, EventArgs e) { // Call the SubtractFrom service operation. value = 50.00D; client.SubtractFrom(value); } protected void Button3_Click(object sender, EventArgs e) { // Call the MultiplyBy service operation. value = 17.65D; client.MultiplyBy(value); } protected void Button4_Click(object sender, EventArgs e) { // Call the DivideBy service operation. value = 2.00D; client.DivideBy(value); } } }