小编Wal*_*lly的帖子

在F#中,为什么同时存在List.empty和List.Empty

在Visual Studio中,Intellisense显示List.emptyList.Empty.有什么不同?他们为什么都在那里?

f#

5
推荐指数
1
解决办法
471
查看次数

找到Microsoft.FSharp.Control.Trampoline引发的异常

我负责F#WPF应用程序。当发生未处理的异常时,它将通过电子邮件发送给我。大多数时候,我能够从错误消息和堆栈跟踪中识别异常的来源。但是,我偶尔会收到类似以下的消息,该消息在堆栈跟踪中不包含我的任何代码。

Summary:
Object reference not set to an instance of an object.
--------------
Details:

System.NullReferenceException: 
Object reference not set to an instance of an object.
   at Microsoft.FSharp.Control.CancellationTokenOps.Start@1234-1.Invoke(Exception e)
   at <StartupCode$FSharp-Core>.$Control.loop@435-40(Trampoline this, FSharpFunc`2 action)
   at Microsoft.FSharp.Control.Trampoline.ExecuteAction(FSharpFunc`2 firstAction)
   at Microsoft.FSharp.Control.TrampolineHolder.Protect(FSharpFunc`2 firstAction)
   at <StartupCode$FSharp-Core>.$Control.-ctor@520-1.Invoke(Object state)
   at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
Run Code Online (Sandbox Code Playgroud)

此堆栈跟踪中有哪些线索可以帮助我识别此异常的来源?

例如,我假设原始异常正在async计算中抛出。(我的大部分异步代码都是基于F#异步块的。)我NullReferenceException可能发生在async块中的任何地方吗?我注意到该异常发生在Start() …

f# asynchronous

5
推荐指数
1
解决办法
121
查看次数

Visual Studio Code是否可以与混合语言.NET Core解决方案一起使用?

是否有以下简单的解决方法?还是这是VSCode和/或特定于语言的扩展中的错误?

我创建了两个项目和一个类似的解决方案:

dotnet new library -lang F# -o .\ClassLibrary
dotnet new console -lang C# -o .\MainProgram
dotnet new sln
dotnet sln add .\ClassLibrary\ClassLibrary.fsproj
dotnet sln add .\MainProgram\MainProgram.csproj
Run Code Online (Sandbox Code Playgroud)

我从MainProgram中添加了对ClassLibrary的项目引用。

dotnet add reference ..\ClassLibrary\ClassLibrary.fsproj
Run Code Online (Sandbox Code Playgroud)

我更新了Program.cs以调用ClassLibrary。

static void Main(string[] args)
{
    ClassLibrary.Say.hello("world.");
}
Run Code Online (Sandbox Code Playgroud)

我可以成功还原,构建和运行该程序。

dotnet restore
dotnet run -p .\MainProgram\MainProgram.csproj
Run Code Online (Sandbox Code Playgroud)

问题是当我在Visual Studio Code中打开解决方案文件夹时。

  • 当我打开Program.cs时,编辑器将F#库类型缺少的红色下划线标出。
  • 当我打开Library.fs时,编辑器几乎用红色强调了所有内容。有趣的是,这些错误来自C#编译器,而不是F#编译器。
  • Ctrl-Shift-B生成带有0个警告和0个错误的项目。
  • VSCode的“问题”窗口显示12个编译器错误。

在此处输入图片说明

在此处输入图片说明

我也尝试过单独打开项目文件夹。

  • 如果在F#库项目中打开VS Code,则Intellisense可以工作。
  • 如果我在C#控制台项目中打开VS Code,则编辑器仍无法识别F#库中的类型。

更新资料

我有VS Code版本1.15.1,安装了五个扩展。

  1. C#,1.12.1
  2. 离子离子锐化,2.33.0
  3. 语言PL / SQL,1.0.4
  4. 单声道调试,0.15.7
  5. XML工具,1.9.2

c# f# .net-core visual-studio-code

5
推荐指数
1
解决办法
555
查看次数

将WPF的控件绑定到F#选项

假设我有一个业务对象(没有AllowNullLiteralAttribute).

type Person(name: string) =
    member val Name = name
    override x.ToString() = name
Run Code Online (Sandbox Code Playgroud)

和视图模型,可选择设置所选人员.

type MainWindowModel() =

    let mutable selectedPerson: Person option = None
        :
    member val People = ObservableCollection<Person>()
    member x.SelectedPerson
        with get() = selectedPerson
        and set(v) =
            if selectedPerson <> v then
                selectedPerson <- v
                x.RaisePropertyChanged("SelectedPerson")
Run Code Online (Sandbox Code Playgroud)

将WPF控件的SelectedItem属性绑定到F#选项属性(不使用AllowNullLiteralAttribute)的最佳方法是什么?

如果我这样做......

<StackPanel>
    <ListBox ItemsSource="{Binding People}"
             SelectedItem="{Binding SelectedPerson}"
             DisplayMemberPath="Name" />
    <TextBlock Text="{Binding SelectedPerson}" />
</StackPanel>
Run Code Online (Sandbox Code Playgroud)

...导致错误,无法将'George'从类型'Person'转换为'Microsoft.FSharp.Core.FSharpOption`1 [Person]'

data-binding wpf f#

4
推荐指数
1
解决办法
806
查看次数

mage.exe,MinVersion和"指定的最低版本无效".

当我运行ClickOnce mage.exe实用程序,使用-MinVersion-Install选项创建新的部署清单时,我收到错误.

> mage.exe -New Deployment -Install true -Version 1.0.0.0 -MinVersion 1.0.0.0
The minimum version specified is not valid.
Run Code Online (Sandbox Code Playgroud)

如何避免此错误消息?

clickonce

4
推荐指数
1
解决办法
503
查看次数

修复了 build.fsx 中“包管理器密钥包未注册”的问题

当我在 Visual Studio Code 中打开一个 Fake 构建脚本时,如下所示:

> dotnet new -i fake-template
> dotnet new fake
> dotnet tool update fake-cli
> code build.fsx
Run Code Online (Sandbox Code Playgroud)

我在编辑器中看到此错误消息,并且没有定义任何假命名空间、模块或类型:

Package manager key 'paket' was not registered in
c:\Users\wallace.kelly\.vscode\extensions\ionide.ionide-fsharp-5.4.0\bin\
Currently registered: nuget
Run Code Online (Sandbox Code Playgroud)

我该如何纠正这个错误?

跑步dotnet fake build效果很好。该错误仅出现在编辑器中。

我有"FSharp.dotNetRoot": "C:\\Program Files\\dotnet\\sdk",我的设置文件。该文件夹包括文件夹 2.1.701 2.2.401 3.1.407 5.0.103 和 5.0.104。

f# f#-fake visual-studio-code ionide

4
推荐指数
2
解决办法
327
查看次数

F# 模块和 GetCurrentClassLogger() 的更好的 NLog 记录器名称

当我将其包含NLog.LogManager.GetCurrentClassLogger()在 F# 模块的顶部时,记录器的名称类似于<StartupCode$NLogTest>.$Program. 我本来希望有类似的事情NLogTest.TestModule

如何为我的 F# 模块获得更好的记录器名称?

这是一个完整的例子:

Program.fs

namespace NLogTest

type TestClass() =
    let logger = NLog.LogManager.GetCurrentClassLogger()
    member _.Run() =
        logger.Info("Inside my class")

module TestModule =
    let logger = NLog.LogManager.GetCurrentClassLogger()
    let Run() =
        logger.Info("Inside my module")

module Program =
    [<EntryPoint>]
    let Main _ =
        TestClass().Run()
        TestModule.Run()
        0
Run Code Online (Sandbox Code Playgroud)

NLogTest.fsproj

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <None Include="NLog.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
    <Compile Include="Program.fs" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="NLog" Version="4.7.13" />
  </ItemGroup>
</Project>
Run Code Online (Sandbox Code Playgroud)

NLog.config

<?xml version="1.0" …
Run Code Online (Sandbox Code Playgroud)

f# nlog

4
推荐指数
1
解决办法
76
查看次数

在Visual Studio Code中,在外部窗口中启动控制台应用程序

我有一个用VS Code打开的控制台应用程序。当我按Ctrl-F5时,程序的输出将与其他文本一起显示在DEBUG CONSOLE窗口中。

如何获取Visual Studio代码以在新的控制台窗口中启动程序?

visual-studio-code

3
推荐指数
1
解决办法
3006
查看次数

运行dotnet build时如何忽略MSB4011

运行时,dotnet build我会多次收到此警告:

C:\Program Files\dotnet\sdk\1.0.4\Microsoft.Common.CrossTargeting.targets(145,3):
warning MSB4011: "*.fsproj.proj-info.targets" cannot be imported again.
It was already imported at "Microsoft.Common.targets (127,3)".
This is most likely a build authoring error.
This subsequent import will be ignored.
[*.fsproj]
Run Code Online (Sandbox Code Playgroud)

如何防止显示这些警告?

f# .net-core dotnet-cli

3
推荐指数
1
解决办法
691
查看次数

F#编译器警告FS0067用于防护时的异常

在F#中,我们可以方便地使用failwithfailwithf抛出异常实例.因此,您可能偶尔需要使用"守卫时"来区分不同的异常情况.

这是一个说明性的例子:

    try
        let isWednesday = DateTime.Now.DayOfWeek = DayOfWeek.Wednesday
        if isWednesday then failwith "Do not run this on Wednesdays!"
    with
    | :? DivideByZeroException -> printfn "You divided by zero."
    | :? Exception as ex when ex.Message.Contains("Wednesdays") -> printfn "I said, %s" ex.Message
    | ex -> printfn "%s" ex.Message
Run Code Online (Sandbox Code Playgroud)

但是,上面的代码会产生两个警告:

Program.fs(14,7): warning FS0067: This type test or downcast will always hold 
Program.fs(14,7): warning FS0067: This type test or downcast will always hold 
Run Code Online (Sandbox Code Playgroud)

我该如何避免这种警告?

f#

3
推荐指数
1
解决办法
98
查看次数