我刚刚在 F# 中创建了一个简单的类库,其中包含以下 fsproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Compile Include="Dto.fs" />
</ItemGroup>
</Project>
现在我想在具有以下 csproj
的 C# 项目中引用此内容:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="4.2.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="3.4.0" />
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
<PackageReference Include="Serilog.Sinks.Splunk" Version="3.6.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\<path>\<to>\<file>.fsproj" />
</ItemGroup>
</Project>
在 Controller 中,我现在尝试引用 F# 类库中的类型,但确实收到以下编译器错误:
Controllers/ProviderController.cs(4,53): error CS0234: The type or namespace name 'Types' does not exist in the namespace 'App.Api' (are you missing an assembly reference?) [/app/src/some more path Api/File.csproj] Controllers/ProviderController.cs(22,28): error CS0246: The type or namespace name 'Provider' could not be found (are you missing a using directive or an assembly reference?) [/homepath.csproj]
实际 Controller 如下所示:
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using NamespaceTo.Types.Dto;
namespace Api.Controllers
{
[ApiController]
[Route("[controller]")]
[ApiVersion("1.0")]
public class ProviderController : ControllerBase
{
private readonly ILogger<ProviderController> _logger;
public ProviderController(ILogger<ProviderController> logger)
{
_logger = logger;
}
[HttpGet]
public IEnumerable<Provider> Get()
{
return null;
}
}
}
似乎 F# 项目根本无法识别,因为我没有获得有关 F# 中定义的类型的任何智能感知。如前所述,构建也失败。在 C# netcoreapp3.1 项目中使用 F# 类型时我还缺少什么吗?
F# 项目中的 Dto.fs
文件:
module Api.Types.Dto
open System
type Provider = {
Id: string
Name: string
Logo: string
}
type Text = {
Short: string
Long: string
}
type ProductTexts = {
Default: Text
Alternatives: Text
}
type ProductAlternative = {
Id: string
IsDefault: bool
Priority: int
ContractLength: int
MonthlyPrice: float
EffectiveRate: float
StartupFee: float
MonthlyFee: float
TotalPrice: float
TotalCost: float
}
type Product = {
Id: string
Name: string
Priority: int
Provider: Provider
Texts: ProductTexts
MinimumAmount: float
MaximumAmount: float
Alternatives: ProductAlternative list
}
type ProductResponse = {
Updated: DateTime
ArticleId: string
Price: float
Currency: string
OperatingChain: string
}
type PaymentOption = {
ArticleIds: string list
Product: Product
}
type PaymentOptionsResponse = PaymentOption list
更新:
当我尝试使用另一个 F# 控制台应用程序中的项目时,我确实遇到了同样的问题。
最佳答案
正如 Slack 上所述:
最初的担忧是 Api.Types.Dto 是顶级模块而不是命名空间 - 这是隐式使用命名空间Api.Types
并向其中添加 Dto
模块。
因此,C# 将 Dto
视为一种类型,而不是可以打开的命名空间 - 对于 C# 来说,诸如 Provider
之类的内部类型只是嵌套类型。
这里有两个解决路径:
第一个是打开命名空间:using Api.Types
,然后点入嵌套类型:Dto.Provider
第二种是使用静态 Api.Types.Dto
关于C#项目引用netcoreapp3.1中的F#类lib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64672130/