运行时的F#编译代码以缺少的程序集结束

MSX*_*MSX 3 f#

我有一个与此问题的答案中提供的代码相关的问题.

我遇到的问题是CompilerParameters在运行时找不到传递给它的三个引用的assmeblies(System.dll,FSharp.Core.dll,FSharp.Powerpack.dll).我得到的错误是:

unknown-file(0,0):错误0:错误FS0218:无法读取程序集'c:\ user s\utente\documents\visual studio 2010\Projects\TrashSolution\TrashSolution\bin\D ebug\FSharp.Core.dll "

如何告诉编译器在GAC中搜索这些程序集,而不是项目的bin目录?如果我在作为字符串提供的代码中打开命名空间,我如何知道要添加哪些程序集?我在哪里可以获得这些信息?

Jac*_* P. 7

在您链接的答案的代码中,底部有一条线:

let asm = Reflection.Assembly.LoadFrom(fileinfo.Value.FullName)
Run Code Online (Sandbox Code Playgroud)

如果您调用Reflection.Load并传递完全限定的程序集名称,它将尝试从GAC(以及其他一些地方,如果程序集不在GAC中)加载程序集.

let asm =
    Assembly.Load "SampleAssembly, Version=1.0.2004.0, Culture=neutral, PublicKeyToken=8744b20f8da049e3"
Run Code Online (Sandbox Code Playgroud)

如果您不知道完全限定的程序集名称,则必须AssemblyName使用程序集的简单名称创建一个名称,然后调用Reflection.Load带有AssemblyName而不是a 的重载string.

let asmName = AssemblyName "Your.Assembly.Name"
let asm = Assembly.Load asmName
Run Code Online (Sandbox Code Playgroud)

至于知道要加载哪些程序集 - 我不认为有一种简单的方法来以编程方式确定.我现在能想到的唯一两个解决方案:

  1. 如果您对所提供的代码有一些了解(作为字符串),您可以使用它来解析它,FSharpCodeProvider并查看打开的命名空间/模块以及使用的类型.如果您正在查看是否使用了某个特定的命名空间或类型(即,您需要在编译代码时包含程序集引用),您可以创建.fsx名称空间的Map(在您的编译中)和/或为程序集名称键入名称,并使用它来引用适当的程序集.
  2. 您可以通过使用半文档化的Fusion API来"强制"搜索GAC,以枚举GAC中安装的所有程序集,然后使用Reflection检查每个程序集并确定它是否是您需要的程序集.这可能非常慢,所以我会不惜一切代价避免它.如果您决定采用此路线,则还必须使用该Assembly.ReflectionOnlyLoad方法加载程序集!这允许在完成检查后卸载程序集 - 如果使用普通反射,则无法卸载程序集,并且程序可能会因某个OutOfMemoryException或类似程序而崩溃.

编辑:事实证明,通过其简单名称加载程序集成功fsi而不是正常的F#代码,因为fsi自动安装AppDomain.AssemblyResolve事件的处理程序.当您尝试加载程序集时,CLR会触发此事件,但无法解析; 该事件为您提供了一种"手动"解析程序集和/或动态生成程序集并返回它的方法.

如果在FileNotFoundException尝试在F#项目中运行代码时查看凸起,您将在异常的Fusion Log属性中看到类似的内容:

=== Pre-bind state information ===
LOG: User = Jack-Laptop\Jack
LOG: DisplayName = System
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: System | Domain ID: 1
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/
LOG: Initial PrivatePath = NULL
Calling assembly : StackOverflow1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System.DLL.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System/System.DLL.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System.EXE.
LOG: Attempting download of new URL file:///C:/Users/Jack/Documents/Visual Studio 2010/Projects/StackOverflow1/StackOverflow1/bin/Debug/System/System.EXE.
Run Code Online (Sandbox Code Playgroud)

查看该日志的底部,您将看到CLR在放弃之前搜索程序集的位置.

这是一个简单的处理程序,可以让您了解如何使用AppDomain.AssemblyResolve处理程序来手动解析程序集.(注意:需要尝试加载程序集的代码之前添加处理程序!)

System.AppDomain.CurrentDomain.add_AssemblyResolve (
    System.ResolveEventHandler (fun _ args ->
        let resolvedAssembly =
            System.AppDomain.CurrentDomain.GetAssemblies ()
            |> Array.tryFind (fun loadedAssembly ->
                // If this assembly has the same name as the one we're looking for,
                // assume it's correct and load it. NOTE : It may not be the _exact_
                // assembly we're looking for -- then you'll need to adjust the critera below.
                args.Name = loadedAssembly.FullName
                || args.Name = loadedAssembly.GetName().Name)

        // Return null if the assembly couldn't be resolved.
        defaultArg resolvedAssembly null))
Run Code Online (Sandbox Code Playgroud)

如果将该代码添加到新的F#控制台项目,然后是AssemblyName与Assembly.Load一起使用的代码,则应该能够加载System程序集,因为它在F#项目中默认引用,并且在运行时将加载它项目.如果您尝试解决System.Drawing,它将失败,因为我们的自定义事件处理程序无法找到程序集.显然,如果你需要一些更复杂的汇编解析逻辑,你应该以适合你​​的应用程序的任何方式将它构建到事件处理程序中.

最后,这里是异常消息中提到的MSDN白皮书的链接:装配加载的最佳实践.如果您遇到困难并且无法弄清楚如何解决所需的程序集,那么值得一读.