本系列演示如何使用 Rafy 領域實體框架快速轉換一個傳統的三層應用程序,並展示轉換完成后,Rafy 帶來的新功能。
《福利到!Rafy(原OEA)領域實體框架 2.22.2067 發布!》
《Rafy 領域實體框架示例(1) - 轉換傳統三層應用程序》
以 Rafy 開發的應用程序,其實體、倉庫、服務代碼不需要做任何修改,即可同時支持單機部署、C/S 分布式部署。本文將說明如果快速使用 C/S 分布式部署。
前言
截止到上一篇,我們開發的應用程序都是采用直接連接數據庫的模式:
接下來,將通過一些簡單的調整,使得這個應用程序支持以 C/S 架構部署。整個過程只需要少量的代碼:
包含以下步驟:
- 添加服務端控制台應用程序項目
- 修改客戶端應用程序連接方式
- 配置客戶端應用程序
- 運行示例
- 代碼下載
添加服務端控制台應用程序項目
在整個解決方案中添加一個新的控制台應用程序,取名為 ServerConsole:
為項目添加所有 Rafy 程序集、CS 實體程序集以及 System.ServiceModel 程序集的引用:
在 Main 函數中添加以下代碼,啟動服務端領域項目,並開始監聽 WCF 端口:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.ServiceModel;
5: using System.Text;
6: using CS;
7: using Rafy;
8: using Rafy.Domain;
9:
10: namespace ServerConsole
11: {
12: class Program
13: {
14: static void Main(string[] args)
15: {
16: PluginTable.DomainLibraries.AddPlugin<CSPlugin>();
17: new DomainApp().Startup();
18:
19: using (ServiceHost serviceHost = new ServiceHost(typeof(Rafy.DataPortal.WCF.ServerPortal)))
20: {
21: serviceHost.Open();
22: Console.WriteLine("Press <enter> to terminate service");
23: Console.ReadLine();
24: serviceHost.Close();
25: }
26: }
27: }
28: }
然后,為本項目添加應用程序配置文件 App.config,代碼如下:
1: <?xml version="1.0" encoding="utf-8" ?>
2: <configuration>
3: <appSettings>
4: <add key="SQL_TRACE_FILE" value="D:\SQLTraceLog.txt"/>
5: </appSettings>
6: <connectionStrings>
7: <add name="CS" connectionString="server=.\SQLExpress;database=ClothesSys;uid=sa;pwd=GIX4" providerName="System.Data.SqlClient"/>
8: </connectionStrings>
9: <system.serviceModel>
10: <services>
11: <service name="Rafy.DataPortal.WCF.ServerPortal" behaviorConfiguration="includesException">
12: <endpoint address="/Text" binding="basicHttpBinding" contract="Rafy.DataPortal.WCF.IWcfPortal"/>
13: <host>
14: <baseAddresses>
15: <add baseAddress="http://localhost:8000/RafyServer" />
16: </baseAddresses>
17: </host>
18: </service>
19: </services>
20: <behaviors>
21: <serviceBehaviors>
22: <behavior name="includesException">
23: <serviceMetadata httpGetEnabled="true" />
24: <serviceDebug includeExceptionDetailInFaults="true" />
25: </behavior>
26: </serviceBehaviors>
27: </behaviors>
28: </system.serviceModel>
29: </configuration>
修改客戶端應用程序連接方式
接下來需要把界面程序變更為連接服務端。打開 ClothesSys 項目中的 Program.cs 文件,修改為以下代碼:
1: static class Program
2: {
3: /// <summary>
4: /// 應用程序的主入口點。
5: /// </summary>
6: [STAThread]
7: static void Main()
8: {
9: PluginTable.DomainLibraries.AddPlugin<CSPlugin>();
10: new ClientDomainApp().Startup();
11:
12: Application.EnableVisualStyles();
13: Application.SetCompatibleTextRenderingDefault(false);
14: Application.Run(new formLogin());
15: }
16: }
17:
18: /// <summary>
19: /// 客戶端使用的應用程序類型。
20: /// </summary>
21: public class ClientDomainApp : DomainApp
22: {
23: protected override void InitEnvironment()
24: {
25: RafyEnvironment.Location.DataPortalMode = DataPortalMode.ThroughService;
26:
27: base.InitEnvironment();
28: }
29: }
RafyEnvironment.Location.DataPortalMode 表示連接數據的模式,默認值是DataPortalMode.ConnectDirectly(直接連接數據庫),ClientDomainApp 類把該值變更為 DataPortalMode. ThroughService(通過服務連接數據)。
配置客戶端應用程序
在客戶端配置文件中,刪除數據庫連接配置,並添加 WCF 連接配置,如下:
1: <?xml version="1.0"?>
2: <configuration>
3: <configSections>
4: <section name="rafy" type="Rafy.Configuration.RafyConfigurationSection, Rafy" />
5: </configSections>
6: <rafy
7: dataPortalProxy="Rafy.DataPortal.WCF.ClientProxy, Rafy.Domain"
8: collectDevLanguages="IsDebugging">
9: </rafy>
10: <system.serviceModel>
11: <client>
12: <endpoint name="ClientProxyEndPoint" address="http://localhost:8000/RafyServer/Text"
13: binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfig"
14: contract="Rafy.DataPortal.WCF.IWcfPortal" />
15: </client>
16: <bindings>
17: <basicHttpBinding>
18: <binding name="basicHttpBindingConfig" receiveTimeout="00:20:00" sendTimeout="02:00:00" maxReceivedMessageSize="1000000000">
19: <readerQuotas maxDepth="64" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
20: </binding>
21: </basicHttpBinding>
22: </bindings>
23: </system.serviceModel>
24: </configuration>
運行程序
先運行 ServerConsole,成功運行界面:
再運行 ClothesSys,點擊登錄。登錄成功,即說明已經成功使用 C/S 進行部署。
代碼下載
下載地址:http://pan.baidu.com/s/1AB9TL
本文的代碼在“3.使用 CS 部署程序”文件夾中。
歡迎試用 Rafy 領域實體框架,框架發布地址:http://www.cnblogs.com/zgynhqf/p/3356692.html。