structuremap

How do I get StructureMap working with an AngularJs / MVC5 and WebApi2 web project

南楼画角 提交于 2019-11-27 14:19:22
问题 So I have an AngularJs/MVC project with normal Controllers and decided to move more to an SPA app and add WebApi2 to pass data back to my UI instead of using MVC. In my Global.asax I had the following for my MVC project: DependencyResolver.SetResolver(new StructureMapDependencyResolver(container)); My WebApiController has a constructor with an IRepository to talk to the database and get some entities back. When my AngularJS web app makes a call to the controller the break points never hit and

Passing constructor arguments when using StructureMap

扶醉桌前 提交于 2019-11-27 14:16:23
I'm using StructureMap for my DI. Imagine I have a class that takes 1 argument like: public class ProductProvider : IProductProvider { public ProductProvider(string connectionString) { .... } } I need to specify the "connectionString at run-time when I get an instance of IProductProvider. I have configured StructureMap as follows: ForRequestedType<IProductProvider>.TheDefault.Is.OfConcreteType<ProductProvider>(). WithCtorArgument("connectionString"); However, I don't want to call EqualTo("something...") method here as I need some facility to dynamically specify this value at run-time. My

StructureMap Beginner | Property Injection

假装没事ソ 提交于 2019-11-27 14:09:51
Part of this question was already asked here : structuremap Property Injection but the answer was never given. With StructureMap, is it possible to do Property Injection such that class SomeController : Controller { public IService Service { get; set; } } gets injected properly? I am a StructureMap supports setter/property injection . So you could do the following: public class SomeController : Controller { [SetterProperty] public IService Service { get; set; } } and then: ObjectFactory.Initialize(x => { x.For<IService>() .Use<ServiceImpl>(); }); or if you don't like the idea of cluttering

StructureMap Auto registration for generic types using Scan

六眼飞鱼酱① 提交于 2019-11-27 13:46:35
I've got an interface: IRepository<T> where T : IEntity while im knocking up my UI im using some fake repository implementations that just return any old data. They look like this: public class FakeClientRepository : IRepository<Client> At the moment im doing this: ForRequestedType<IRepository<Client>>() .TheDefaultIsConcreteType<FakeRepositories.FakeClientRepository>(); but loads of times for all my IEntities. Is it possible to use Scan to auto register all my fake repositories for its respective IRepository? Edit: this is as far as I got, but i get errors saying the requested type isnt

How to use Container instead of ObjectFactory in StructureMap ServiceActivator?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 12:03:25
When implementing DI in WebAPI with StructureMap, we used the ServiceActivator found in Configuring Dependency Injection with ASP.NET WebAPI 2.1 WebAPI + APIController with structureMap public class ServiceActivator : IHttpControllerActivator { public ServiceActivator(HttpConfiguration configuration) {} public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType) { var controller = ObjectFactory.GetInstance(controllerType) as IHttpController; return controller; } } But now with the new StructureMap , my ReSharper suggested: Class

Using structuremap with log4net wrapper

做~自己de王妃 提交于 2019-11-27 10:59:50
问题 I have the following interface: public interface ILogger { void Debug(string message, params object[] values); void Info(string message, params object[] values); void Warn(string message, params object[] values); void Error(string message, params object[] values); void Fatal(string message, params object[] values); } and the following implementation: public class Log4netLogger : ILogger { private ILog _log; public Log4netLogger(Type type) { _log = LogManager.GetLogger(type); } public void

Null User on HttpContext obtained from StructureMap

橙三吉。 提交于 2019-11-27 09:54:09
Ok, my previous question/setup had too many variables, so I'm stripping this down to it's bare bones components. Given the code below using StructureMap3... //IoC setup For<HttpContextBase>().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null )); For<ICurrentUser>().Use<CurrentUser>(); //Classes used public class CurrentUser : ICurrentUser { public CurrentUser(HttpContextBase httpContext) { if (httpContext == null) return; if (httpContext.User == null) return; var user = httpContext.User; if (!user.Identity.IsAuthenticated)

StructureMap singleton usage (A class implementing two interface)

≯℡__Kan透↙ 提交于 2019-11-27 07:04:51
public interface IInterface1 { } public interface IInterface2 { } public class MyClass : IInterface1, IInterface2 { } ... ObjectFactory.Initialize(x => { x.For<IInterface1>().Singleton().Use<MyClass>(); x.For<IInterface2>().Singleton().Use<MyClass>(); }); var x = ObjectFactory.GetInstance<IInterface1>(); var y = ObjectFactory.GetInstance<IInterface2>(); I get two different MyClass instances with the above code. How can I get one? You can use the Forward<,>() registration to tell StructureMap to resolve a type using the resolution of a different type. This should do what you expect:

Xml configuration or Configuration through code?

人盡茶涼 提交于 2019-11-27 05:58:45
问题 I personally like the option to configure StructureMap from C# code. From what I understand, one of the advantages of DI, is that we can easily swap in a new concrete instance. But, if the configuration is defined in code, then the concrete instances are hardcoded in the dll. So, practically, its as good as having hard coded the dependencies, right? I know, during testing it makes life easier... My point is, wouldnt it be better to use xml configuration instead? you want to plugin a new

Operation could destabilize the runtime in StructureMap

孤街醉人 提交于 2019-11-27 03:35:31
问题 I am getting this error in one of my ASP.NET 4.5 MVC application on my local machine. Other applications setup with ASP.NET 4.5 and using StructureMap work fine. Any help/solution on this would be highly appreciated. The line of code that causes this is: using StructureMap; using StructureMap.Graph; namespace Management.Web.DependencyResolution { public static class IoC { public static IContainer Initialize() { ObjectFactory.Initialize(x => { x.Scan(scan => { scan.TheCallingAssembly(); scan