跟刚才的项目一样,调用时候稍微改变,不需要更改config就可以
一看就能看懂,跟配置config其实是一样的,只不过是动态配置,不解释了,host.addserviceendpoint里面的三个参数,1、契约。2、绑定。3、地址。
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 button2_Click(object sender, EventArgs e) { if (host.State != CommunicationState.Closed) { host.Close(); label1.Text = "服务已停止!"; } }