Unity.WebAPI | simple integration of Microsoft 's Unity IoC container | Dependency Injection library
kandi X-RAY | Unity.WebAPI Summary
kandi X-RAY | Unity.WebAPI Summary
The easiest way to add Unity.WebApi to your project is via the NuGet package. You can search for unity.webapi using the GUI or type the following into the package manager console in Visual Studio:. Once installed, just add a call to UnityConfig.RegisterComponents() in the Application_Start method of Global.asax.cs and the Web API framework will then use the Unity.WebAPI DependencyResolver to resolve your components. Add your Unity registrations in the RegisterComponents method of the UnityConfig class. All components that implement IDisposable should be registered with the HierarchicalLifetimeManager to ensure that they are properly disposed at the end of the request.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Unity.WebAPI
Unity.WebAPI Key Features
Unity.WebAPI Examples and Code Snippets
Community Discussions
Trending Discussions on Unity.WebAPI
QUESTION
Why I'm I getting this error on Employee Controller rest of them are working perfectly Here is my Employee Controller
...ANSWER
Answered 2017-Jul-18 at 08:10Make sure that the controller has a parameterless public constructor
QUESTION
I am using Asp.Net MVC 5 and am trying to wire up my dependencies for a web api controller but it doesnt work and says the controller needs a parameter less constructor.
I have updated unity container to v4 which meant updating some other references, namely unity.abstractions is also v4.
I am using unity.mvc not unity.mvc5.
In UnityConfig I have
...ANSWER
Answered 2019-Apr-01 at 21:42You will need the Unity.AspNet.WebApi adapter to configure your ApiControllers.
Careful you don't mix up packages. Some of those are not by the same project owner (Unity.Mvc5, Unity.WebAPI). I haven't used those so I can't comment on its suitability.
I have a example https://github.com/jasenhk/MovieStar from another answer that uses Unity v5.10 and Unity.MVC as well as Unity.AspNet.WebApi.
Here is the packages.config
QUESTION
I have implemented Unity.WebAPI in web api project successfully. Some how new instance of MService class is not being created. I get following error message when i run unit test method.
Unable to get default constructor for class MServiceTests.
Service Class
...ANSWER
Answered 2018-Nov-15 at 15:20You are missing the [TestClass] attribute on the test class (assuming your are using MSTest framework). Test classes need to have an empty default constructor or no constructors at all.
For setting up the tests you can either arrange it the way that you
- Manually create the required instances or
- Use a mocking framework or
- Do the same initializations just like in your application regarding dependency injection, and then resolve the instances as required
Maybe you also want to take a look, at the Arrange-Act-Assert pattern for unit tests: (see http://defragdev.com/blog/?p=783)
Also keep in mind, that you rather want to test the code and not the DI framework.
QUESTION
Is it possible to expose class public properties in different class through IOC. I am creating an instance of Interface but i am not able to access public properties of class. I am using Unity.WebApi for resolving dependencies.
TransactionService Class
...ANSWER
Answered 2018-Nov-14 at 19:31If you are in control of the services then refactor the interfaces to expose the desired members
QUESTION
Sorry i am new to IOC concepts. I have been trying to implement Unity.WebAPI (5.3.0.) in my web api project but getting following error;
- An error occurred when trying to create a controller of type 'TransactionController'. Make sure that the controller has a parameterless public constructor
UnityResolver Class
...ANSWER
Answered 2018-Nov-14 at 17:12Check to make sure that all dependencies for the object graphs have been registered so that they can be resolved correctly.
You are most likely missing a dependency registration.
For TransactionService
, you are passing/injecting the implementations in the constructors
QUESTION
I'm trying to implement a web application using ASP.NET MVC and the Microsoft Unity DI framework. The application needs to support multiple user sessions at the same time, each of them with their own connection to a separate database (but all users using the same DbContext; the database schemas are identical, it's just the data that is different).
Upon a user's log-in, I register the necessary type mappings to the application's Unity container, using a session-based lifetime manager that I found in another question here.
My container is initialized like this:
...ANSWER
Answered 2018-Oct-15 at 17:43the issue seems to originate from your registration call, when registering the same type multiple times with unity, the last registration call wins, in this case, that will be data access object for whoever user logs-in last. Unity will take that as the default registration, and will create instances that have the connection to that user's database.
The SessionLifetimeManager is there to make sure you get only one instance of the objects you resolve under one session.
One option to solve this is to use named registration syntax to register the data-access types under a key that maps to the logged-in user (could be the database name), and on the resolve side, retrieve this user key, and use it resolve the corresponding data access implementation for the user
QUESTION
I configured Unity in my Web API application to inject the dependencies, but it's throwing an exception when I try to register the DbContext with PerRequestLifetimeManager. It works fine if I use the default LifeTimeManager. I have Unity.MVC and Unity.Webapi nuget packages added to the app. Here is the code for RegisterTypes.
...ANSWER
Answered 2018-Jun-09 at 22:53I figured it out. Just like the error message says, I was trying to inject the DbContext in a non Http Request context at the time of application initialization. It works fine if I do it as shown below, without trying to configure the "InjectionConstructor" inside RegisterType for Repositories. It successfully injects a new instance of DbContext in the repository every request. Seems like you can't use InjectionConstructor for Types that are registered with "PerRequestLifetimeManager".
QUESTION
My website gives...
Could not load file or assembly 'System.Web.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
... and yet in web.config, I have...
...ANSWER
Answered 2017-Dec-28 at 23:43Many hours later I discovered that this error only appears on my default route and only on my dev machine. It was probably doing it all along and I assumed that something had changed after I re-checked out the code.
What is a word for this?
QUESTION
How tell to Unity.WebApi dependency injection framework, inject the correct class in the correct controller?
DI Project Container
...ANSWER
Answered 2017-Oct-06 at 01:05Rather than using a strategy or facade to solve this, a better solution would be to redesign your interfaces to be unique per controller. Once you have a unique interface type, your DI container will automatically inject the right service into each controller.
Option 1Use a generic interface.
QUESTION
This definitely appears to be a different issue than the Multithreading issue.
Using Unity with Dependency Injection(DI) for all my services and using an entity framework context which appears to be initialized and working correctly.
Currently using .net 4.5 with mvc and Unity.WebApi and entity framework 6.0.0.0.
I want to know if its possible to persist a service in this model or use outside of DI and be able to close and re-initialize entity framework in the persisted service if possible.
Currently getting this error when I try to persist a service through a stack class so its performance will be very fast b/c I'm using more frequently.
...ANSWER
Answered 2017-Sep-12 at 16:17Since I had stored a copy of my service with my entity's DBContext it was failing because it was being used by multiple threads. The fix was to new it up each time because it had originally came from a Unity DI resolver method and the entity doesn't get newed up each time the stored service and its methods are called.
Its unknown if the current context can be cleared up a different or better way to make it perform faster when finished with a specific use case scenario in a particular web api thread or end point instead of newing it up in each method call of the saved service.
Solution every time its stored in a static variable and reused on each method:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Unity.WebAPI
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page