How to use AppDomainInitializer in Powershell

Welcome Forums General PowerShell Q&A How to use AppDomainInitializer in Powershell

Viewing 1 reply thread
  • Author
    Posts
    • #43858
      Participant
      Topics: 1
      Replies: 0
      Points: 0
      Rank: Member

      Hi guys,

      I am trying to create an AppDomain in a Powershell script. I implemented the exact same in C# and it seems to be working fine. However, the Powershell version always fails with an exception. The script code is:

      function Execute($arguments)
      {
          Write-Host "Arguments: " $arguments[0] $arguments[1] $arguments[2]
      }
      
      function Main
      {
          $appDomain = $null
          Try
          {
              $appDomainSetup = New-Object -TypeName System.AppDomainSetup
              $appDomainSetup.AppDomainInitializer = ${function:Execute}
              $appDomainSetup.AppDomainInitializerArguments = @("Test1", "Test2", "Test3")
      
              $appDomain = [AppDomain]::CreateDomain("TestDomain", $null, $appDomainSetup)
          }
          Finally
          {
              If ($appDomain -ne $null)
              {
                  [AppDomain]::Unload($appDomain)
              }
          }
      }
      
      Main
      

      And the exception is: Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. at System.AppDomain.nCreateDomain(String friendlyName, AppDomainSetup setup, Evidence providedSecurityInfo, Evidence creatorsSecurityInfo, IntPtr parentSecur ityDescriptor) …

      We figured out that Powershell is creating a dynamic method from the delegate in AppDomainSetup. But I don’t know to change this behavior or any other way how I can use AppDomainInitializer in Powershell?

      This question has been originally asked on http://stackoverflow.com/questions/37896034/how-to-use-appdomaininitializer-in-powershell.

    • #44694
      Keymaster
      Topics: 18
      Replies: 4872
      Points: 1,903
      Helping HandTeam Member
      Rank: Community Hero

      Could you just use embedded C#?

Viewing 1 reply thread
  • The topic ‘How to use AppDomainInitializer in Powershell’ is closed to new replies.