我有一个使用Jersey 2.x的简单REST API项目.我尝试使用Google Guice来注入我的依赖项,但它似乎不起作用.我收到此错误:
org.glassfish.hk2.api.UnsatisfiedDependencyException:SystemInjecteeImpl没有可用于注入的对象(requiredType = AccountService,parent = AccountsResource,qualifiers = {},position = 0,optional = false,self = false,unqualified = null,1658198405 )
我有这个简单的资源类
@Path("/accounts")
@Produces(MediaType.APPLICATION_JSON)
public class AccountsResource {
private final AccountService accountService;
@Inject
public AccountsResource(AccountService accountService) {
this.accountService = accountService;
}
@GET
@Path("test")
public String test() {
return this.accountService.test();
}
Run Code Online (Sandbox Code Playgroud)
我想将此服务注入我的资源类
public class AccountService {
public AccountService() {}
public String test() {
return "test";
}
}
Run Code Online (Sandbox Code Playgroud)
因此,按照Guice的指南,我创建了这个模块类
import com.google.inject.*;
public class AccountsResourceModule extends AbstractModule {
@Override
protected void configure() {
bind(AccountService.class); …Run Code Online (Sandbox Code Playgroud) 这篇文章https://medium.com/@dmitryzaets/legacy-net-applications-configuration-management-net-framework-4-5-1-68220335d9d8描述了如何将选项模式与 Autofac 一起使用。我试图将其翻译为与 Simple Injector 一起使用。但我没有运气。这是我的国际奥委会代码
public class IocBootstrap2
{
private Container Container { get; }
public IocBootstrap2()
{
Container = new Container();
var configurationBuilder = new ConfigurationBuilder()
.SetBasePath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Configuration"))
.AddJsonFile("settings.json", optional: false, reloadOnChange: true);
var configuration = configurationBuilder.Build();
//Register Options
Container.Register(typeof(IOptions<>), typeof(OptionsManager<>));
Container.Register(typeof(IOptionsMonitor<>), typeof(OptionsMonitor<>));
Container.Register(typeof(IOptionsFactory<>), typeof(OptionsFactory<>));
Container.Register(typeof(IOptionsMonitorCache<>), typeof(OptionsCache<>));
// Register ConfigurationOptions
Container.RegisterConfigurationOptions2<MailingOptions>(configuration.GetSection("mailing"));
#if DEBUG
Container.Verify();
#endif
}
}
public static class ConfigurationSetupExtensions2
{
public static void RegisterConfigurationOptions2<TOptions>(this Container container, IConfiguration config)
where TOptions : class
{
container.Register(typeof(IOptionsChangeTokenSource<TOptions>), …Run Code Online (Sandbox Code Playgroud) configuration configurationmanager legacy-code simple-injector .net-core-configuration
我正在处理一个奇怪的案件。我有一个 .NET Core 控制台应用程序,其设置如下:
private static async Task Main(string[] args)
{
var runAsService = !(Debugger.IsAttached || args.Contains("console"));
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddLogging(loggingBuilder => { loggingBuilder.AddConsole(); });
services.AddGatewayServers();
services.AddHostedService<GatewayService>();
});
if (runAsService)
await builder.RunServiceAsync();
else
await builder.RunConsoleAsync();
}
Run Code Online (Sandbox Code Playgroud)
然后我IServiceCollection对此设置进行了扩展AddGatewayServers(),如下所示:
public static void AddGatewayServers(this IServiceCollection services)
{
services.AddTransient<IGatewayServer, Server1>();
services.AddTransient<IGatewayServer, Server2>();
services.AddTransient<Func<ServerType, IGatewayServer>>(provider => key =>
{
switch (key)
{
case ServerType.Type1: return provider.GetService<Server1>();
case ServerType.Type2: return provider.GetService<Server2>();
default: return null;
}
}); …Run Code Online (Sandbox Code Playgroud) 我有和 类I1,其中继承( :)并在 中实现。我想注册一个作用域依赖项,以便在单个作用域内引用任何接口时始终返回相同的对象。I2FooI2I1I2I1I2FooFoo
我使用以下注册进行了尝试,但它Foo在同一范围内产生了三个不同的实例:
services.AddScoped<Foo>();
services.AddScoped<I1, Foo>();
services.AddScoped<I2, Foo>();
Run Code Online (Sandbox Code Playgroud)
我的定义如下:
interface I1
{
GetData();
}
interface I2 : I1
{
string Data { set; }
}
class Foo : I2
{
private string myData;
public string Data { set => myData = value; }
public string GetData() => this.myData;
}
Run Code Online (Sandbox Code Playgroud)
功能性使用。
class Bar
{
private I2 _i2;
public Bar(I2 i2) => _i2 = i2;
public void …Run Code Online (Sandbox Code Playgroud) 我有一个使用 RazorPages 的 .NET 5 项目,我设置此代码来验证 Progam.cs 文件中的依赖注入:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseDefaultServiceProvider(options =>
{
options.ValidateOnBuild = true;
options.ValidateScopes = true;
})....
Run Code Online (Sandbox Code Playgroud)
我忘记注册注入到我的页面中的服务,所以我预计当我尝试启动应用程序时,错误页面会显示此类问题,但我不明白为什么它不会发生,因为例如,如果我不注册 ILocalizerService ,就会发生这种情况: 这是我的 RazorPage:
public class SignupModel : IdentityPageModel
{
[BindProperty]
public Models.Account.Signup Signup { get; set; }
private readonly CustomUserManager _userManager;
private readonly ILogger<SignupModel> _logger;
private readonly INcsService _ncsService;
public SignupModel(CustomUserManager userManager,
ILogger<SignupModel> logger,
INcsService ncsService) : base(localizerService)
{
Guard.Against.Null(userManager, nameof(userManager));
Guard.Against.Null(logger, nameof(logger));
Guard.Against.Null(ncsService, nameof(ncsService));
_userManager = userManager;
_logger = logger;
_ncsService = …Run Code Online (Sandbox Code Playgroud) 我只想要字母和特殊符号,/并-在我的文本框中.
如何在自定义验证器中给出表达式?
提前致谢
我不知道是不是我只是厚,但我有一些验证码来检查是或否答案.
我似乎无法让它工作我已经在我的程序中的其他地方使用了代码并且它可以工作,但是如果有的话我无法发现错误.无论如何,代码都会贯穿整个输入字符.
string correctDestenation = Console.ReadLine().ToLower();
while (correctDestenation != "y" || correctDestenation != "n")
{
Console.WriteLine(
"Oops! You must enter a 'y' for yes and a 'n' for no");
correctDestenation = Console.ReadLine().ToLower();
}
Run Code Online (Sandbox Code Playgroud) 在我从事的 Unity iOS 项目中,存在“不同语言链接”问题;这是 xcode 4 中的警告,但在 xcode 5 中变成了错误。
/.../Libraries/SomeFile.mm:145:10: Declaration of 'SomeMethod' has a different language linkage
Run Code Online (Sandbox Code Playgroud)
这是文件:
void SomeMethod(const char* _List);
...
extern "C"
{
void SomeMethod(const char* _List)
...
}
Run Code Online (Sandbox Code Playgroud)
我该如何修复它,或者至少(因为它并不是那么重要,并且至少能够构建一些东西会很好),再次发出警告?
我是编写 C# Api 的新手,正在查看 IdentityServer4 QuickStart1 我发现 QuickstartIdentityServer 模块具有
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace QuickstartIdentityServer
{
public class Program
{
public static void Main(string[] args)
{
Console.Title = "IdentityServer";
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
}
Run Code Online (Sandbox Code Playgroud)
并且API有
using System;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
namespace Api
{
public class Program
{
public static void Main(string[] args)
{
Console.Title = "API";
BuildWebHost(args).Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
}
} …Run Code Online (Sandbox Code Playgroud) 我正在使用.net核心2.1,并且我正在使用本机IoC容器注册服务并需要一个值来注册另一组服务
var hostBuilder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services
.AddSingleton<IFirstService, FirstService>()
.AddMyServices(FirstService.MyProperty);
});
Run Code Online (Sandbox Code Playgroud)
我在哪里AddMySerivces使用扩展IServiceCollection来注册其他依赖项
public static IServiceCollection AddMyServices(
this IServiceCollection serviceCollection, string myProperty)
{
// user myProperty here to register other things
}
Run Code Online (Sandbox Code Playgroud)
有什么好办法FirstService.MyProperty让它传入AddMyServices