在kernel.Bind上获取和出错(scanner = 2010 ..."扫描仪"在VS 2010中有一个小错误行.
无法将lambda表达式转换为'System.Type []'类型,因为它不是委托类型
像2.0中的旧kernel.scan一样自动注册.我无法弄清楚我做错了什么.添加和删除了这么多Ninject包.完全迷失了,浪费时间.
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
//using Ninject.Extensions.Conventions;
using Ninject.Web.WebApi;
using Ninject.Web.Mvc;
using CommonServiceLocator.NinjectAdapter;
using System.Reflection;
using System.IO;
using LR.Repository;
using LR.Repository.Interfaces;
using LR.Service.Interfaces;
using System.Web.Http;
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void …Run Code Online (Sandbox Code Playgroud) 在创建StructureMap容器时,我通常会这样做:
var container = new Container(registry => {
registry.AddRegistry<MyRegistry>();
});
Run Code Online (Sandbox Code Playgroud)
哪里
public class MyRegistry : Registry {
public MyRegistry() {
Scan(x => {
x.Assembly("My.Assembly.Name");
x.RegisterConcreteTypesAgainstTheFirstInterface();
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这导致registry.AddRegistrybootstrapper文件中有很多行,然后将其复制到许多项目中.我希望能够调用一个方法来获取一个构造容器并为其添加一个注册表,因此我可以模块化库.
我想出了这个:
public static void Setup(ref Container container) {
container.PluginGraph.ImportRegistry(typeof(MyRegistry));
}
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为注册表被正确地添加到容器中(通过调用container.WhatDoIHave()之前和之后看到),但似乎实际的映射没有完成 - 即接口IFoo没有注册到Foo定义的具体类My.Assembly.Name.
是什么在做差异ImportRegistry和AddRegistry?我的Setup方法可以修复吗?
我有以下在版本2.6.4中工作的StructureMap注册,我最终升级到最新的SM(撰写本文时为3.1.2).并且需要更新它,因为似乎不再有IContext.BuildStack.
这是2.6.4的旧版本:
initialization.For(typeof(IRepository<,>))
.Use(context =>
{
var genericArgs = context.BuildStack.Current.RequestedType.GetGenericArguments();
return RepositoryFactory.GetInstance(genericArgs[0], genericArgs[1], repositoryName);
}
);
Run Code Online (Sandbox Code Playgroud)
所以我认为将其更改为可行:
initialization.For(typeof (IRepository<,>))
.Use("IRepository<,>", context =>
{
var genericArgs = context.ParentType.GetGenericArguments();
return RepositoryFactory.GetInstance(genericArgs[0], genericArgs[1],
repositoryName);
}
);
Run Code Online (Sandbox Code Playgroud)
但context.ParentType为null.当我查看context.RootType时,它被设置为System.Object,这显然不是我想要的.
我获取此存储库实例的测试代码是:
var userRepository = ObjectFactory.GetInstance<IRepository<User, Guid>>();
Run Code Online (Sandbox Code Playgroud)
我没有看到任何其他财产有这些信息,但我猜我错过了什么.
我正在尝试从 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 的新方法?
提前致谢!
我该如何解决这个错误?
版本
Microsoft.AspNet.SignalR.Core 2.2.0,structuremap 3.1.4.143
global.asax signalR依赖解析
// SIGNALR DEPENDENCY RESOLVER
GlobalHost.DependencyResolver = new StructureMapSignalRDependencyResolver(Container ?? ObjectFactory.Container);
Run Code Online (Sandbox Code Playgroud)
StructureMapSignalRDependencyResolver
public class StructureMapSignalRDependencyResolver : DefaultDependencyResolver
{
private readonly IContainer _container;
public StructureMapSignalRDependencyResolver(IContainer container)
{
_container = container;
}
public override object GetService(Type serviceType)
{
object service = null;
//Below is a key difference between this StructureMap example, GetInstance is used for concrete classes.
if (!serviceType.IsAbstract && !serviceType.IsInterface && serviceType.IsClass)
{
//If the type is a concrete type we get here...
service = …Run Code Online (Sandbox Code Playgroud) 我正在使用 StructureMap 来解决依赖关系,这在旧版本中运行良好。但是在更新 StructureMap 版本 4.2.0.40 后,我遇到了这个错误。
ObjectFactory 现在在新版本中已过时。 那么如何修改以下逻辑以适应更新版本。
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
try
{
if ((requestContext == null) || (controllerType == null))
return null;
return (Controller)ObjectFactory.GetInstance(controllerType);
}
catch (StructureMapException)
{
System.Diagnostics.Debug.WriteLine(ObjectFactory.WhatDoIHave());
throw new Exception(ObjectFactory.WhatDoIHave());
}
}
Run Code Online (Sandbox Code Playgroud)
引导程序
public static class Bootstrapper
{
public static void Run()
{
ControllerBuilder.Current
.SetControllerFactory(new StructureMapControllerFactory());
ObjectFactory.Initialize(x =>
{
x.AddConfigurationFromXmlFile(@"D:\Samples\Web_API\OneCode\StructureMap.Web\StructureMap.Web\StructureMap.xml");
});
}
}
}
Run Code Online (Sandbox Code Playgroud) structuremap asp.net-mvc asp.net-mvc-4 structuremap3 structuremap4
在Visual Studio(2017)中调试(F5)我的代码时出现以下错误:
找不到ServiceLocatorImplBase.cs
阅读细节,似乎是在我的计算机上寻找以下位置的文件:
'C:\首页\克里斯\项目\ CommonServiceLocator \主\ Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs'
我没有目录'c:\ Home\Chris ...',我不知道它为什么会在那里看.
我怎样才能解决这个问题?
我使用 StructureMap 4.6 作为我的 IoC 容器。我对它的生命周期有点困惑。正如我在其文档中所读到的,Transient 将为每个容器创建一个对象实例。支持的生命周期
我正在通过创建一个简单的控制台应用程序项目来检查这个场景。我的代码如下:
class Program
{
private static IContainer _Container;
static void Main(string[] args)
{
_Container = Container.For<ConsoleRegistry>();
var serv1 = _Container.GetInstance<IFileService>();
Console.WriteLine($"Name: {_Container.Name}");
Console.WriteLine(serv1.GetUniqueID());
var serv2 = _Container.GetInstance<IFileService>();
Console.WriteLine($"Name: {_Container.Name}");
Console.WriteLine(serv2.GetUniqueID());
Console.ReadKey();
}
}
Run Code Online (Sandbox Code Playgroud)
public class ConsoleRegistry : Registry
{
public ConsoleRegistry()
{
Scan(_ =>
{
_.TheCallingAssembly();
_.WithDefaultConventions();
});
}
}
Run Code Online (Sandbox Code Playgroud)
public interface IFileService
{
string Read(string path);
void Write(string path, string content);
bool FileExists(string path);
string GetUniqueID();
}
Run Code Online (Sandbox Code Playgroud)
在 StructureMap 中,您可以声明一个Forward<,>语句,该语句允许注册单个具体实例,以便由StructureMap 文档中的多个接口解析:
var container = new Container(_ =>
{
// Let's make StatefulCache a SingletonThing in the container
_.ForConcreteType<StatefulCache>().Configure.Singleton();
_.Forward<StatefulCache, IReader>();
_.Forward<StatefulCache, IWriter>();
});
container.GetInstance<IReader>().ShouldBeOfType<StatefulCache>();
container.GetInstance<IWriter>().ShouldBeOfType<StatefulCache>();
Run Code Online (Sandbox Code Playgroud)
我正在考虑可能迁移到 Lamar,它是 StructureMap 的替代品,但我在注册选项中没有看到任何与之匹配的内容。
这在拉马尔可能吗?
我收到这个错误:
StructureMap Exception Code: 202
No Default Instance defined for PluginFamily
Run Code Online (Sandbox Code Playgroud)
我的设置如下:
Console.WriteLine("Structure Map");
SetupSM sm = new SetupSM();
sm.Setup();
ISomeThing someThing = ObjectFactory.GetInstance<ISomeThing>();
Console.WriteLine("something.HowManyTHings: " + someThing.HowManyThings("asdf"));
public class SetupSM
{
public void Setup()
{
var c1 = new Container(config =>
{
config.Scan(scan =>
{
scan.TheCallingAssembly();
scan.WithDefaultConventions();
});
});
var c2 = new Container(x =>
{
x.For<ISomeThing>().Use<SomeThingOne>();
});
}
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次尝试使用结构图,我错过了什么?看来他们主网站上的指南使用旧语法等很老了.