WCF服务合同和文本框 [英] WCF service contract and textbox
问题描述
我已经从Windows窗体创建了tcp WCF服务.我实现的operationcontract将更新文本框的文本.但是当客户端调用该操作时,文本框不会显示该文本.如何解决呢?
这是我的服务器:
I''ve create a tcp WCF service from windows form. The operationcontract that i implemented will update the text of a textbox. but When client call that operation, the textbox doesn''t show that text. How to solve this?
This is my server:
<br />
<pre lang="cs">using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
using System.ServiceModel;<br />
using MiniChat;<br />
namespace MiniChatServer<br />
{<br />
public partial class FrmMiniChatServer : Form, IServerContract<br />
{<br />
private ServiceHost host;<br />
public FrmMiniChatServer()<br />
{<br />
InitializeComponent();<br />
}<br />
private void MiniChatServer_Load(object sender, EventArgs e)<br />
{<br />
host = new ServiceHost(typeof(FrmMiniChatServer));<br />
host.Open();<br />
}<br />
#region IServerContract Members<br />
public void ClientToServer(string msg)<br />
{<br />
textBox1.Text = "Client: " + msg;<br />
MessageBox.Show(textBox1.Text);<br />
}<br />
#endregion<br />
private void MiniChatServer_Closed(object sender, FormClosedEventArgs e)<br />
{<br />
}<br />
}<br />
}</pre><br />
<br />
和客户
And Client
<br />
<pre lang="cs"><br />
ServerServices.IServerContract server = new ServerServices.ServerContractClient();<br />
server.ClientToServer("message from client");<br />
</pre><br />
和合同接口
and contract interfaces
<br />
<pre lang="cs"><br />
using System;<br />
using System.Collections.Generic;<br />
using System.Linq;<br />
using System.ServiceModel;<br />
namespace MiniChat<br />
{<br />
public interface ICallback<br />
{<br />
[OperationContract(IsOneWay=true)]<br />
void ServerToClient(string msg);<br />
}<br />
[ServiceContract(CallbackContract=typeof(ICallback))]<br />
public interface IServerContract<br />
{<br />
[OperationContract(IsOneWay=true)]<br />
void ClientToServer(string msg);<br />
}<br />
<br />
}</pre><br />
<br />
在函数中:
In the function:
<br />
<pre lang="cs">public void ClientToServer(string msg)<br />
{<br />
textBox1.Text = "Client: " + msg;<br />
MessageBox.Show(textBox1.Text);<br />
}</pre><br />
MessageBox.Show(textBox1.Text);正确显示消息,但textBox1不显示任何内容.
MessageBox.Show(textBox1.Text); show message correctly, but textBox1 doesn''t show anything.
推荐答案
我看到您在这里做什么.尽管您的命名约定确实令人困惑.
替换
I see what you''r doing here. Although your naming convention is really confusing.
Replace
void IServerContract.ClientToServer(string msg) {
textBox1.Text = "Client: " + msg;
}
与
with
public void ClientToServer(string msg) {
textBox1.Text = "Client: " + msg;
}
然后转到您的IServerContract类,并为ClientToServer定义接口
Then go to your IServerContract Class and define the Interface for ClientToServer
public void ClientToServer(string msg);
然后更新您的客户端代码.
祝你好运!
Then update your client code.
Good luck!
这篇关于WCF服务合同和文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!