What does WebActivator do?

According to: http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx This new attribute allows you to have code run way early in the ASP.NET pipeline as an application starts up. I mean way early, even before Application_Start. This happens to also be before code in your App_code folder (assuming you have any code in there) has been compiled. To use this attribute, … Read more

Ninject sample application? [closed]

Shameless plug: I have put together a very basic tutorial to get started with Ninject. Have a look at Ninject Mini-Tutorial Part1 and Ninject Mini-Tutorial Part2. That should help go get you started. There you can find a reference to the source code on Github.

Ninject: Registering an already created instance with Ninject?

You can use the ToConstant method which takes an already existing instance and registers it as singleton. var kernel = new StandardKernel(); kernel.Bind<MyClass>().ToConstant(myClassInstance); If you want to something more complex you can use the ToMethod (where you can use a Func to get your instance) combined with the InSingletonScope var kernel = new StandardKernel(); kernel.Bind<MyClass>().ToMethod(context … Read more

Dependency Injection with Ninject and Filter attribute for asp.net mvc

See this answer: Custom Authorization MVC 3 and Ninject IoC If you want to use constructor injection then you need to create an attribute and a filter. /// Marker attribute public class MyAuthorizeAttribute : FilterAttribute { } /// Filter public class MyAuthorizeFilter : IAuthorizationFilter { private readonly IUserService _userService; public MyAuthorizeFilter(IUserService userService) { _userService = … Read more

What is Ninject and when do you use it?

Ninject is dependency injector for .NET, practical realisation of pattern Dependency Injection (form of Inversion of Control pattern). Suppose you have two classes DbRepository and Controller: class Controller { private DbRepository _repository; // … some methods that uses _repository } class DbRepository { // … some bussiness logic here … } So, now you have … Read more

Creating an instance using Ninject with additional parameters in the constructor

The With.ConstructorArgument existed in 1.0 for this purpose. In 2.0, the syntax has changed slightly:- With.Parameters.ConstructorArgument with ninject 2.0 See Inject value into injected dependency for more details and examples of how to use the context, providers and arguments to pass stuff like this around more correctly. EDIT: As Steven has elected to pretend my … Read more

Using HttpContext.Current in WebApi is dangerous because of async

HttpContext.Current gets the current context by Thread (I looked into the implementation directly). It would be more correct to say that HttpContext is applied to a thread; or a thread “enters” the HttpContext. Using HttpContext.Current inside of async Task is not possible, because it can run on another Thread. Not at all; the default behavior … Read more

An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key

Edit: Original answer used Find instead of Local.SingleOrDefault. It worked in combination with @Juan’s Save method but it could cause unnecessary queries to database and else part was probably never executed (executing the else part would cause exception because Find already queried the database and hadn’t found the entity so it could not be updated). … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)