Run the Application Server as a Windows Service

Important

This topic demonstrates the code that can be generated automatically by the Solution Wizard. Proceed, if you want to implement the demonstrated functionality in the existing XAF solution. If you are creating a new XAF solution, use the wizard instead.

In a production environment, it is convenient to setup 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 - WCF Service 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 error before conversion.

Add the Windows Service Application Server Project

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.

  • Right-click the solution in the Solution Explorer.
  • In the invoked context menu, choose Add | New Project....
  • Choose the DevExpress v18.2 XAF Solution Wizard template.
  • Specify the project name (e.g., MySolutionApplicationServer) and click OK.
  • Select the Application Server Project in the Solution Wizard and press Next.
  • Choose the Windows Service as a Middle Tier Server Type and press Finish

As a result, the MySolutionApplicationServer project will be created from the template.

Copy the Server Configuration from the Console Application Server Project

Copy the ServerApplication.ApplicationName property and ServerApplication.Modules collection initializations from the ServerApplication.cs (ServerApplication.vb) file located in the Console Application Server project to the ApplicationServerService.cs (ApplicationServerService.vb) 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 (e.g., 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 module projects and click OK.

The Windows Service Application Server is configured to use the WCF client connection type, PermissionPolicyUser user type, PermissionPolicyRole role type and AuthenticationStandard authentication type. If your settings for the Console Application Server differ, copy the corresponding code to the MySolutionService.cs (MySolutionService.vb) 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;
        SecurityAdapterHelper.Enable();
        serverApplication.Setup();
        serverApplication.CheckCompatibility();
        serverApplication.Dispose(); 
        Func dataServerSecurityProvider = () =>
            new SecurityStrategyComplex(typeof(PermissionPolicyUser), 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();
    }
}
Note

The SecurityAdapterHelper.Enable method enables the Security Permissions Caching to improve the performance.

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.

Install and Run the Windows Service Application Server

To install and run the application server service, do the following.

  • Build the application server project.
  • Run Visual Studio Command Prompt (from the Windows Start menu).
  • Type "installutil path_to_service_executable" and hit ENTER.
  • Type "net start service_name". You can see the service name in the previous step in the installutil output. The service name can be configured in a designer invoked for the ProjectInstaller.cs (ProjectInstaller.vb) file (change the ServiceInstaller.ServiceName value).

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.

Windows Service Troubleshooting

If the service could not be started via the net start command, start the Event Viewer application and open the Application log to find out what is the issue.

DebugAppServer

If this does not help, refer to the MSDN topics listed below.

See Also