需要将两个类合并在一起
本文关键字:两个 合并 在一起 | 更新日期: 2023-09-27 17:49:38
我正在尝试使用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);
}
}
}
我只是无法弄清楚,我如何合并"类Form1:Form"answers"类ServerWCallbackImpl: IServerWithCallback",这样我就能够从一个按钮点击诱导Message2Client函数,以及添加TextBox1。text += msg当*Message_Cleint2Server*发生时
谢谢!
你不需要合并你的类和表单:你需要创建它的一个实例。像这样:
IServerWCallback server = new ServerWCallbackImpl();
server.Message2Client("hello world");
然而(从目前的代码结构来看),您可能需要在之前创建类的实例。这允许您连接该实例并保留它以供以后的操作。
你可能还想阅读MSDN关于类和对象(类的实例)的页面,以确保在继续之前你已经完全理解了这些概念——这些东西对于。net编程是非常基础的。
合并的必要性是什么?为什么不能使用继承?
在c# .net中解决多重继承问题点击查看详细信息(stackoverflow)
我想这会给你答案