WCF全面解析之 第四讲 使用代码 部署服务

关键代码:

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();

}

猜你喜欢

转载自www.cnblogs.com/ltbhltbh/p/12327257.html