【问题标题】:Can't get ShadowCopy to work无法让 ShadowCopy 工作
【发布时间】:2014-12-16 09:24:23
【问题描述】:

我在程序集 1 中有以下代码

Public Interface ITest
    Sub Run()
End Interface

Public Class Runner
    Inherits MarshalByRefObject

    Public Sub Run(pluginPath As String)
        For Each assemblyPath As String In Directory.GetFiles(pluginPath, "*.dll", SearchOption.AllDirectories)
            ScanAssemblyForTasks(assemblyPath)
        Next
    End Sub

    Private Sub ScanAssemblyForTasks(assemblyPath As String)
        Dim assembly As Assembly = assembly.LoadFile(assemblyPath)
        For Each type As Type In assembly.GetTypes().Where(Function(t) t.IsClass AndAlso
                                                                       Not t.IsAbstract AndAlso
                                                                       Not t.IsGenericType AndAlso
                                                                       GetType(ITest).IsAssignableFrom(t))

            Dim test As ITest = CType(type.GetConstructor(type.EmptyTypes).Invoke(Nothing), ITest)
            test.Run()
        Next
    End Sub
End Class

Module Module1

    Private mAppDomain As AppDomain
    Private mCachePath As String
    Private mPluginDirectory As String
    Private mRunner As Runner

    Sub Main()
        mCachePath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "ShadowCache")
        mPluginDirectory = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, "Assemblies")
        If (Not Directory.Exists(mCachePath)) Then
            Directory.CreateDirectory(mCachePath)
        End If
        If (Not Directory.Exists(mPluginDirectory)) Then
            Directory.CreateDirectory(mPluginDirectory)
        End If
        Console.WriteLine("Press any key to load assemblies")
        Console.ReadKey()

        Dim setup As New AppDomainSetup()
        setup.ApplicationName = "MyApplication"
        setup.ShadowCopyFiles = "true"
        setup.CachePath = mCachePath
        setup.ShadowCopyDirectories = mPluginDirectory
        setup.ConfigurationFile = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile

        mAppDomain = AppDomain.CreateDomain("MyApplication", AppDomain.CurrentDomain.Evidence, setup)

        mRunner = CType(mAppDomain.CreateInstanceAndUnwrap(GetType(Runner).Assembly.FullName, GetType(Runner).FullName), Runner)
        mRunner.Run(mPluginDirectory)

        Console.WriteLine("Press any key to end application")
        Console.ReadKey()
    End Sub

End Module

在位于 mPluginDirectory 的 Assembly2 中,我有以下代码

Public Class Class1
    Implements ITest

    Public Sub Run() Implements ITest.Run
        Console.WriteLine("Run")
    End Sub
End Class

当我运行应用程序时,Assembly2 被加载到应用程序域并打印“运行”,但目录 mCachePath 是空的,我无法在应用程序运行时修改/删除 Assembly2.dll,为什么要添加?当我告诉它使用它时,t .net 使用卷影复制功能。

【问题讨论】:

    标签: .net vb.net appdomain shadow-copy


    【解决方案1】:

    我找到了一个解决方案,将Assembly.LoadFile 替换为Assembly.LoadFrom,它就可以了,不要问我为什么...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-23
      • 2014-12-26
      • 2017-12-22
      • 2012-02-28
      • 2015-12-01
      • 2012-01-29
      • 2013-04-19
      • 2014-09-20
      相关资源
      最近更新 更多