expressappframework-113360-data-security-and-safety-security-system-security-tiers-middle-tier-security-xpo-run-the-application-server-as-a-windows-service.md
Important
The Template Kit generates the code shown in this help topic when you create an application. Follow this article if you want to implement the demonstrated functionality in an existing XAF solution.
In a production environment, it is convenient to set up and run the XAF Application Server as a Windows Service. This topic describes how to convert the Console Application Server implemented in the Middle Tier Security topic to a Windows Service. While debugging, use the Console Application Server. Convert it to a Windows Service before final deployment. Ensure that the Console Application Server operates with no errors before conversion.
Although the Middle Tier application server can be implemented as a regular Windows Service, XAF provides a project template to simplify this task. So to add a Windows Service application server project to your solution, do the following:
As a result, the MySolutionApplicationServer project will be created from the template.
Copy the ServerApplication.ApplicationName property and ServerApplication.Modules collection initializations from the ServerApplication.cs file located in the Console Application Server project to the ApplicationServerService.cs file.
using DevExpress.ExpressApp.Security.ClientServer.Wcf;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
// …
public partial class ApplicationServerService : System.ServiceProcess.ServiceBase {
// …
protected override void OnStart(string[] args) {
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
ValueManager.ValueManagerType = typeof(MultiThreadValueManager<>).GetGenericTypeDefinition();
ServerApplication serverApplication = new ServerApplication();
serverApplication.ApplicationName = "DxSampleService";
serverApplication.Modules.BeginInit();
serverApplication.Modules.Add(new MySolutionWindowsFormsModule());
serverApplication.Modules.Add(new MySolutionAspNetModule());
serverApplication.Modules.EndInit();
//...
}
//...
}
Do not forget to add required references to your module projects (for example, MySolution.Module , MySolution.Module.Win , and MySolution.Module.Web ). Right-click the newly created Application Server project and choose Add reference…. In the invoked dialog, switch to the Projects tab, select the module projects, and click OK.
The Windows Service Application Server is configured to use the WCF client connection type, ApplicationUser user type (derived from PermissionPolicyUser), PermissionPolicyRole role type, and AuthenticationStandard authentication type. If your settings for the Console Application Server differ, copy the corresponding code to the MySolutionService.cs file. Below is an example for WCF (the OnStart and OnStop methods of the MySolutionService class are modified).
using DevExpress.ExpressApp.Security.ClientServer.Wcf;
using DevExpress.ExpressApp.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
// …
public partial class ApplicationServerService : System.ServiceProcess.ServiceBase {
// …
protected override void OnStart(string[] args) {
//...
serverApplication.CheckCompatibilityType = CheckCompatibilityType.DatabaseSchema;
serverApplication.CreateCustomObjectSpaceProvider += (s, e) =>
e.ObjectSpaceProvider = new XPObjectSpaceProvider(connectionString);
serverApplication.ConnectionString = connectionString;
serverApplication.Setup();
serverApplication.CheckCompatibility();
serverApplication.Dispose();
Func dataServerSecurityProvider = () =>
new SecurityStrategyComplex(typeof(ApplicationUser), typeof(PermissionPolicyRole), new AuthenticationStandard());
serviceHost = new WcfXafServiceHost(connectionString, dataServerSecurityProvider);
string serviceEndPoint = @"net.tcp://localhost:1451/DataServer";
serviceHost.AddServiceEndpoint(typeof(IWcfXafDataServer), WcfDataServerHelper.CreateNetTcpBinding(), serviceEndPoint);
serviceHost.Open();
}
protected override void OnStop() {
serviceHost.Close();
}
}
A reference to the System.ServiceModel.dll assembly is required when using WCF. To connect the application server to a database provider, specify the connection string in the configuration file (App.config) located in the application server project, or explicitly set it to the connectionString variable in the code above.
Do the following to install and run the application server service:
To stop the service, type “net stop service_name“. To uninstall it, type “installutil path_to_service_executable /u”. Do not forget to stop the service each time you need to rebuild the application server project. Otherwise, Visual Studio will not be able to replace the service executable with the new one.
Note
If you experience any difficulties with these steps, refer to the MSDN topics listed below.
If the service could not be started with the net start command, start the Event Viewer application and open the Application log to determine the issue.
If this does not help, refer to the MSDN topics listed below.
See Also
Execute Direct SQL Queries in Integrated Mode and with XPO Middle Tier Security