本主題描述了如何實現一個 WCF 中間層應用程序服務器及如何配置 XAF客戶端連接到此服務器。
4.打開新創建的項目的 Program.cs (Program.vb) 文件。將以下代碼添加到 Main 方法 (在此示例中,假定您的 XAF 解決方案稱為"MySolution")。
using System; using System.Collections.Generic; using System.ServiceModel; using DevExpress.Persistent.Base; using DevExpress.Xpo; using DevExpress.Xpo.DB; using DevExpress.ExpressApp; using DevExpress.ExpressApp.MiddleTier; using DevExpress.ExpressApp.Security; using DevExpress.ExpressApp.Security.ClientServer; using DevExpress.ExpressApp.Security.ClientServer.Wcf; using DevExpress.ExpressApp.Security.Strategy; using DevExpress.ExpressApp.Web.SystemModule; using DevExpress.ExpressApp.Win.SystemModule; using DevExpress.ExpressApp.Xpo; // ... static void Main() { try { Console.WriteLine("Starting..."); DataSet dataSet = new DataSet(); string connectionString = "Integrated Security=SSPI;Pooling=false;Data Source=(local);Initial Catalog=MySolution"; ValueManager.ValueManagerType = typeof(MultiThreadValueManager<>).GetGenericTypeDefinition(); ServerApplication serverApplication = new ServerApplication(); serverApplication.ApplicationName = "MySolution"; serverApplication.Modules.Add(new MySolution.Module.MySolutionModule()); serverApplication.Modules.Add(new SystemWindowsFormsModule()); serverApplication.Modules.Add(new SystemAspNetModule()); serverApplication.CreateCustomObjectSpaceProvider += delegate(object sender, CreateCustomObjectSpaceProviderEventArgs e) { e.ObjectSpaceProvider = new XPObjectSpaceProvider(connectionString, null); }; serverApplication.DatabaseVersionMismatch += delegate(object sender, DatabaseVersionMismatchEventArgs e) { e.Updater.Update(); e.Handled = true; }; Console.WriteLine("Setup..."); serverApplication.Setup(); Console.WriteLine("CheckCompatibility..."); serverApplication.CheckCompatibility(); serverApplication.Dispose(); Console.WriteLine("Starting server..."); QueryRequestSecurityStrategyHandler securityProviderHandler = delegate() { return new SecurityStrategyComplex( typeof(SecuritySystemUser), typeof(SecuritySystemRole), new AuthenticationStandard()); }; IDisposable[] disposable; IDataLayer dataLayer = new SimpleDataLayer(XpoTypesInfoHelper.GetXpoTypeInfoSource().XPDictionary, DevExpress.Xpo.DB.MSSqlConnectionProvider.CreateProviderFromString(connectionString, DevExpress.Xpo.DB.AutoCreateOption.None, out disposable)); SecuredDataServer dataServer = new SecuredDataServer(dataLayer, securityProviderHandler); ServiceHost serviceHost = new ServiceHost(new WcfSecuredDataServer(dataServer)); serviceHost.AddServiceEndpoint(typeof(IWcfSecuredDataServer), WcfDataServerHelper.CreateDefaultBinding(), "http://localhost:1451/DataServer"); serviceHost.Open(); Console.WriteLine("Server is started. Press Enter to stop."); Console.ReadLine(); Console.WriteLine("Stopping..."); serviceHost.Close(); Console.WriteLine("Server is stopped."); } catch(Exception e) { Console.WriteLine("Exception occurs: " + e.Message); Console.WriteLine("Press Enter to close."); Console.ReadLine(); } }
注意
-
ServerApplication.ApplicationName 屬性值是客戶端應用程序的名稱 (即 XafApplication.ApplicationName) 相同。
-
ServerApplication.Modules 集合應包含由客戶端應用程序直接引用的模塊。要查看哪些客戶端應用程序要求哪些模塊,可以在 WinApplication/WebApplication 的InitializeComponent方法中找到。
-
QueryRequestSecurityStrategyHandler 對象指定了用戶類型、 角色類型和身份驗證。
-
服務終結點是通過 ServiceHost.AddServiceEndpoint 方法添加的。
-
如果您使用自定義權限請求和自定義登錄參數,在用戶初始化數據服務器之前注冊通過靜態的 WcfDataServerHelper.AddKnownType 方法。
-
如果您想要使用一個自定義的綁定對象,不要使用 WcfDataServerHelper.CreateDefaultBinding 方法。自己創建所需的綁定對象并將它傳遞給ServiceHost.AddServiceEndpoint 方法。
-
當使用 AuthenticationActiveDirectory 時, all the methods of the application server should be invoked in the caller's context (a Windows account under which the client application is running). Refer to the Delegation and Impersonation with WCF and Security in Remoting articles in MSDN for more details on how this can be done, depending on the transport technology used. For instance, in the case of WCF, you can modify the ServiceAuthorizationBehavior.ImpersonateCallerForAllOperations property in the code of your service.
-
打開 Windows 窗體應用程序項目的 Program.cs (Program.vb) 文件。修改 Main 方法,如下所示。
using System.ServiceModel; using DevExpress.ExpressApp; using DevExpress.ExpressApp.Security; using DevExpress.ExpressApp.Security.ClientServer; using DevExpress.ExpressApp.Security.ClientServer.Wcf; // ... [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); EditModelPermission.AlwaysGranted = System.Diagnostics.Debugger.IsAttached; MySolutionWindowsFormsApplication winApplication = new MySolutionWindowsFormsApplication(); string connectionString = "http://localhost:1451/DataServer"; try { WcfSecuredDataServerClient clientDataServer = new WcfSecuredDataServerClient( WcfDataServerHelper.CreateDefaultBinding(), new EndpointAddress(connectionString)); ServerSecurityClient securityClient = new ServerSecurityClient(clientDataServer, new ClientInfoFactory()); securityClient.IsSupportChangePassword = true; winApplication.ApplicationName = "MySolution"; winApplication.Security = securityClient; winApplication.CreateCustomObjectSpaceProvider += delegate( object sender, CreateCustomObjectSpaceProviderEventArgs e) { e.ObjectSpaceProvider = new DataServerObjectSpaceProvider(clientDataServer, securityClient); }; winApplication.Setup(); winApplication.Start(); clientDataServer.Close(); } catch(Exception e) { winApplication.HandleException(e); } }
-
ServerSecurityClient.IsSupportChangePassword 屬性指示可以通過 ChangePasswordByUser 和 ResetPasswords 操作更改用戶密碼。如果在服務器端使用AuthenticationStandard 身份驗證,則將此屬性設置為 true。IfAuthenticationActiveDirectory 使用時,無需初始化的 IsSupportChangePassword 屬性,因為它的默認值為 false。請注意,此設置只會影響的 ChangePasswordByUser 和 ResetPasswords 操作的可見性,請不要授予寫權限給用戶的 StoredPassword 屬性。創建相應的成員級別權限,以允許非管理用戶更改他們的密碼。
-
備注:
在調試時,服務器主機名是在連接字符串中的"localhost"。更改根據服務器端設置的端口號。(因為它默認應用程序項目中完成的),您也可以從通過配置應用程序對象的配置文件讀取連接字符串。為簡單起見在這里連接是硬編碼的。如果您使用自定義權限請求和自定義登錄參數,在用戶客戶端應用程序在初始化之前注冊通過靜態的 WcfDataServerHelper.AddKnownType 方法。
-
-
當應用程序服務器正在使用中時,在服務器端執行兼容性檢查。在 XafApplication.DatabaseVersionMismatch 的事件發生時,應無條件地引發異常。編輯WinApplication.cs (WinApplication.vb) 文件,并按以下方式修改 DatabaseVersionMismatchevent 處理程序。
public partial class MySolutionWindowsFormsApplication : WinApplication { //... private void MySolutionWindowsFormsApplication_DatabaseVersionMismatch( object sender, DevExpress.ExpressApp.DatabaseVersionMismatchEventArgs e) { throw new InvalidOperationException( "The application cannot connect to the specified database " + "because the latter does not exist or its version is older " + "than that of the application."); } } }
-
編輯 Module.cs (Module.vb) 文件位于與平臺無關模塊項目(即你的XXX.Module項目)。注冊以下列方式使用的安全類型。(就是用戶和角色所使用的類型)
using DevExpress.ExpressApp.Security.Strategy; // ... public sealed partial class MySolutionModule : ModuleBase { // ... protected override IEnumerable<Type> GetDeclaredExportedTypes() { List<Type> result = new List<Type>(base.GetDeclaredExportedTypes()); result.AddRange(new Type[] { typeof(SecuritySystemUser), typeof(SecuritySystemRole) }); return result; } }
上面的代碼需要引用 DevExpress.ExpressApp.Security.v15.2 程序集。
默認情況下,導航欄中不會顯示角色的列表,這個行為與2層架構不同,如果想要顯示角色列表,需要手動的在xafml中增加角色列表,列表的名稱是:"SecuritySystemRole_ListView"。
文章列表
留言列表