今天寫了一個WCF demo給大家分享說明如下
服務接口類庫WCFServicedll
服務實現類庫WCFServicImpdll
服務宿主項目WCFHost
客戶端調用項目:WCFTest
大家下載完DEMO運行的時候先運行服務器端WCFHost然後再運行客戶端WCFTest進行調用關於數據庫服務器的地址請自己配置
請大家注意下服務器端和客戶端的nfig文件關於WCF服務的所有配置都在這兩個文件中進行配置
客戶端調用服務的時候要引用接口類庫 WCFServicedll
代碼
服務器端宿主
public partial class Form : Form
{
public Form()
{
InitializeComponent();
}
static List<ServiceHost> _hosts = new List<ServiceHost>();
private void Form_Load(object sender EventArgs e)
{
//啟動服務器端
Configuration conf = ConfigurationManagerOpenExeConfiguration(AssemblyGetEntryAssembly()Location);
ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)confGetSectionGroup(systemserviceModel);
foreach (ServiceElement el in svcmodServicesServices)
{
Type svcType = TypeGetType(elName + + elNameSubstring( elNameLastIndexOf()));
if (svcType == null)
{
throw new Exception(配置文件中的服務 [ + elName + ] 無效);
}
ServiceHost hst = new ServiceHost(svcType);
_hostsAdd(hst);
hstOpen();
}
}
}
客戶端調用代碼
private void button_Click(object sender EventArgs e)
{ using (ChannelFactory<WCFServiceICity> channelFactory = new ChannelFactory<WCFServiceICity>(CityImp))
{
WCFServiceICity proxy = channelFactoryCreateChannel();
thisdataGridViewDataSource = proxyGetAllCity()Tables[];
}
}
From:http://tw.wingwit.com/Article/program/net/201311/13922.html