wcfhttpform/wcfHttpForm/mainForm.cs

78 lines
1.9 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
namespace WCF_FORM
{
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
checkHost();
}
ServiceHost host;
private void button1_Click(object sender, EventArgs ex)
{
this.openHost();
}
private void openHost() {
if (host == null)
{
host = new ServiceHost(typeof(webHttp.Service));
host.Opened += delegate { MessageBox.Show("启动服务OK"); };
host.Closed += delegate { MessageBox.Show("服务关闭"); };
foreach (ServiceEndpoint EP in host.Description.Endpoints)
{
EP.Behaviors.Add(new BehaviorAttribute());
}
try
{
host.Open();
}
catch (Exception e)
{
Console.WriteLine("youwenti");
MessageBox.Show(e.Message);
}
}
else
{
MessageBox.Show("服务已经启动了");
}
checkHost();
}
private void button2_Click(object sender, EventArgs e)
{
if (host != null)
{
host.Close();
host = null;
}
checkHost();
}
private void mainForm_Load(object sender, EventArgs e)
{
this.openHost();
}
private void checkHost()
{
this.label1.Text = host != null? "服务已开启":"服务未开启";
}
}
}