The requested page is not available for the requested platform. You are viewing the content for .NET Framework 4.5.2+ platform.

Medium Trust Support (Obsolete)

  • 5 min to read
Important

We do not guarantee correct product operation under partial trust environments. For more information, refer to Medium Trust is no longer supported in XAF ASP.NET apps.

XAF allows you to create ASP.NET applications that can work at the Medium Trust permission level. This document describes the requirements and restrictions for deploying an ASP.NET application built using XAF on a web host requiring a Medium Trust permission level.

How to Prepare an XAF Application for the Medium Trust Environment

To deploy an XAF application at the Medium Trust permission level, do the following.

  1. If you use the DevExpress.Persistent.BaseImpl assembly in your application, do the following.

    • Open the %PROGRAMFILES(x86)%\DevExpress 19.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl\DevExpress.Persistent.BaseImpl.csproj project in Visual Studio. Invoke the Properties window for the project. In the Signing tab, check the Sign the assembly option to generate a public key for the assembly.
    • Add the "#define MediumTrust" preprocessor directive to the code of each business class that is used in your application, e.g., BaseObject.cs, Person.cs, etc.
    • Rebuild XAF sources. To do this, use the instructions from the How to rebuild assemblies from the source code KB article.
  2. In the Web.config file, set the following option:

    <system.web>
       <trust level="Medium" originUrl="" />
    </system.web>
    

    For details, refer to the trust Element (ASP.NET Settings Schema) topic in MSDN.

  3. Ensure that the requirePermission attribute is set to false for all the sections under the sectionGroup element in the Web.config file.

    <configSections>
        <sectionGroup name="devExpress">
            <section name="compression" requirePermission="false" 
                type="DevExpress.Web.ASPxClasses.CompressionConfigurationSection, DevExpress.Web.v,
                Version=.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
            <section name="themes" requirePermission="false" 
                type="DevExpress.Web.ASPxClasses.ThemesConfigurationSection, DevExpress.Web.v, 
                Version=.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
            <section name="settings" requirePermission="false" 
                type="DevExpress.Web.ASPxClasses.SettingsConfigurationSection, DevExpress.Web.v,
                Version=.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" />
        </sectionGroup>
    </configSections>
    
  4. If you use XPO, set the static XpoDefault.UseFastAccessors property to false, and the static XpoDefault.IdentityMapBehavior property to Strong in the Application_Start method located in the Global.asax.cs (Global.asax.vb) file.

    public class Global : System.Web.HttpApplication {
       protected void Application_Start(object sender, EventArgs e) {
          XpoDefault.UseFastAccessors = false;
          XpoDefault.IdentityMapBehavior = IdentityMapBehavior.Strong; 
       }
       //...
    }
    
  5. Override the GetModelAssemblyFilePath and GetModulesVersionInfoFilePath methods of your WebApplication descendant located in the WebApplication.cs (WebApplication.vb) file.

    using System.Web;
    // ...
    protected override string GetModelAssemblyFilePath() {
        if((HttpContext.Current != null) && (HttpContext.Current.Server != null)) {
            return System.IO.Path.Combine(
                HttpContext.Current.Server.MapPath(""), ModelAssemblyFileName);
        }
        return base.GetModelAssemblyFilePath();
    }
    protected override string GetModulesVersionInfoFilePath() {
        if((HttpContext.Current != null) && (HttpContext.Current.Server != null)) {
            return System.IO.Path.Combine(
                HttpContext.Current.Server.MapPath(""), ModulesVersionInfoFileName);
        }
        return base.GetModulesVersionInfoFilePath();
    }
    
  6. Start your application in Full Trust mode. This can be done in your local development environment. Once you do this, the ModulesVersionInfo and ModelAssembly files will be generated in the application folder. These assemblies should be copied to the root website directory when deployed.
    Note

    The ModulesVersionInfo and ModelAssembly.dll files are bound to the current versions of all modules used in your application. So, if you modify your application, you will need to regenerate and redeploy these assemblies.

After following the steps above, your application is ready for use in a Medium Trust environment.

Note

You can create and update the application's database at the Full Trust permission level. Use the DBUpdater.v..exe to create or update an application's database before deploying a new version. By default, this tool is located in the %PROGRAMFILES(x86)%\DevExpress 19.2\Components\Tools\eXpressAppFramework\DBUpdater\DBUpdater.v19.2.exe path.

Medium Trust Restrictions

  • The built-in Export Action and the Export feature supplied with the Reports V2 Module), Pivot Chart Module and Printing System are not supported.
  • All constructors/properties/fields of your persistent object must have the Public access modifier, so that XPO can access them without using Reflection. For details, refer to the XpoDefault.UseFastAccessors topic.

    For instance, you can declare a property in the following manner.

    private string nickName;
    public string NickName {
       get {
          return nickName;
       }
       set {
          SetPropertyValue(nameof(NickName), ref nickName, value);
       }
    }
    

    However, you cannot implement a property as illustrated below, because read-only properties do not persist and a private field cannot be accessed by XPO at the Medium Trust permission level.

    [Persistent]
    private string nickName;
    public string NickName {
       get {
          return nickName;
       }
    }
    

    For the same reason, only public classes should be declared.

  • The following code (in which the Delayed attribute is used) is not appropriate for the Medium Trust level.

    private XPDelayedProperty fRtf = new XPDelayedProperty();
    [Delayed(nameof(fRtf)), ValueConverter(typeof(StringCompressionConverter))]
    public string Rtf {
       get { return (string)fRtf.Value; }
       set {
          fRtf.Value = value;
          OnChanged();
       }
    }
    

    Use the following code instead.

    [Delayed, ValueConverter(typeof(StringCompressionConverter))]
    public string Rtf {
       get { return GetDelayedPropertyValue<string>(nameof(Rtf)); }
       set {
          SetDelayedPropertyValue<string>(nameof(Rtf), value);
          OnChanged();
       }
    }
    

There are other restrictions that exist in the .NET framework. You can learn more about them in the How To: Use Medium Trust in ASP.NET 2.0 MSDN article.

For more information on trust levels, please refer to the ASP.NET Trust Levels and Policy Files document in MSDN.

See Also