Skip to main content
All docs
V24.2

Access Object Space, Security System, and Caption Helper in Custom Endpoint Methods

  • 4 minutes to read

This topic describes how to access an Object Space, the Security System, and the Caption Helper from custom endpoint methods.

Useful APIs

Object to access

Injecting service

Methods and properties

Object Space

IObjectSpaceFactory
INonSecuredObjectSpaceFactory

IObjectSpaceFactory.CreateObjectSpace
INonSecuredObjectSpaceFactory.CreateObjectSpace

Security System

ISecurityProvider
ISecurityStrategyBase

ISecurityProvider.GetSecurity
ISecurityStrategyBase.IsAuthenticated
ISecurityStrategyBase.User

Caption Helper

ICaptionHelperProvider

ICaptionHelperProvider.GetCaptionHelper

Prerequisites

If you created your application in v21.2 or earlier, you need to use the Application Builder’s ObjectSpaceProviders property to register Object Space Providers instead of the XafApplication.CreateDefaultObjectSpaceProvider method or XafApplication.CreateCustomObjectSpaceProvider event. Refer to the following help topic for more information on how to do this: Integrate Application Builders into Existing Applications.

We strongly recommend this because the use of the XafApplication.CreateDefaultObjectSpaceProvider method and XafApplication.CreateCustomObjectSpaceProvider event conflicts with IObjectSpaceFactory and IObjectSpaceProviderFactory service usage.

The use of IObjectSpaceFactory instead of XafApplication‘s members also improves the performance of your application because the application instance is not created in this case.

Examples

Object Space

To create an Object Space in an ASP.NET Core application, inject the IObjectSpaceFactory service. Note that this service ensures if a user is logged on. If not, it throws an authorization exception. To avoid this exception, you can use the following techniques:

  • Use the INonSecuredObjectSpaceFactory service instead of IObjectSpaceFactory.
  • Use another way to ensure if a user is logged on (for example, use AuthorizationPolicyBuilder.RequireXafAuthentication with AuthorizeAttribute).
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;

namespace MySolution.WebApi {
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CustomEndpointController : ControllerBase {
        IObjectSpaceFactory objectSpaceFactory;
        public CustomEndpointController(IObjectSpaceFactory objectSpaceFactory) {
            this.objectSpaceFactory = objectSpaceFactory;
        }
        [HttpGet]
        public IActionResult Get() {
            using IObjectSpace newObjectSpace = objectSpaceFactory.CreateObjectSpace<Contact>();
            // ...
            return Ok();
        }
        //...
    }
}

Security System

To access the Security System in an ASP.NET Core application, inject the ISecurityProvider service. Note that this service ensures if a user is logged on. If not, it throws an authorization exception. To avoid this exception, you can use the following techniques:

  • Use the ISecurityStrategyBase service instead of ISecurityProvider if you do not need to operate with an authenticated user object. This service does not ensure if a user is logged on or not, so the current user object might not be available here.
  • Use another way to ensure if a user is logged on (for example, use AuthorizationPolicyBuilder.RequireXafAuthentication with AuthorizeAttribute.

These workarounds do not guarantee that you will not receive authentication exceptions, even if you specify the correct user credentials.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Security;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.OData.Routing.Controllers;
using MySolution.Module.BusinessObjects;

namespace MySolution.WebApi {
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class CustomEndpointController : ControllerBase {
        ISecurityProvider securityProvider;
        public CustomEndpointController(ISecurityProvider securityProvider) {
            this.securityProvider = securityProvider;
        }
        [HttpGet]
        public IActionResult Get() {
            ISecurityStrategyBase security = securityProvider.GetSecurity();
            var userId = security.UserId; 
            // ...
            return Ok();
        }
        //...
    }
}

Caption Helper

The Caption Helper class allows you to get localized captions for XAF Controllers and Razor Components that are shown in XAF Views. XAF initializes the Application Model of this class on requests to standard XAF pages. To get captions with other requests (middleware, Web API method, and so on), use the ICaptionHelperProvider service.

This service uses a shared Application Model and does not return user-specific localized strings from a user Model Differences Storage. If your application does not store different captions for different users in the Application Model, use the ICaptionHelperProvider service as a unified way to get localized captions.

using DevExpress.ExpressApp.AspNetCore.Services.Localization;
using DevExpress.ExpressApp.Utils;
using Microsoft.AspNetCore.Mvc;

namespace MySolutionName.Module.Blazor.Controllers {
    public class CustomLocalizationController : ControllerBase {
        internal ICaptionHelperProvider captionHelperProvider;

        public CustomLocalizationController(ICaptionHelperProvider captionHelperProvider) {
            this.captionHelperProvider = captionHelperProvider;
        }

        [HttpGet]
        public string GetActionCaption(string actionName) {
            ICaptionHelper helper = captionHelperProvider.GetCaptionHelper();
            return helper.GetActionCaption(actionName);
        }
    }
}

The CaptionHelper.GetService method returns an ICaptionHelper instance to use on other platforms such as Win and Web.

using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Utils;

ICaptionHelper helper = CaptionHelper.GetService(Application.ServiceProvider)
string newActionName = helper.GetActionCaption("New");
// ...
See Also