构建WCF单一到多服务器之路

构建WCF单一到多服务器之路

第一部wcf一个服务

一、新建一个项目,名称:WcfServerice,如图(1)

 (图1

二、初始界面,如图(2),

将原来IService1接口类重新改成名称IServiceFirst,将Service1 改成ServiceFirst,这是第一个服务。

重新修改其内容

  // 注意: 使用重构菜单上的重命名命令,可以同时更改代码和配置文件中的接口名“IService1”

    [ServiceContract]

    public interface IServiceFirst

    {

        // TODO: 在此添加您的服务操作

        [OperationContract]

        /* 获得表名称   */

        string GetTableName();

 

        [OperationContract]

        /* 获得表中数据   */

        DataSet GetTableData(string tablename);

    }

       

    // 使用下面示例中说明的数据约定将复合类型添加到服务操作。

    [DataContract]

    public class Product

    {

        string _TablenName ;

        /* 数据库中表的名称   */

        [DataMember]

        public string TableName

        {

            get { return _TablenName ; }

            set { _TablenName  = value; }

        }

 

        string _Connstring;

        /* 数据库连接字符串   */

        [DataMember]

        public string Connstring

        {

            get { return _Connstring ; }

            set { _Connstring  = value; }

        }

    }

如图:

三、 添加一个服务器管理项目,如图:

项目名称:WcfServiceManager

在此项目中,添加两个引用,一个是system.configuration,其作用下面程序要用到,另一个就是system.serverModal. 这个必须的,不能少。

如图:

 

并添加如下引用 :

四、 还要添加对项目的引用,如图:

 

五、 WcfServiceManager,添加app.config文件

 

修改配置如下:

 

      <?xmlversion="1.0"encoding="utf-8" ?>

<configuration>

  <system.serviceModel>

    <services>

      <!--添加服务-->

      <servicename=" WcfServerice.ServiceFirst"behaviorConfiguration="CalculatorServiceBehavior">

        <!--name 必须与代码中的host实例初始化的服务一样 

            behaviorConfiguration 行为配置-->

        <host>

          <baseAddresses>

            <!--添加调用服务地址-->

            <addbaseAddress="http://localhost:8000/"/>

          </baseAddresses>

 

        </host>

        <!--添加契约接口   contract="WcfDemo.IService1" WcfDemo.IService1为契约接口   binding="wsHttpBinding" wsHttpBinding为通过Http调用-->

        <endpointaddress=""    binding="wsHttpBinding"contract=" WcfServerice.

.IServiceFirst"></endpoint>

      </service>

 

 

    </services>

 

    <!--定义CalculatorServiceBehavior的行为-->

    <behaviors>

      <serviceBehaviors>

        <behaviorname="CalculatorServiceBehavior">

          <serviceMetadatahttpGetEnabled="true"/>

          <serviceDebugincludeExceptionDetailInFaults="false"/>

 

        </behavior>

 

      </serviceBehaviors>

 

    </behaviors>

  </system.serviceModel>

</configuration>

 

六、 项目的主要文件:

 

七、添加客户端过程

在此也要添加引用:

最重要的要添加引用服务过程:

另外,还要修改客户端的app.config的文件配置

<?xmlversion="1.0"encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <bindingname="BasicHttpBinding_IServiceFirst"maxReceivedMessageSize="655300" />  

            </basicHttpBinding>

        </bindings>

        <client>

            <endpointaddress="http://localhost:5131/ServiceFirst.svc"binding="basicHttpBinding"

                bindingConfiguration="BasicHttpBinding_IServiceFirst"contract="ServiceRefFirst.IServiceFirst"

                name="BasicHttpBinding_IServiceFirst" />

        </client>

    </system.serviceModel>

</configuration>

客户端的界面如图

内容如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.ServiceModel;

 

namespace WcfClient

{

    public partial class FrmMain : Form

    {

 

        ServiceRefFirst.ServiceFirstClient  myFrist = new ServiceRefFirst.ServiceFirstClient();

 

        public FrmMain()

        {

            InitializeComponent();

        }

 

        private void btnclose_Click(object sender, EventArgs e)

        {

            this.Close();

        }

 

        private void btngetdata_Click(object sender, EventArgs e)

        {

           // myFrist .c

 

            dataGridView1.DataSource = myFrist.GetTableData("").Tables[0];

        }

    }

}

 

第二部wcf多个服务

八、再第一部基本上,再增加一个服务

修改名称,如图,

这里主要是修改接口和类的名称等

 

 

八、修改服务端的配置文件app.config ,主要是添加第二个服务的配置内容,及绑定数据流的大小。

<?xmlversion="1.0"encoding="utf-8" ?>

<configuration>

  <system.serviceModel>

    <services>

      <!--添加服务-->

      <servicename="WcfServerice.ServiceFirst"behaviorConfiguration="CalculatorServiceBehavior">

        <!--name 必须与代码中的host实例初始化的服务一样 

            behaviorConfiguration 行为配置-->

        <host>

          <baseAddresses>

            <!--添加调用服务地址-->

            <addbaseAddress="http://localhost:8000//ServiceFirst.svc"/>

          </baseAddresses>

        </host>

        <!--添加契约接口   contract="WcfDemo.IService1" WcfDemo.IService1为契约接口   binding="wsHttpBinding" wsHttpBinding为通过Http调用-->

        <endpointaddress=""    binding="wsHttpBinding"contract="WcfServerice.IServiceFirst"></endpoint>

      </service>

 

      <!--添加服务-->

      <servicename="WcfServerice.ServiceSecond"behaviorConfiguration="CalculatorServiceBehavior">

        <!--name 必须与代码中的host实例初始化的服务一样 

            behaviorConfiguration 行为配置-->

        <host>

          <baseAddresses>

            <!--添加调用服务地址-->

            <addbaseAddress="http://localhost:8000//ServiceSecond.svc"/>

          </baseAddresses>

        </host>

        <!--添加契约接口   contract="WcfDemo.IService1" WcfDemo.IService1为契约接口   binding="wsHttpBinding" wsHttpBinding为通过Http调用-->

        <endpointaddress=""    binding="wsHttpBinding"contract="WcfServerice.IServiceSecond"></endpoint>

      </service>

 

    </services>

 

    <!--定义CalculatorServiceBehavior的行为-->

    <behaviors>

      <serviceBehaviors>

        <behaviorname="CalculatorServiceBehavior">

          <serviceMetadatahttpGetEnabled="true"/>

          <serviceDebugincludeExceptionDetailInFaults="false"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

 

    <!--定义binding的行为-->

    <bindings>

      <wsHttpBinding>

        <bindingname="NewBindingStationCnfg"maxReceivedMessageSize="2147483647" />

      </wsHttpBinding>

    </bindings>

 

  </system.serviceModel>

</configuration>

 

这里主要注意到    <addbaseAddress="http://localhost:8000//ServiceFirst.svc"/>

如果是单一服务是不需要增加ServiceFirst.svc,再多服务系统中必须要分服务名进行添加,方可以。

 

在客户端添加引用时,也要分别进行命名,在如图的使命名空间中。

第一个命名为ServiceFirst. 下面依次进行命名,主要是为对应服役端的相对服务。

 

 

 

九、如果修改服务端的内容,则客户先要更新才可以使用。如图更新方法,

选择客户端相对应的服务,点击“更新服务费用”即可。

 

十,注意客户端的读取数据流量。

打开客户端的APP.CONFIG文件,如图:

<?xmlversion="1.0"encoding="utf-8" ?>

<configuration>

    <system.serviceModel>

        <bindings>

            <basicHttpBinding>

                <bindingname="BasicHttpBinding_IServiceFirst"maxReceivedMessageSize ="655000" />

                <bindingname="BasicHttpBinding_IServiceSecond"maxReceivedMessageSize ="655000" />

            </basicHttpBinding>

        </bindings>

        <client>

            <endpointaddress="http://localhost:5131/ServiceSecond.svc"binding="basicHttpBinding"

                bindingConfiguration="BasicHttpBinding_IServiceSecond"contract="ServiceRefSecond.IServiceSecond"

                name="BasicHttpBinding_IServiceSecond" />

            <endpointaddress="http://localhost:5131/ServiceFirst.svc"binding="basicHttpBinding"

                bindingConfiguration="BasicHttpBinding_IServiceFirst"contract="ServiceRefFirst.IServiceFirst"

                name="BasicHttpBinding_IServiceFirst" />

        </client>

    </system.serviceModel>

</configuration>

 

再这里主要添加maxReceivedMessageSize ="655000",系统默认大小65500,所以可能无法满足我们的大小。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值