1. 程式人生 > >無廢話WCF入門教程三[WCF的宿主]

無廢話WCF入門教程三[WCF的宿主]

1 <?xml version="1.0" encoding="utf-8" ?> 2 <configuration> 3 <system.serviceModel> 4 <services> 5 <service name="WCFLibrary.User"> 6 <host> 7 <baseAddresses> 8 <add baseAddress="http://localhost:8081/User
"/> 9 </baseAddresses> 10 </host> 11 <endpoint address="" binding="wsHttpBinding" contract="WCFLibrary.IUser"></endpoint> 12 </service> 13 </services> 14 <behaviors> 15 <serviceBehaviors> 16 <behavior> 17
<serviceMetadata httpGetEnabled="True"/> 18 <serviceDebug includeExceptionDetailInFaults="False"/> 19 </behavior> 20 </serviceBehaviors> 21 </behaviors> 22 </system.serviceModel> 23 </configuration> 24 25 26 using System;
27 using WCFLibrary; 28 using System.ServiceModel; 29 using System.Windows.Forms; 30 using System.Configuration; 31 32 namespace WCFHost_Form 33 { 34 public partial class MainForm : Form 35 { 36 ServiceHost host; 37 38 public MainForm() 39 { 40 InitializeComponent(); 41 } 42 43 //應用程式載入 44 private void MainForm_Load(object sender, EventArgs e) 45 { 46 host = new ServiceHost(typeof(User)); 47 //開啟宿主 48 host.Open(); 49 this.lblState.Text = "WCF中的HTTP監聽已啟動...."; 50 } 51 52 //應用程式關閉 53 private void MainForm_FormClosed(object sender, FormClosedEventArgs e) 54 { 55 host.Close(); 56 } 57 } 58 }