Rafy 领域实体框架演示(3) - 快速使用 C/S 架构部署
2013-11-13 15:35
465 查看
本系列演示如何使用Rafy领域实体框架快速转换一个传统的三层应用程序,并展示转换完成后,Rafy带来的新功能。
《
《
《
以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进行部署。
代码下载
下载地址:本文的代码在“3.使用CS部署程序”文件夹中。
欢迎试用Rafy领域实体框架,框架发布地址:
相关文章推荐
- Rafy 领域实体框架演示(4) - 使用本地文件型数据库 SQLCE 绿色部署
- Rafy 领域实体框架演示(2) - 新功能展示
- 示例:使用 Rafy 领域实体框架转换传统三层应用程序
- 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序
- Rafy 领域实体框架简介
- 应用程序框架实战十四:DDD分层架构之领域实体(基础篇)
- 【转载】实体框架之领域驱动实践:分层架构
- 使用ansible快速部署一个主流的Web架构
- 应用程序框架(一):DDD分层架构:领域实体(基础篇)
- [翻译]ADO.NET Entity Framework Beta2(六)/快速入门(实体框架)(1)/创建学校演示数据库
- Rafy 领域实体框架设计 - 重构 ORM 中的 Sql 生成
- 福利到!Rafy(原OEA)领域实体框架 2.22.2067 发布!
- 应用程序框架实战十五:DDD分层架构之领域实体(验证篇)
- Rafy 领域实体框架示例(1) - 转换传统三层应用程序
- 译文:使用MVC5的Entity Framework 6入门(五)——MVC程序中实体框架的Code First迁移和部署
- Rafy 领域实体框架 - 领域模型设计器(建模工具)设计方案
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署
- Rafy 领域实体框架 - 公司内部培训视频
- [渣译文] 使用 MVC 5 的 EF6 Code First 入门 系列:MVC程序中实体框架的Code First迁移和部署 - Bce
- 在快速自定义的NopCommerce中使用实体框架(EF)代码优先迁移