今天写了一个简单的WCF客户端和服务器端的DEMO.较之前的一篇文章,服务器端的启动代码发生了一些改变,供大家参考。
大家可以参考一下,之前那篇文章,只是启动方式发生了改变。
最后,要告诉大家一点,对于WCF的所有配置都要写在配置文件中,这样设计更灵活.
服务器端的配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<!---->
<services>
<service name="WCFServicImp.CityImp" >
<endpoint address="http://localhost:8003/WCFServicImp.CityImp" contract="WCFService.ICity" bindingConfiguration="testbind" binding="wsHttpBinding"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="LMSbehavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceThrottling maxConcurrentCalls="200" maxConcurrentInstances="200" maxConcurrentSessions="200"/>
<dataContractSerializer maxItemsInObjectGraph="2147483647"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="testbind" openTimeout="00:10:00" receiveTimeout="00:10:00" closeTimeout="10:10:00"
sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="64" maxStringContentLength="2147483647"
maxArrayLength="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
服务器端启动代码
private void button1_Click(object sender, EventArgs e)
{
//Type svcType = Type.GetType("WCFServicImp.CityImp,WCFServicImp");
ServiceHost host = new ServiceHost(typeof(CityImp));
host.Open();
}
客户端配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8003/WCFServicImp.CityImp" contract="WCFService.ICity" bindingConfiguration="testbind" binding="wsHttpBinding" name="CityImp"></endpoint>
</client>
<behaviors>
<serviceBehaviors>
<behavior name="testbeh"></behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<wsHttpBinding>
<binding name="testbind" openTimeout="00:10:00" receiveTimeout="00:10:00" closeTimeout="10:10:00"
sendTimeout="00:10:00" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="64" maxStringContentLength="2147483647"
maxArrayLength="2147483647" />
</binding>
</wsHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
客户端调用代码
private void button1_Click(object sender, EventArgs e)
{
using (ChannelFactory<WCFService.ICity> channelFactory = new ChannelFactory<WCFService.ICity>("CityImp"))
{
WCFService.ICity proxy = channelFactory.CreateChannel();
this.dataGridView1.DataSource = proxy.GetAllCity().Tables[0];
}
}