F#从FSX脚本编译时管理参考



我需要与SharePoint(以前提(进行交互,并决定尝试F#。我只使用CLI工具做到了或应该很简单。

我设法与网站进行互动并获取所需的信息。我在所需的DLL上挣扎,但最终

#if INTERACTIVE
#r @"C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
#r @"C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
// seems to be required
#r @"[...]Microsoft.Online.SharePoint.Client.Tenant.dll.15.0.4615.1001Microsoft.Online.SharePoint.Client.Tenant.dll"
#r @"[...]SharePointPnPCoreOnline.3.8.1904libnet45OfficeDevPnP.Core.dll"
#endif

使用Fsi REPLFsi script.fsx进行了处理,但我无法编译,以FS文件或FSX脚本为单位。

我的代码是:

open Microsoft.SharePoint.Client;;
let main () = 
    let authnManager = OfficeDevPnP.Core.AuthenticationManager()
    printfn "%A" authnManager
    0
main()

使用FSI运行:

PS> fsi script.fsx
OfficeDevPnP.Core.AuthenticationManager #OK!

尝试编译:

PowerShell>  fsc --warn:5 -r "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll" `
>>      -r "C:Program FilesCommon FilesMicrosoft SharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll" `
>>      -r "absolutepathtoMicrosoft.Online.SharePoint.Client.Tenant.dll.15.0.4615.1001Microsoft.Online.SharePoint.Client.Tenant.dll" `
>>      -r "absolutepathtoSharePointPnPCoreOnline.3.8.1904libnet45OfficeDevPnP.Core.dll"  .script.fsx
Microsoft (R) F# Compiler version 10.4.0 for F# 4.6
Copyright (c) Microsoft Corporation. All Rights Reserved.
> .script.exe
Exception non gérée (unmanaged exception) : System.IO.FileNotFoundException: 
Impossible de charger le fichier ou l'assembly 'OfficeDevPnP.Core,
Version=3.8.1904.0, Culture=neutral, PublicKeyToken=5e633289e95c321a'
ou une de ses dépendances. Le fichier spécifié est introuvable.
   à Script.main()
   à <StartupCode$script>.$Script$fsx.main@()

为什么这种区别?我想念什么?如何使用FSC加载参考(如Nuget安装了相当多的传递依赖关系(?当然,它们也必须与FSC进行管理!(除非OfficeDevPnP.Core.dll ...(

我认为通过f#Interactive中的 #r引用的dll需要在依赖项中。因此,如果SharePoint DLL依赖于官僚主义DLL,则首先需要引用PosineDevpnp DLL(它的#r行需要在SharePoint #r行之前出现(。加载DLL后,您将必须重置交互式会话以按正确的顺序重载它们。

通常,F#Interactive中的加载程序包依赖项非常棘手。有一些f#工具,例如paket,您可以看一下,这可能会使您的生活更轻松一些。另一个选项,如果您已经有一个带有所需的参考的Visual Studio项目,就是使用它来生成脚本文件的软件包引用。您可以读取.fsproj文件并从项目使用的引用中生成#r语句。这样的事情可能有效:

#r "System.Xml"
#r "System.Xml.Linq"
open System
open System.IO
open System.Linq
open System.Xml.Linq
let inline isNotNull o = o |> isNull |> not
let private getProject path =
    Directory.EnumerateFiles(path, "*.*proj") |> Seq.head |> XDocument.Load    
let generateDlls path =
    let projectFile = getProject path
    let references =
        projectFile.Descendants <| XName.Get("HintPath", "http://schemas.microsoft.com/developer/msbuild/2003")
        |> Seq.filter (fun reference -> reference.Value.ToLower().EndsWith(".dll"))
        |> Seq.filter (fun reference -> reference.Value.StartsWith("$") |> not)
        |> Seq.map (fun reference -> reference.Value)
    let projects =
        projectFile.Descendants <| XName.Get("ProjectReference", "http://schemas.microsoft.com/developer/msbuild/2003")
        |> Seq.map (fun reference -> reference.Elements(XName.Get("Name", "http://schemas.microsoft.com/developer/msbuild/2003")).SingleOrDefault())
        |> Seq.filter (fun nameElement -> nameElement |> isNotNull)
        |> Seq.map (fun nameElement -> nameElement.Value)
        |> Seq.map (fun reference -> sprintf "../%s/bin/debug/%s.dll" reference reference)
    references 
    |> Seq.append projects
    |> Seq.iter (fun reference -> printfn "#r @"%s"" reference)

相关内容

  • 没有找到相关文章

最新更新