本主题描述了如何实现一个 WCF 中间层应用程序服务器及如何配置 XAF客户端连接到此服务器。

注意
本主题演示可以由解决方案向导自动生成的代码。执行操作时,如果你想要在现有的 XAF 解决方案中实现的显示的功能。如果您要创建一个新的 XAF 解决方案,请使用向导。
 
完整的样例项目是在 http://www.devexpress.com/example=E4599
 
1.打开现有的 XAF 解决方案,启用新的安全系统,创建几个用户帐户。如果您没有此现有的解决方案,看这里如何创建一个基于客户端的安全(2 层架构)教程
2.向 XAF 解决方案中添加一个控制台应用程序项目。这个项目将代表在此示例中的应用程序服务器。
3.将引用添加到您的 XAF 解决方案 (例如,MySolution.Module、 MySolution.Module.Win 和 MySolution.Module.Web) 的模块项目。右键项目,单击创建新项目,然后在对话框中选择“添加引用......” ,切换到项目选项卡,选择模块项目,并单击确定。

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();
}
}

注意

  1. ServerApplication.ApplicationName 属性值是客户端应用程序的名称 (即 XafApplication.ApplicationName) 相同。
  2. ServerApplication.Modules 集合应包含由客户端应用程序直接引用的模块。要查看哪些客户端应用程序要求哪些模块,可以在 WinApplication/WebApplication 的InitializeComponent方法中找到。
  3. QueryRequestSecurityStrategyHandler 对象指定了用户类型、 角色类型和身份验证。
  4. 服务终结点是通过 ServiceHost.AddServiceEndpoint 方法添加的。
  5. 如果您使用自定义权限请求和自定义登录参数,在用户初始化数据服务器之前注册通过静态的 WcfDataServerHelper.AddKnownType 方法。
  6. 如果您想要使用一个自定义的绑定对象,不要使用 WcfDataServerHelper.CreateDefaultBinding 方法。自己创建所需的绑定对象并将它传递给ServiceHost.AddServiceEndpoint 方法。
  7. 当使用 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.

  8. 打开 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);
}
}
  1. ServerSecurityClient.IsSupportChangePassword 属性指示可以通过 ChangePasswordByUser 和 ResetPasswords 操作更改用户密码。如果在服务器端使用AuthenticationStandard 身份验证,则将此属性设置为 true。IfAuthenticationActiveDirectory 使用时,无需初始化的 IsSupportChangePassword 属性,因为它的默认值为 false。请注意,此设置只会影响的 ChangePasswordByUser 和 ResetPasswords 操作的可见性,请不要授予写权限给用户的 StoredPassword 属性。创建相应的成员级别权限,以允许非管理用户更改他们的密码。
    • 备注:


      在调试时,服务器主机名是在连接字符串中的"localhost"。更改根据服务器端设置的端口号。(因为它默认应用程序项目中完成的),您也可以从通过配置应用程序对象的配置文件读取连接字符串。为简单起见在这里连接是硬编码的。
      如果您使用自定义权限请求和自定义登录参数,在用户客户端应用程序在初始化之前注册通过静态的 WcfDataServerHelper.AddKnownType 方法。
       
  2. 当应用程序服务器正在使用中时,在服务器端执行兼容性检查。在 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.");
}
}
}
  1. 编辑 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"。

 
现在你可以运行在应用服务器和客户端应用程序。在解决方案资源管理器中将应用程序服务器项目设置为启动项目,并且运行服务器。要运行客户端应用程序,右击解决方案资源管理器中的应用程序项目,然后选择调试 |启动新实例。下图显示了服务器和客户端。

XAF How to: 实现一个WCF Application Server 并配置它的客户端应用的更多相关文章

  1. Websphere Application Server 环境配置与应用部署最佳实践

    在发布一个运行于 WebSphere Application Server 的 J2EE 应用之前,对服务器进行配置和部署应用是必不可少的一个过程,这个过程是非常复杂的.WAS 为用户提供了可视化的管 ...

  2. 将 Spring 和 Hibernate 与 WebSphere Application Server 一起使用

    本文摘要 如果您考虑将 Spring 或 Hibernate 与 IBM® WebSphere® Application Server 一起使用,则本文向您阐述了如何配置这些框架,以适用于 WebSp ...

  3. WCF入门教程(四)通过Host代码方式来承载服务 一个WCF使用TCP协议进行通协的例子 jquery ajax调用WCF,采用System.ServiceModel.WebHttpBinding System.ServiceModel.WSHttpBinding协议 学习WCF笔记之二 无废话WCF入门教程一[什么是WCF]

    WCF入门教程(四)通过Host代码方式来承载服务 Posted on 2014-05-15 13:03 停留的风 阅读(7681) 评论(0) 编辑 收藏 WCF入门教程(四)通过Host代码方式来 ...

  4. 如果你想深刻理解ASP.NET Core请求处理管道,可以试着写一个自定义的Server

    我们在上面对ASP.NET Core默认提供的具有跨平台能力的KestrelServer进行了详细介绍(<聊聊ASP.NET Core默认提供的这个跨平台的服务器——KestrelServer& ...

  5. WCF入门教程2——创建第一个WCF程序

    本节目标 掌握接口 理解契约式编程 创建宿主程序 创建客户端程序访问服务 什么是接口 认识一下接口 必须知道的接口特性 接口不可以被实例化(常作为类型使用) 实现类必须实现接口的所有方法(抽象类除外) ...

  6. Step by Step 创建一个WCF Service

    原创地址:http://www.cnblogs.com/jfzhu/p/4025448.html 转载请注明出处 (一)创建WCF Service (1)创建WCF Service类库 创建一个Cla ...

  7. 跟我一起学WCF(4)——第一个WCF程序

    一.引言 前面几篇文章分享了.NET 平台下其他几种分布式技术,然而前面几种分布式技术专注于某一特定的领域,并且具有不同编程接口,这使得开发人员需要掌握多个API的使用.基于这样的原因,微软在.NET ...

  8. linux/win7下安装websphere application server

    说明: 1.参考网址:http://www.ibm.com/developerworks/cn/aix/library/au-wasonlinux/ 2.在ibm官网上下载websphere appl ...

  9. 在 WebSphere Application Server V7 集群环境中管理 HTTP session[阅读]

    http://www.ibm.com/developerworks/cn/websphere/library/techarticles/1012_dingsj_wascluster/1012_ding ...

随机推荐

  1. 【java基础学习】GUI

    GUI 容器 布局管理器 组件 菜单 事件处理机制

  2. Java输入流之BufferReader和Scanner的用法!

    在Java中,我们都知道Java的标准输入串是System.in.但是我们却很少在Java中看到谁使用它,这是因为我们平时输入的都是一个字符串或者是一个数字等等.而System.in提供的read方法 ...

  3. qmake的使用(可设置c编译器flag参数)

    本文由乌合之众 lym瞎编,欢迎转载 my.oschina.net/oloroso***还是先说一下当前的系统环境:Ubuntu 14.04 + Qt5.4如果没有安装过QT,可以安装下面几个qt软件 ...

  4. C# DataGrid合并单元格

    1.栏位枚举 private enum DataGridColumn { ROWNUM = , EMPID, EMPNAME, SEX, SALARY, ADRRESS, PHONE, TEL, PO ...

  5. 如何真正抓住微信小程序的红利? 阿禅知乎live总结

    微信App定义 为满足用户某种开发需求.完全基于微信的消息或网页应用,入口是公众号,用户无需离开微信即可完成所有操作,所有需求都在公众号里被满足 微信App的优势 1. 顾客在哪里,就让顾客在哪里看到 ...

  6. http://www.blogjava.net/xylz/archive/2013/08/05/402405.html

    http://www.blogjava.net/xylz/archive/2013/08/05/402405.html

  7. svn更新报错:svn unable to connect to a repository at url

    出现错误:unable to connect to a repository at url 解决办法1. 右键点击本地副本,TortoiseSVN -> Settings -> Saved ...

  8. cocos2dx 3.x(场景(层)的生命周期)

    //进入当前层初第一步始化层调用 bool GameScence::init() { if( !void init() ) { returnfalse; } log("进入当前层初第一步始化 ...

  9. JAVA线程池原理详解二

    Executor框架的两级调度模型 在HotSpot VM的模型中,JAVA线程被一对一映射为本地操作系统线程.JAVA线程启动时会创建一个本地操作系统线程,当JAVA线程终止时,对应的操作系统线程也 ...

  10. ANE 从入门到精通 --- 简单的Whatever

    Flash还是很好的,不过国内没人用. Whatever了. 以下是ANE制作中的一点笔记 分享下 也留作备份 Step1 编写As3 Library package com.eran { impor ...