none
How to use AppDomainInitializer in Powershell RRS feed

  • Question

  • 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


    René Vajkay

    Sunday, June 19, 2016 8:51 AM

Answers

All replies

  • The answer given in the other forum means that PowerShell is not capable of hosting domains that call PowerShell code. One AppDomian cannot call into another app domain  without special consideration for the declaration of the code.

    The following works without error:

    $appDomainSetup = New-Object -TypeName System.AppDomainSetup  
    $appDomain = [AppDomain]::CreateDomain("TestDomain", $null, $appDomainSetup)

    If we try to add delegate calls it will throw memory violations as you have seen.


    \_(ツ)_/

    Sunday, June 19, 2016 9:43 AM
  • Looking at how we do this in C it is clear that the function as defined is set up as a static public function that can be cast or converted into a callback.  I see no way to do that with a script.

    Why would you want to do this in a script?


    \_(ツ)_/

    Sunday, June 19, 2016 10:01 AM
  • I use Powershell here, because the script will be for our database team. They understand Powershell better than C#. The task I need to do is copy over some DLLs from the build server, load them and based on reflection call other commands. After I am done, I need to delete those DLLs. But because they were loaded in, I cannot delete them.


    René Vajkay

    Sunday, June 19, 2016 10:43 AM
  • Create a scheduled job to delete them after the PS script finishes.


    \_(ツ)_/

    Sunday, June 19, 2016 11:23 AM
    • Marked as answer by René Vajkay Sunday, June 19, 2016 3:56 PM
    Sunday, June 19, 2016 11:25 AM