我知道如何在 .NET 5(或之前版本)的 Startup.cs 中进行依赖项注入,但是如何对 .NET 6 中的顶级 Program.cs 执行相同的操作?
.NET 5:例如,我可以在Configure方法中注入一个类
public class Startup
{
public IConfiguration _configuration { get; }
public IWebHostEnvironment _env { get; set; }
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
_configuration = configuration;
_env = env;
}
public void ConfigureServices(IServiceCollection services)
{
// TODO
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IToInjectService serviceToInject)
{
// USE SERVICE
}
}
Run Code Online (Sandbox Code Playgroud)
如何在 .NET 6 中实现这一目标?
我无法让我的注册脚本在 PowerShell (admin) 中运行。它可以在 Powershell ISE 中运行,但随后脚本卡在“正在连接到服务器...”
\n难道我做错了什么?
\n\n这是脚本:
\n $ErrorActionPreference="Stop";If(-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole( [Security.Principal.WindowsBuiltInRole] \xe2\x80\x9cAdministrator\xe2\x80\x9d)){ throw "Run command in an administrator PowerShell prompt"};If($PSVersionTable.PSVersion -lt (New-Object System.Version("3.0"))){ throw "The minimum version of Windows PowerShell that is required by the script (3.0) does not match the currently running version of Windows PowerShell." };If(-NOT (Test-Path $env:SystemDrive\\\'azagent\')){mkdir $env:SystemDrive\\\'azagent\'}; cd $env:SystemDrive\\\'azagent\'; for($i=1; $i -lt 100; $i++){$destFolder="A"+$i.ToString();if(-NOT (Test-Path ($destFolder))){mkdir $destFolder;cd $destFolder;break;}}; $agentZip="$PWD\\agent.zip";$DefaultProxy=[System.Net.WebRequest]::DefaultWebProxy;$securityProtocol=@();$securityProtocol+=[Net.ServicePointManager]::SecurityProtocol;$securityProtocol+=[Net.SecurityProtocolType]::Tls12;[Net.ServicePointManager]::SecurityProtocol=$securityProtocol;$WebClient=New-Object Net.WebClient; $Uri=\'[url]\';if($DefaultProxy -and (-not $DefaultProxy.IsBypassed($Uri))){$WebClient.Proxy= New-Object Net.WebProxy($DefaultProxy.GetProxy($Uri).OriginalString, $True);}; $WebClient.DownloadFile($Uri, $agentZip);Add-Type -AssemblyName …Run Code Online (Sandbox Code Playgroud)