我正在尝试从 Structuremap 2.6.4.1 移动到 3.1.4.143,但无法弄清楚如何处理 HybridHttpOrThreadLocalScoped。没有一个 SO Q/AI 可以找到似乎对我有用......
我有以下代码:
using StructureMap;
using StructureMap.Web;
ObjectFactory.Configure( x =>
{
x.For<IRepository<Person>>()
.HybridHttpOrThreadLocalScoped() // Need the repo to survive for the duration of the thread!
.Use<Repository_Stub<Person>>()
.Ctor<IEnumerable<IEntity>>( "seed" ).Is( persons );
} );
Run Code Online (Sandbox Code Playgroud)
这给了我这个错误:
'StructureMap.Configuration.DSL.Expressions.CreatePluginFamilyExpression<IRepository<Person>>'
does not contain a definition for 'HybridHttpOrThreadLocalScoped' and no extension method
'HybridHttpOrThreadLocalScoped' accepting a first argument of type
StructureMap.Configuration.DSL.Expressions.CreatePluginFamilyExpression<IRepository<Person>>'
could be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
是否有调用 HybridHttpOrThreadLocalScoped 的新方法?
提前致谢!
结构图方法,scan.WithDefaultConventions(); 在structuremap.MVC 5中假定使用约定IMyClassName,MyClassName作为依赖注入.如果您只创建了类,这没关系.
使用开箱即用的ASP.NET MVC 5应用程序,约定IMyClassName,MyClassName不会退出用户身份.如何配置structuremap以忽略ASP.NET Framework接口/类?
structuremap asp.net-mvc dependency-injection ioc-container structuremap3
我无法让StructureMap将值注入Global.asax.cs文件的MvcApplication的构造函数中.我创建了一个全新的干净项目,并使用StructureMap.MVC5包在DependencyResolution子文件夹中生成必要的结构.
我要注入的课很简单:
namespace SMTest.Models {
public interface ITestSM
{
int OnePlusTwo();
}
public class TestSM : ITestSM
{
public int OnePlusTwo()
{
return 1 + 2;
}
} }
Run Code Online (Sandbox Code Playgroud)
IoC.cs是:
namespace SMTest.DependencyResolution {
using StructureMap;
public static class IoC {
public static IContainer Initialize() {
return new Container(c => c.AddRegistry<DefaultRegistry>());
}
}
}
Run Code Online (Sandbox Code Playgroud)
StructuremapMvc.cs包含:
public static void Start() {
IContainer container = IoC.Initialize();
StructureMapDependencyScope = new StructureMapDependencyScope(container);
DependencyResolver.SetResolver(StructureMapDependencyScope);
DynamicModuleUtility.RegisterModule(typeof(StructureMapScopeModule));
}
Run Code Online (Sandbox Code Playgroud)
DefaultRegistry.cs包含:
public DefaultRegistry() {
Scan(
scan => {
scan.TheCallingAssembly();
scan.WithDefaultConventions(); …Run Code Online (Sandbox Code Playgroud) 继我之前关于如何在结构图中实现IContainer的帖子之后,我已经打了一段时间我希望是我的最后一个问题了.
如何将其他(非结构图注入)对象传递给构造函数?
让我们从我用来测试这些东西的示例控制台应用程序中获取以下内容.
static void Main(string[] args)
{
_container = StructureMapConfig.GetContainer();
_userService = _container.GetInstance<IUserService>();
}
Run Code Online (Sandbox Code Playgroud)
抛出以下错误,因为我的构造函数有randomParam,而structuremap不知道如何填补空白:
StructureMap.dll中发生了一个未处理的"StructureMap.StructureMapBuildPlanException"类型异常
附加信息:无法为具体类型CommonServices.UserService创建构建计划
构造函数:
public UserService(IUserRepository userRepository, IStringService stringService, string randomParam)
{
_userRepository = userRepository;
_stringService = stringService;
}
Run Code Online (Sandbox Code Playgroud)
在我的注册表中,我定义了我的用户服务:
this.For<IUserService>().Use<UserService>();
Run Code Online (Sandbox Code Playgroud)
我的问题是如何以最简单的方式做到这一点?
我找到了这个链接,但看不到如何使用这些建议,因为我必须让我的调用类知道UserService的依赖关系.您可以看到其中一些是数据层项目,我不想告诉我的UI层有关它们.
http://structuremap.github.io/resolving/passing-arguments-at-runtime/
我在我的应用程序中使用了StructureMap和ASP.Net Identity.我有这条线的时候Application_Start
ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());
Run Code Online (Sandbox Code Playgroud)
这是StructureMapControllerFactory:
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
if (controllerType == null && requestContext.HttpContext.Request.Url != null)
throw new InvalidOperationException(string.Format("Page not found: {0}",
requestContext.HttpContext.Request.Url.AbsoluteUri.ToString(CultureInfo.InvariantCulture)));
return ObjectFactory.GetInstance(controllerType) as Controller;
}
}
Run Code Online (Sandbox Code Playgroud)
return ObjectFactory.GetInstance(controllerType) as Controller;抛出StructureMapConfigurationException异常说:
No default Instance is registered and cannot be automatically determined for type 'IUserStore<Person>'
Run Code Online (Sandbox Code Playgroud)
但如果我删除ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());行一切正常,所以它的StructureMap的问题不是我的代码.
好的,我之前的问题/设置有太多的变量,所以我将其分解为简单的骨骼组件。
鉴于以下使用 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) return;
UserId = httpContext.User.GetIdentityId().GetValueOrDefault();
UserName = httpContext.User.Identity.Name;
}
public Guid UserId { get; set; }
public string UserName { get; set; }
}
public static class ClaimsExtensionMethods
public static Guid? GetIdentityId(this IPrincipal …Run Code Online (Sandbox Code Playgroud) c# structuremap asp.net-mvc dependency-injection structuremap3