unitycontainer / aspnet-webapi Goto Github PK
View Code? Open in Web Editor NEWUnity.AspNet.WebApi package
License: Apache License 2.0
Unity.AspNet.WebApi package
License: Apache License 2.0
We have an infrequent but persistent issue with Unity.
Once every few thousand requests or so Unity throws a NullReference Exception when the attempting to construct the controller
Now these are requests that work as expected 99.9% of the time, so It's not that we haven't registered the dependencies, which is what I'd expect with this specific exception and message. Frankly I'm more than a little puzzled and would love some pointers on how to track down our issue.
The fact that it's infrequent and unpredictable makes isolating the issue very tricky.
Top Level Exception: System.InvalidOperationException - An error occurred when trying to create a controller of type 'SettingsController'. Make sure that the controller has a parameterless public constructor.
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__15.MoveNext()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()
Inner Exception: Object reference not set to an instance of an object.
at Unity.UnityContainer.<>c.<.ctor>b__73_2(BuilderStrategy[] chain, BuilderContext& context)
at Unity.Builder.BuilderContext.Resolve(Type type, String name, InternalRegistration registration)
at Unity.Builder.BuilderContext.Resolve(Type type, String name)
at Unity.Builder.BuilderContext.Resolve(ParameterInfo parameter, Object value)
at Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context)
at Unity.UnityContainer.<>c.<.ctor>b__73_2(BuilderStrategy[] chain, BuilderContext& context)
at Unity.Builder.BuilderContext.Resolve(Type type, String name, InternalRegistration registration)
at Unity.Builder.BuilderContext.Resolve(Type type, String name)
at Unity.Builder.BuilderContext.Resolve(ParameterInfo parameter, Object value)
at Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context)
at Unity.UnityContainer.<>c.<.ctor>b__73_2(BuilderStrategy[] chain, BuilderContext& context)
at Unity.Builder.BuilderContext.Resolve(Type type, String name, InternalRegistration registration)
at Unity.Builder.BuilderContext.Resolve(Type type, String name)
at Unity.Builder.BuilderContext.Resolve(ParameterInfo parameter, Object value)
at Unity.Strategies.BuildPlanStrategy.PreBuildUp(BuilderContext& context)
at Unity.UnityContainer.<>c.<.ctor>b__73_1(BuilderContext& context)
at Unity.UnityContainer.Unity.IUnityContainer.Resolve(Type type, String name, ResolverOverride[] overrides)
at Unity.UnityContainerExtensions.Resolve(IUnityContainer container, Type t, ResolverOverride[] overrides)
at Unity.AspNet.WebApi.UnityDependencyResolver.SharedDependencyScope.GetService(Type serviceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
未能加载文件或程序集“Unity.AspNet.WebApi, Version=5.0.1.9, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)
[FileLoadException: 未能加载文件或程序集“Unity.AspNet.WebApi, Version=5.0.1.9, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)]
VOCs.WebAPI.App_Start.UnityWebApiActivator.Start() in H:\VOCs\03_Code\00_SourceCode\VOCsV2\06_Web\WebAPI\App_Start\UnityWebApiActivator.cs:20
[TargetInvocationException: 调用的目标发生了异常。]
System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0
System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +114
System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +146
WebActivatorEx.BaseActivationMethodAttribute.InvokeMethod() +81
WebActivatorEx.ActivationManager.RunActivationMethods(Boolean designerMode) +699
WebActivatorEx.ActivationManager.Run() +123
[InvalidOperationException: 针对类型 WebActivatorEx.ActivationManager 的应用程序预启动初始化方法 Run 引发了异常,显示下列错误消息: 调用的目标发生了异常。。]
System.Web.Compilation.BuildManager.InvokePreStartInitMethodsCore(ICollection1 methods, Func
1 setHostingEnvironmentCultures) +902
System.Web.Compilation.BuildManager.InvokePreStartInitMethods(ICollection`1 methods) +169
System.Web.Compilation.BuildManager.CallPreStartInitMethods(String preStartInitListPath, Boolean& isRefAssemblyLoaded) +173
System.Web.Compilation.BuildManager.ExecutePreAppStart() +172
System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +912
[HttpException (0x80004005): 针对类型 WebActivatorEx.ActivationManager 的应用程序预启动初始化方法 Run 引发了异常,显示下列错误消息: 调用的目标发生了异常。。]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +534
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +111
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +718
错误 CS1705 标识为“Unity.AspNet.WebApi, Version=5.0.2.17, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”的程序集“Unity.AspNet.WebApi”所使用的“Unity.Abstractions, Version=2.0.2.67, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”版本高于所引用的标识为“Unity.Abstractions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=6d32ff45e0ccc69f”的程序集“Unity.Abstractions” VOCs.WebAPI H:\WebAPI\App_Start\UnityWebApiActivator.cs 17 活动的
This is NOT an issue but a request, Put in Issue per Eugene's recommendation.
Dear Unity WebApi Authors, Thanks for the great Unity WebApi package, makes my life simple!! I have a quick question, this is my setup Project: AspNet WebApi2, .netframework 4.7, Unity Asp WebAPI latest as the IOC tool I have a class like below
public class Logger: ILogger {
private readonly HttpRequestMessage request;
// Current HttpRequestMessage to be injected by Unity
public Logger(HttpRequestMessage request)
{
this.request = request;
} }
I would like to USE Unity/Unity WebAPI package to inject Current HttpRequestMessage thru IOC above for every request
Can you Please reply soon as I am stuck on this for sometime. Kindly let me know any questions. Thanks again and keep up the great work.
Thanks, Sanjev Saalaivazan
It too me ages to figure out how this works and what it does exactly. Don't see why there isn't a small blurb on how to use it or at least a link to a proper documentation page.
When using Unity with ASP.NET Web API project i added the following packages
<package id="Unity" version="5.8.5" targetFramework="net471" />
<package id="Unity.Abstractions" version="3.3.0" targetFramework="net471" />
<package id="Unity.AspNet.WebApi" version="5.0.13" targetFramework="net471" />
<package id="Unity.Container" version="5.7.0" targetFramework="net471" />
<package id="WebActivatorEx" version="2.2.0" targetFramework="net471" />
However, I can not use PerRequestLifetimeManager
because it is not part of Unity.AspNet.WebApi
. Instead PerRequestLifetimeManager
is part of Unity.Mvc
. and when Unity.Mvc
Nuget package is added to Web Api Project it brings in other Web UI related dependencies like
Microsoft.AspNet.Razor
Microsoft.AspNet.WebPages
Microsoft.AspNet.Mvc
Is there a way to use PerRequestLifetimeManager
in ASP.NET Web API without adding Unity.Mvc
package?
Hello Guys ,
Need to access appsettings.json file or IConfiguration to be injected in my controller how would i declare the same in unity config file.
My project is aspnet-webapi, the project needs upgrading to.Net Core, but you only support net47, net46 and net45, can.Net Core Support?
Why not support netcoreapp2.0 and netstandard2.0
UnityDependencyResolver can also be used with a self-hosting on OWIN
but this package has a dependency on Microsoft.AspNet.WebApi which in its turn
depends on Microsoft.AspNet.WebApi.WebHost.
UnityDependencyResolver, however, has no dependency on any particular hosting environment.
Please, consider extracting UnityDependencyResolver into a separate package.
Hi,
I am trying to use Unity.AspNet.Web API v5.11.1 as IOC container. Couldn't figure it out if I should add something about unity on Application Start. I couldn't find examples, please guide me.
Thanks.
I am trying to implement the dependency injection in ASP.Net web API with unity container and have implemented the exactly with below 2 articles:
Both are related to each other with some minor change but implementation for both are the same.
After implementing I am getting an error in the method GetService in UnityResolver.cs class:
"Resolution failed with error: No public constructor is available for type System.Web.Http.Dispatcher.IHttpControllerActivator."
This is great for controllers but does not work for custom filters and if you add a dependency in your custom filter (property injection) it never get's resolved.
The only way I figured out how to do this was from https://michael-mckenna.com/dependency-injection-for-asp-net-web-api-action-filters-in-3-easy-steps/ where he creates a custom UnityFilterProvider. This is so benificial I'm not sure why it's not in this package...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.