[英]Unable to consume MSBuild properties in service resolving source generator
I use Injectio and I'm having issue resolving the MSBuild properties.我使用Injectio ,但在解析 MSBuild 属性时遇到问题。 These are also documented in the source generator cookbook .
这些也记录在源代码生成器说明书中。
I made a simple Console application as a minimal reproducible example and initially added Injectio as a NuGet package which I then replaced with local Injectio project reference, so that it helps me debug the MSBuild properties and see what's wrong, more specifically this line of code :我制作了一个简单的控制台应用程序作为最小的可重现示例,并最初将 Injectio 添加为 NuGet package 然后我将其替换为本地 Injectio 项目引用,这样它可以帮助我调试 MSBuild 属性并查看问题所在,更具体地说是这行代码:
public void Initialize(IncrementalGeneratorInitializationContext context)
{
if (!Debugger.IsAttached)
{
Debugger.Launch();
}
...
It ignores the property group below in my .csproj
file.它忽略了我的
.csproj
文件中下面的属性组。 Why?为什么? Do I have to do CompilerVisibleProperty or something?
我必须做 CompilerVisibleProperty 什么的吗? This was also documented in the Injectio README , scroll down to the bottom.
这也记录在Injectio README中,向下滚动到底部。
<PropertyGroup>
<InjectioName>Library</InjectioName>
</PropertyGroup>
All this property is supposed to do is let me use custom name for the registrator instead of services.AddTest()
, eg in this case it should be services.AddLibrary()
.所有这个属性应该做的是让我为注册器使用自定义名称而不是
services.AddTest()
,例如在这种情况下它应该是services.AddLibrary()
。
.ConfigureServices(services =>
{
services.AddTest(); // Expected: => services.AddLibrary()
})
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<PropertyGroup>
<InjectioName>Library</InjectioName>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Injectio" Version="1.0.33" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
</ItemGroup>
</Project>
using Injectio.Attributes;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddTest();
})
.ConfigureLogging(x => x.AddConsole())
.UseConsoleLifetime()
.Build();
await host.RunAsync();
[RegisterSingleton(ServiceType = typeof(IHostedService))]
internal sealed class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly PeriodicTimer _timer = new(TimeSpan.FromSeconds(10));
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (await _timer.WaitForNextTickAsync(stoppingToken))
{
try
{
_logger.LogInformation("Worker running at: {Time}", DateTimeOffset.Now);
}
catch (Exception ex)
{
_logger.LogError(ex, "Oh, no! Something bad happened");
}
}
}
}
It seems like I had to add CompilerVisibleProperty
to the .csproj
file, so it recognizes the MSBuild property.似乎我必须将
CompilerVisibleProperty
添加到.csproj
文件中,因此它可以识别 MSBuild 属性。
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>
<PropertyGroup>
<InjectioName>Library</InjectioName>
</PropertyGroup>
<ItemGroup>
<CompilerVisibleProperty Include="InjectioName" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Injectio" Version="1.0.33" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
</ItemGroup>
</Project>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.