C#
static void Main(string[] args){ //创建宿主的基地址 Uri baseAddress = new Uri("http://localhost:8080/User"); //创建宿主 using (ServiceHost host = new ServiceHost(typeof(User), baseAddress)) { host.AddServiceEndpoint(typeof(IUser), new WSHttpBinding(), ""); //将HttpGetEnabled属性设置为true ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; //将行为添加到Behaviors中 host.Description.Behaviors.Add(smb); //打开宿主 host.Open(); Console.WriteLine("WCF中的HTTP监听已启动...."); Console.ReadLine(); host.Close(); }}
App.config
public partial class MainForm : Form{ ServiceHost host; public MainForm() { InitializeComponent(); } private void MainForm_Load(object sender, EventArgs e) { host = new ServiceHost(typeof(User)); //打开宿主 host.Open(); this.label1.Text = "WCF中的HTTP监听已启动...."; } private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { host.Close(); }}