我正在尝试使用wcf命名服务器 - 客户端消息传递应用程序。
到目前为止,这是服务器部分。
namespace server
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e) /// once the form loads, create and open a new ServiceEndpoint.
{
ServiceHost duplex = new ServiceHost(typeof(ServerWCallbackImpl));
duplex.AddServiceEndpoint(typeof(IServerWithCallback), new NetTcpBinding(), "net.tcp://localhost:9080/DataService");
duplex.Open();
this.Text = "SERVER *on-line*";
}
private void buttonSendMsg_Click(object sender, EventArgs e)
{
Message2Client(textBox2.Text); /// The name 'Message_Server2Client' does not exist in the current context :(
}
class ServerWCallbackImpl : IServerWithCallback /// NEED TO SOMEHOW MERGE THIS ONE WITH THE FORM1 CLASS
{
IDataOutputCallback callback = OperationContext.Current.GetCallbackChannel<IDataOutputCallback>();
public void StartConnection(string name)
{
/// client has connected
}
public void Message_Cleint2Server(string msg)
{
TextBox1.text += msg; /// 'TextBox1' does not exist in the current context :(
}
public void Message2Client(string msg)
{
callback.Message_Server2Client(msg);
}
}
[ServiceContract(Namespace = "rf.services",
CallbackContract = typeof(IDataOutputCallback),
SessionMode = SessionMode.Required)]
public interface IServerWithCallback ///// what comes from the client to the server.
{
[OperationContract(IsOneWay = true)]
void StartConnection(string clientName);
[OperationContract(IsOneWay = true)]
void Message_Cleint2Server(string msg);
}
public interface IDataOutputCallback ///// what goes from the sertver, to the client.
{
[OperationContract(IsOneWay = true)]
void AcceptConnection();
[OperationContract(IsOneWay = true)]
void Message_Server2Client(string msg);
}
}
}
我只是弄清楚,如何合并“ class Form1:Form ”和“ class ServerWCallbackImpl:IServerWithCallback ”,这样我就可以从按钮点击中引出 Message2Client 函数,并在* Message_Cleint2Server *发生时添加 TextBox1.text + = msg 。
谢谢!
答案 0 :(得分:1)
答案 1 :(得分:1)