您的位置:首页 > 运维架构 > 网站架构

Rafy 领域实体框架演示(3) - 快速使用 C/S 架构部署

2013-11-13 15:35 465 查看




本系列演示如何使用Rafy领域实体框架快速转换一个传统的三层应用程序,并展示转换完成后,Rafy带来的新功能。

福利到!Rafy(原OEA)领域实体框架2.22.2067发布!》

Rafy领域实体框架示例(1)-转换传统三层应用程序》

Rafy领域实体框架演示(2)-新功能展示》

以Rafy开发的应用程序,其实体、仓库、服务代码不需要做任何修改,即可同时支持单机部署、C/S分布式部署。本文将说明如果快速使用C/S分布式部署。

前言

截止到上一篇,我们开发的应用程序都是采用直接连接数据库的模式:





接下来,将通过一些简单的调整,使得这个应用程序支持以C/S架构部署。整个过程只需要少量的代码:





包含以下步骤:

添加服务端控制台应用程序项目

修改客户端应用程序连接方式

配置客户端应用程序

运行示例

代码下载

添加服务端控制台应用程序项目

在整个解决方案中添加一个新的控制台应用程序,取名为ServerConsole:





为项目添加所有Rafy程序集、CS实体程序集以及System.ServiceModel程序集的引用:





在Main函数中添加以下代码,启动服务端领域项目,并开始监听WCF端口:

usingSystem;

[code]usingSystem.Collections.Generic;
usingSystem.Linq;

usingSystem.ServiceModel;

usingSystem.Text;

usingCS;

usingRafy;

usingRafy.Domain;


namespaceServerConsole

{

classProgram

{

staticvoidMain(string[]args)

{

PluginTable.DomainLibraries.AddPlugin<CSPlugin>();

newDomainApp().Startup();


using(ServiceHostserviceHost=newServiceHost(typeof(Rafy.DataPortal.WCF.ServerPortal)))

{

serviceHost.Open();

Console.WriteLine("Press<enter>toterminateservice");

Console.ReadLine();

serviceHost.Close();

}

}

}

}

[/code]

然后,为本项目添加应用程序配置文件App.config,代码如下:


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

[code]<configuration>
<appSettings>

<addkey="SQL_TRACE_FILE"value="D:\SQLTraceLog.txt"/>

</appSettings>

<connectionStrings>

<addname="CS"connectionString="server=.\SQLExpress;database=ClothesSys;uid=sa;pwd=GIX4"providerName="System.Data.SqlClient"/>

</connectionStrings>

<system.serviceModel>

<services>

<servicename="Rafy.DataPortal.WCF.ServerPortal"behaviorConfiguration="includesException">

<endpointaddress="/Text"binding="basicHttpBinding"contract="Rafy.DataPortal.WCF.IWcfPortal"/>

<host>

<baseAddresses>

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

</baseAddresses>

</host>

</service>

</services>

<behaviors>

<serviceBehaviors>

<behaviorname="includesException">

<serviceMetadatahttpGetEnabled="true"/>

<serviceDebugincludeExceptionDetailInFaults="true"/>

</behavior>

</serviceBehaviors>

</behaviors>

</system.serviceModel>

</configuration>

[/code]

修改客户端应用程序连接方式

接下来需要把界面程序变更为连接服务端。打开ClothesSys项目中的Program.cs文件,修改为以下代码:


staticclassProgram

[code]{
///<summary>

///应用程序的主入口点。

///</summary>

[STAThread]

staticvoidMain()

{

PluginTable.DomainLibraries.AddPlugin<CSPlugin>();

newClientDomainApp().Startup();


Application.EnableVisualStyles();

Application.SetCompatibleTextRenderingDefault(false);

Application.Run(newformLogin());

}

}


///<summary>

///客户端使用的应用程序类型。

///</summary>

publicclassClientDomainApp:DomainApp

{

protectedoverridevoidInitEnvironment()

{

RafyEnvironment.Location.DataPortalMode=DataPortalMode.ThroughService;


base.InitEnvironment();

}

}

[/code]

RafyEnvironment.Location.DataPortalMode表示连接数据的模式,默认值是DataPortalMode.ConnectDirectly(直接连接数据库),ClientDomainApp类把该值变更为DataPortalMode.ThroughService(通过服务连接数据)。

配置客户端应用程序

在客户端配置文件中,删除数据库连接配置,并添加WCF连接配置,如下:


<?xmlversion="1.0"?>

[code]<configuration>
<configSections>

<sectionname="rafy"type="Rafy.Configuration.RafyConfigurationSection,Rafy"/>

</configSections>

<rafy

dataPortalProxy="Rafy.DataPortal.WCF.ClientProxy,Rafy.Domain"

collectDevLanguages="IsDebugging">

</rafy>

<system.serviceModel>

<client>

<endpointname="ClientProxyEndPoint"address="http://localhost:8000/RafyServer/Text"

binding="basicHttpBinding"bindingConfiguration="basicHttpBindingConfig"

contract="Rafy.DataPortal.WCF.IWcfPortal"/>

</client>

<bindings>

<basicHttpBinding>

<bindingname="basicHttpBindingConfig"receiveTimeout="00:20:00"sendTimeout="02:00:00"maxReceivedMessageSize="1000000000">

<readerQuotasmaxDepth="64"maxStringContentLength="2147483647"maxArrayLength="2147483647"maxBytesPerRead="4096"maxNameTableCharCount="16384"/>

</binding>

</basicHttpBinding>

</bindings>

</system.serviceModel>

</configuration>

[/code]

运行程序

先运行ServerConsole,成功运行界面:





再运行ClothesSys,点击登录。登录成功,即说明已经成功使用C/S进行部署。







代码下载

下载地址:http://pan.baidu.com/s/1AB9TL





本文的代码在“3.使用CS部署程序”文件夹中。

欢迎试用Rafy领域实体框架,框架发布地址:/article/4875385.html。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐