wcf客户端和服务器端的简单通信

本文介绍了一个使用WSHttpBinding的WCF服务示例,包括服务器端与客户端的配置文件设置,以及通过C#实现的服务启动与调用代码。重点展示了如何通过配置文件灵活调整服务参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天写了一个简单的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];
            }
        }


 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值