我刚拿起Simple Injector,我目前想做的就是:
Container.Register<IMessageHandler>(() => new MessageHandlerOne());
Container.Register<IMessageHandler>(() => new MessageHandlerTwo());
Run Code Online (Sandbox Code Playgroud)
我只是想注册两个消息处理程序,所以我可以稍后迭代它们.
要恢复这些注册的实例,这种方法听起来就像它可以做到的那样.
var instances = Container.GetAllInstances<IMessageHandler>();
//why doesn't instances contain both MessageHandlerOne, and MessageHandlerTwo ?
Run Code Online (Sandbox Code Playgroud)
相反,这会返回一个空的IEnumerable.
我究竟做错了什么?对于名为Simple Injector的东西,您会认为这会起作用吗?我是否以错误的方式思考这个问题?
有谁知道,我必须在 Unity 项目目录结构中放置一个文件(sqlite 文件),以便在 apk 编译和安装后,该文件仍存储在 Android 端的 persistedDataPath 中?
提前致谢!
我以前在我的一个属性中设置了属性注入设置
Container.RegisterInitializer<PermitAttribute>(initialize =>
{
initialize.QueryProcessor = Container.GetInstance<IQueryProcessor>();
});
Run Code Online (Sandbox Code Playgroud)
用法是
public class PermitAttribute : ActionFilterAttribute
{
public IQueryProcessor QueryProcessor { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但在更新到simpleinjector之后2.6.1属性注入破了.当我试图访问QueryProcessorPermitAttribute内的对象时.它解析了null值,因为Simple Injector配置仍然通过委托实例注入相同的属性.
由于它在v2.5中工作并且在2.6.1中不再起作用,属性注入行为是否有任何重大变化?
更新1:
配置中的Line为MVC过滤器提供程序注册v2.6.1中的属性引发了错误
container.RegisterMvcIntegratedFilterProvider();
Run Code Online (Sandbox Code Playgroud)
为此我评论了它.它阻止了房产注入工作.属性注入是我的一个属性.我猜这就是影响它的线.它在v2.6.1中抛出错误
更新2:
信息
已为另一个Container实例注册了MVC筛选器提供程序.此方法不支持为不同容器注册MVC筛选器提供程序.
堆栈跟踪 :
at SimpleInjector.SimpleInjectorMvcExtensions.RequiresFilterProviderNotRegistered(Container container)
at SimpleInjector.SimpleInjectorMvcExtensions.RegisterMvcIntegratedFilterProvider(Container container)
at RemsPortal.App_Start.SimpleInjectorInitializer.Initialize() in d:\Projects Work\RemsPortal\V2.0 Web Portal\RemsPortal\App_Start\SimpleInjectorInitializer.cs:line 39
Run Code Online (Sandbox Code Playgroud)
更新3:
整个配置
public static void Initialize()
{
var container = new Container();
InitializeContainer(container);
container.RegisterMvcIntegratedFilterProvider();
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());
container.Verify();
DependencyResolver.SetResolver(new SimpleInjectorDependencyResolver(container));
}
private static void InitializeContainer(Container Container)
{
Container.RegisterManyForOpenGeneric(typeof(IAsyncCommandHandler<,>),
AppDomain.CurrentDomain.GetAssemblies());
Container.RegisterOpenGeneric(typeof(ITransactionCommandHandler<,>),
typeof(TransactionCommandHandlerDecorator<,>)); …Run Code Online (Sandbox Code Playgroud) 所以,我在团结中使用C#来使用Unity UI放置一些对象,并且我有一个对象,其在脚本中的位置与它在场景中的位置不同.我在函数中有以下代码:
using UnityEngine;
using System.Collections;
public class SeedChecker : MonoBehaviour {
Vector3 lockedPos;
RectTransform tran;
// Use this for initialization
void Awake () {
tran = GetComponent<RectTransform>();
}
public void LockPos(Vector3 pos) {
tran.localPosition = pos;
lockedPos = pos;
print (tran.localPosition);
}
}
Run Code Online (Sandbox Code Playgroud)
例如,此代码打印其x位置为-60 - 这是我正在寻找的正确数字 - 屏幕中间左侧60个像素.但是,当我查看检查器和场景时,x位置为0 - 并且对象正好位于屏幕中间.这是一个相当令人沮丧的情况,我在这里也谈过这个问题.这令人沮丧,因为它说它正在获得正确的位置,但是当我将这个正确的位置分配给对象时,它会转到我认为不正确的位置.这是对RectTransforms中localposition如何工作的误解吗?我的对象是一些对象的子对象,但是它们全部归零到屏幕的中心,并且对于缩放值具有"1".
我正在使用Simple Injector,希望查看是否可以注册/解析继承抽象类的类的集合。
情况如下:我具有以下抽象/具体类:
public abstract class Animal
{
public string Type { get; set; }
public abstract string MakeSound();
}
public class Dog : Animal
{
public override string MakeSound()
{
return "woof";
}
}
public class Cat : Animal
{
public override string MakeSound()
{
return "meow";
}
}
public class Pig : Animal
{
public override string MakeSound()
{
return "oink";
}
}
Run Code Online (Sandbox Code Playgroud)
此外,我有一个应接收an的类,IEnumerable<Animal>并MakeSound为每个动物调用该函数,如下所示:
public class Zoo
{
private IEnumerable<Animal> _animals;
public Zoo(IEnumerable<Animal> animals) …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Simple Injector在C#中实现域事件模式.
我已经将我的代码简化为一个文件,该文件可以作为控制台应用程序运行,并且已经排除了Simple Injector代码,以便为此问题保持清晰.
我遇到的问题是每个事件可能有多个事件处理程序,并且可能会引发多个事件,但我想限制我的Dispatcher只处理实现该IEvent接口的事件,所以我把这个限制放在我的Dispatch方法上.
这引起了如何从Simple Injector获取实例的问题,因为每次Dispatch调用该方法都是TEvent类型IEvent(正如我所期望的那样)但是我需要获取传入的事件的类型,以便我可以从Simple获取相关的处理程序注射器.
希望我的代码能够更好地解释这个:
interface IEvent
{
}
interface IEventHandler<T> where T : IEvent
{
void Handle(T @event);
}
class StandardEvent : IEvent
{
}
class AnotherEvent : IEvent
{
}
class StandardEventHandler : IEventHandler<StandardEvent>
{
public void Handle(StandardEvent @event)
{
Console.WriteLine("StandardEvent handled");
}
}
class AnotherEventHandler : IEventHandler<AnotherEvent>
{
public void Handle(AnotherEvent @event)
{
Console.WriteLine("AnotherEvent handled");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的调度员:
static class Dispatcher
{
// I …Run Code Online (Sandbox Code Playgroud) 我正在处理一个应用程序,我在Startup.cs部分类中遇到错误,我无法找到为什么会出现此错误:
以下方法或属性之间的调用不明确:Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection,System.Action)Microsoft.Extensions.DependencyInjection.OptionsServiceCollectionExtensions.Configure(Microsoft.Extensions.DependencyInjection.IServiceCollection) ,System.Action)
我的两个部分Startup.cs类是
using Microsoft.Extensions.DependencyInjection;
using System.Security.Claims;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
namespace GiftUWish.WebAPI
{
public partial class Startup
{
private void SetServices(IServiceCollection services)
{
//PROFILE service and repository
services.AddScoped<IProfileService, ProfileService>();
services.AddScoped<IProfileRepository, ProfileRepository>();
////GIFT service and repository
services.AddScoped<IBankAccountService, BankAccountService>();
services.AddScoped<IBankAccountRepository, BankAccountRepository>();
//EVENT service and repository
services.AddScoped<ICreditCardService, CreditCardService>();
services.AddScoped<ICreditCardRepository, CreditCardRepository>();
//CREDIT CARD INFORMATION serice and repository
services.AddScoped<IPayPalService, PayPalService>();
services.AddScoped<IPayPalRepository, PayPalRepository>();
//CHIPPED IN serive and repository
services.AddScoped<IShippingAdressService, ShippingAdressService>();
services.AddScoped<IShippingAdressRepository, ShippingAdressRepository>();
//UNIT OF WORK
services.AddScoped<IUnitOfWork, UnitOfWork>();
//Authentication-Token , Header which contains information about …Run Code Online (Sandbox Code Playgroud) 背景
我正在构建一个winforms应用程序,我正在使用IoC容器(SimpleInjector)来注册我的类型.在我的应用程序中,大多数屏幕(即表单)在任何给定时间只有一个实例.
问题
对于在任何给定时间只需要一个实例的表单,我可以将它们注册为单例:
container.Register<IHomeView, HomeView>(Lifestyle.Singleton);
Run Code Online (Sandbox Code Playgroud)
这允许我使用容器来跟踪所有表单.但是,在这种情况下,当表单被关闭时,它将被释放(表单实现IDisposable).如果应用程序尝试使用容器再次打开该表单,则将处置该表单的容器实例,并抛出异常.
题
处理这个问题的正确方法是什么?我目前看到两种解决方案:
我可以通过在验证期间抑制诊断警告来解决问题b).
registration = container.GetRegistration(typeof(ILoginView)).Registration;
registration.SuppressDiagnosticWarning(
DiagnosticType.DisposableTransientComponent,
"Winforms registration supression.");
Run Code Online (Sandbox Code Playgroud)
在这里采取正确的方法是什么?我错过了什么吗?
这个问题与史蒂文的答案有关 - 这里.他提出了一个非常好的记录器包装器.我将在下面粘贴他的代码:
public interface ILogger
{
void Log(LogEntry entry);
}
public static class LoggerExtensions
{
public static void Log(this ILogger logger, string message)
{
logger.Log(new LogEntry(LoggingEventType.Information,
message, null));
}
public static void Log(this ILogger logger, Exception exception)
{
logger.Log(new LogEntry(LoggingEventType.Error,
exception.Message, exception));
}
// More methods here.
}
Run Code Online (Sandbox Code Playgroud)
所以,我的问题是创建代理Serilog的实现的正确方法是什么?
注意:这个问题与关于log4net的这个问题有关,但现在特定于Serilog.
似乎直到Dispose在控制器作用域中解析的元素上调用之前,执行上下文都不会保留。这可能是由于asp.net核心必须在本机代码和托管代码之间跳转,并在每次跳转时重置执行上下文。似乎在处置范围之前不再恢复正确的上下文。
以下内容演示了该问题-只需将其放入默认的asp.net核心示例项目中并注册TestRepo为临时依赖项即可。
调用时,GET api/values/我们在调用开始时在静态AsyncLocal中将当前任务的值设置为5。该值按预期方式流动,毫无问题。但是,在调用之后处置控制器及其依赖项时,AsyncLocal上下文已被重置。
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly TestRepo _testRepo;
public ValuesController(TestRepo testRepo) => _testRepo = testRepo;
[HttpGet()]
public async Task<IActionResult> Get()
{
_testRepo.SetValue(5);
await Task.Delay(100);
var val = _testRepo.GetValue(); // val here has correctly 5.
return Ok();
}
}
public class TestRepo : IDisposable
{
private static readonly AsyncLocal<int?> _asyncLocal = new AsyncLocal<int?>();
public int? GetValue() => _asyncLocal.Value;
public void SetValue(int x) => _asyncLocal.Value = x;
public void …Run Code Online (Sandbox Code Playgroud) c# ×8
.net ×3
.net-core ×1
asp.net-core ×1
asp.net-mvc ×1
generics ×1
logging ×1
serilog ×1
transform ×1
unityscript ×1
winforms ×1