locked
Accessing my assembly in GAC fails in LegacyCasmodel enabled RRS feed

  • Question

  • User1022657388 posted

    I have a helper assembly dll developmed using C# which i invoke from my web application during a transaction.

    I need my web application to be full trust and wanted to enable legacyCasModel in it, so i have added the following lines in the web.config of the application

    <trust level="Full" legacyCasModel="true" />

    The C# dll was loading from GAC without any issues and i was able to call the function of the assembly till the time i have added this trust tag in the web.config.

    After the addition of legacyCasModel="true", the request to the web app is getting failed and there was an exception thrown in the event logs as

    Failed to initialize the AppDomain:/LM/W3SVC/2/ROOT

    Exception: System.IO.FileLoadException

    Message: Loading this assembly would produce a different grant set from other instances. (Exception from HRESULT: 0x80131401)

    StackTrace: at System.Web.Configuration.HttpConfigurationSystem.EnsureInit(IConfigMapPath configMapPath, Boolean listenToFileChanges, Boolean initComplete)
    at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel) at System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel) at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironment(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters) at System.Web.Hosting.ApplicationManager.CreateAppDomainWithHostingEnvironmentAndReportErrors(String appId, IApplicationHost appHost, HostingEnvironmentParameters hostingParameters)

    If i comment out the function bodies in the assembly and just return; the control, the exception is not occuring. If any of the functions in the assembly have .Net Framework classes like System.Web.HttpContext then the exception gets thrown.

    How can i resolve this error without disabling legacyCasModel? Should i implement any security attributes to my assembly ?

    Note: My assembly is installed in GAC with AnyCPU, .Net 3.5 compiled. It is digitally signed with digicert but does not have x509Certificate in the evidence(does it matters?? )

    Monday, September 10, 2018 9:26 AM

All replies

  • User283571144 posted

    Hi vsselvaraj ,

    As far as I know, the legacyCasModel is enabled it will affect the legacy code access.

    I guess your assembly is the legacy code, so it will show this error.

    More details about what security rules will apply, you could refer to this article.

    https://docs.microsoft.com/en-us/dotnet/api/system.web.configuration.trustsection.legacycasmodel?view=netframework-4.7.2#remarks 

    Besides, I suggest you could try below solution to solve this issue.

    You could create a new registry DWORD value called LoaderOptimization and give it the value 1 within the key “HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework”
    Perform IISRESET and check Web Apps behavior.

    In key ‘HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework’, create a new ‘DWORD (32-bit) Value’ named “LoaderOptimization” with a value 1 (either in decimal or hexadecimal as they are the same).


    In key ‘HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework‘, create a new ‘DWORD (32-bit) Value’ named “LoaderOptimization” with a value 1 (either in decimal or hexadecimal as they are the same).

    This configuration will disable the loader optimization of assemblies that provoke the aforementioned behavior by setting assembly loading into SingleDomain mode.

    Best Regards,

    Brando

    Tuesday, September 11, 2018 2:52 AM
  • User1022657388 posted

    Thanks Brando,

    Yeah.. I have tried with LoaderOptimization and it worked. It seems to be a work around And  i dont want to do in that way. 

    I like to find out a solution that will load even without LoaderOptimization and disabling legacyCasModel. 

    Tuesday, September 11, 2018 6:18 AM
  • User-1492487179 posted

    hello,

    Microsoft official documents recommended that we used the LoaderOptimization when occurred this issue . After researched, it seem that we can not have the methods bypass modifying  LoaderOptimization  and legacyCasModel setting. What's your consideration for above workaround?

    Regards,

    Tuesday, October 9, 2018 9:21 AM