关键代码:
ServiceHost host = null;
//启动
private void button1_Click(object sender, EventArgs e)
{
host=new ServiceHost(typeof(winServer2.MyClass));
NetTcpBinding tcpBind=new NetTcpBinding();
string address = "net.tcp://localhost:3200/hello";
host.AddServiceEndpoint(typeof(ClassLibrary1.myInterface),tcpBind,address);
host.Opened += delegate { label1.Text = "服务已启动!"; };
host.Open();
}
//停止
private void btnClose_Click(object sender, EventArgs e)
{
if (host.State != CommunicationState.Closed)
host.Close();
}
原文:https://www.cnblogs.com/ltbhltbh/p/12327257.html