构建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,所以可能无法满足我们的大小。

相关文章

迭代器模式(Iterator)迭代器模式(Iterator)[Cursor]意图...
高性能IO模型浅析服务器端编程经常需要构造高性能的IO模型,...
策略模式(Strategy)策略模式(Strategy)[Policy]意图:定...
访问者模式(Visitor)访问者模式(Visitor)意图:表示一个...
命令模式(Command)命令模式(Command)[Action/Transactio...
生成器模式(Builder)生成器模式(Builder)意图:将一个对...