我正在处理一个问题,我有很多iterfaces和他们的实现都是统一创建的.这些类包含一些在常规基础上抛出异常的方法,我想围绕这些类创建动态代理,这样我就可以捕获方法中出现的所有异常,并在其他地方处理它们.
当我在玩Unity时,我想知道是否可以使用Unity Interception完成这样的事情.
即创建一个TransparentProxyInterceptor并围绕这些方法的invocatino包装一个try-catch块.这是可能的,还是我走向了错误的方向?谢谢
有谁知道如何解决Unity Container InjectionConstructor没有Func <string>的任何重载的事实?
this.unityContainer
.RegisterType<IService1Client, Service1Client>()
.Configure<InjectedMembers>()
.ConfigureInjectionFor<Service1Client>(
new InjectionConstructor(() =>
this.unityContainer.Resolve<User>()
.SelectedDepartment
.ApplicationServerUrl
.ToString()));
Run Code Online (Sandbox Code Playgroud)
干杯,
我有Unity DI容器最初使用我的Windows窗体应用程序.在Program.cs我有以下几点:
static void Main()
{
var container = BuildUnityContainer();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(container.Resolve<MainForm>());
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
container.RegisterType<ITest, MyTestClass>();
container.RegisterType<ISomeOtherTest, MyOtherClass>();
return container;
}
Run Code Online (Sandbox Code Playgroud)
在我的MainForm构造函数中,我有以下工作:
private readonly ITest test;
public MainForm(ITest test)
{
this.test = test;
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
容器已解析,代码正常.问题/问题是,我如何实例化一个新表单MainForm,比如说Form2有以下构造函数:
private readonly ISomeOtherTest someOtherTest;
public Form2(ISomeOtherTest someOtherTest)
{
this.someOtherTest = someOtherTest;
InitializeComponent();
}
Run Code Online (Sandbox Code Playgroud)
如果我在我的尝试中尝试以下内容MainForm:
Form2 form2 = new Form2();
form2.Show(); …Run Code Online (Sandbox Code Playgroud) 我的理解是不应该在并发Web请求之间共享DbContext实例,所以绝对不能跨线程.但是如何在非并发Web请求中共享它呢?
由于线程敏捷性(ASP.Net中线程敏捷性的含义是什么?),我是否正确,一个线程可以在它死之前处理多个Web请求?
如果是这样,依赖为每个线程注入DbContext实例是否安全?
原因是我使用Unity,它不包括每个请求的生命周期选项.从MVC,EF - DataContext单例实例Per-Web-Request在Unity中,我想我可以使用自定义LifetimeManager; 我只是想知道使用PerThreadLifetimeManager是否安全和充足.
asp.net-mvc entity-framework dependency-injection unity-container dbcontext
我试图从NuGet商店安装Unity版本3.0.1304.1并收到以下错误,我的项目设置为.NetFramwork V4.0:
无法安装包"Unity 3.0.1304.1".您正在尝试将此软件包安装到以".NETFramework,Version = v4.0"为目标的项目中,但该软件包不包含与该框架兼容的任何程序集引用或内容文件.有关更多信息,请与软件包作者联系.
有谁有任何线索有什么不对?
我希望使用WithMappings.FromMatchingInterface约定将实现特定接口的所有类注册到Unity中.另外,我希望使用接口拦截行为拦截所有已注册的对象.问题是Unity还注册了具体类之间的映射,当这些类被解析时,会抛出一个异常消息:
"[类型]不可拦截"
我意识到使用具体类类型解析对象不是最佳实践,但我想知道为什么Unity会自动为接口添加映射 - >具体类以及具体类 - >按惯例注册时的具体类?这意味着如果添加接口拦截器并使用具体类型解析它将永远不会工作.
我希望得到的结果是Unity在按惯例注册并给它一个接口拦截器时没有添加具体类型 - >具体类型映射,这样我们就可以使用它的具体类型来解析类,如果我们愿意,我们只是没有拦截.
我不想使用VirtualMethodInterceptor因为我不想对类进行更改以便拦截工作,这包括继承MarshalByRef.我还想避免单独注册所有对象.
因此,我的问题是,如何按惯例注册时只注册接口映射?
更新:单独注册类会产生相同的问题,因此假设一旦对象使用interfaceinterceptor注册,则无法通过使用具体类型来解析它.
新注册码:
container.RegisterType<ISomeService, SomeService>(new InjectionMember[]
{
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<TraceInterceptor>()
});
container.RegisterType<ISomeRepository, SomeRepository>(new InjectionMember[]
{
new Interceptor<InterfaceInterceptor>(),
new InterceptionBehavior<TraceInterceptor>()
});
Run Code Online (Sandbox Code Playgroud)
更新2为所有接口添加默认拦截器似乎工作,虽然这个解决方案相当hacky.该解决方案在按惯例进行标准注册之前需要一些代码,并且InterfaceInterceptor在基于约定的注册中删除.
预注册代码
foreach (var type in types)
{
container
.Configure<Interception>()
.SetDefaultInterceptorFor(type.GetInterface("I" + type.Name), new InterfaceInterceptor());
}
Run Code Online (Sandbox Code Playgroud)
一些解释困境的代码:
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using System;
using System.Diagnostics;
using System.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{ …Run Code Online (Sandbox Code Playgroud) 我们运行的网站有大约15.000个实时用户(谷歌分析)(大约1000个请求/秒(性能计数器)).
我们有两个Web服务器后面的负载均衡器.
有时每天有时每周一次我们的Web服务器停止执行请求并开始响应错误,每个请求都记录异常:"System.IndexOutOfRangeException - Index超出了数组的范围."
我们的环境:IIS 8.5,.Net 4.5.0,Mvc 5.1.0,Unity 3.5(与3.0相同),WebActivatorEx 2.0在IIS中,工作进程1和其他设置具有默认值.
我们无法捕捉到任何特定情况造成此错误.应用程序池回收后,一切都没有问题.在每个请求响应错误之前,没有任何与之相关的错误.
过去有一个问题与相关的旧Unity版本有关:https : //unity.codeplex.com/discussions/328841 http://unity.codeplex.com/workitem/11791 看不到我能做些什么.
这里有异常细节:
System.IndexOutOfRangeException
Index was outside the bounds of the array.
System.IndexOutOfRangeException: Index was outside the bounds of the array.
at System.Collections.Generic.List`1.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereListIterator`1.MoveNext()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Microsoft.Practices.Unity.NamedTypesRegistry.RegisterType(Type t, String name)
at Microsoft.Practices.Unity.UnityDefaultBehaviorExtension.OnRegisterInstance(Object sender, RegisterInstanceEventArgs e)
at System.EventHandler`1.Invoke(Object sender, TEventArgs e)
at Microsoft.Practices.Unity.UnityContainer.RegisterInstance(Type t, String name, Object instance, LifetimeManager lifetime)
at Microsoft.Practices.Unity.UnityContainerExtensions.RegisterInstance[TInterface](IUnityContainer container, TInterface instance, LifetimeManager lifetimeManager)
at DemoSite.News.Portal.UI.App_Start.UnityConfig.<>c__DisplayClass1.<RegisterTypes>b__0() …Run Code Online (Sandbox Code Playgroud) asp.net-mvc dependency-injection ioc-container inversion-of-control unity-container
我在ASP.NET WebApi解决方案中使用Unity.WebApi NuGet包(Unity 4.0.1和Unity.WebApi 5.2.3).我面临的问题是,在尝试运行代码时,我收到错误:Make sure that the controller has a parameterless public constructor.我在这里搜索过类似的帖子,但我找不到任何符合我问题的帖子.
请不要只说"添加无参数构造函数",因为它显示您显然不知道IoC是什么以及为什么该语句完全没有意义并且违背了IoC的目的.我之所以这么说是因为我在迄今为止看过的很多其他主题中看到了这一点.
这是我的Startup.cs(我使用的是Owin,所以我没有Global.asax):
public void Configuration(IAppBuilder app) {
var config = new HttpConfiguration();
config.MapHttpAttributeRoutes();
// Registering unity stuff
UnityConfig.RegisterComponents();
app.UseWebApi(config);
}
Run Code Online (Sandbox Code Playgroud)
这是我的UnityConfig.cs:
public static class UnityConfig {
public static void RegisterComponents() {
var container = new UnityContainer();
// Register controller
container.RegisterType<MyController>();
// Register interface
container.RegisterType<IService, Service>();
GlobalConfiguration.Configuration.DependencyResolver = new UnityDependencyResolver(container);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的API控制器:
public class MyController : ApiController {
private IService service
public MyController(IService service) …Run Code Online (Sandbox Code Playgroud) c# dependency-injection inversion-of-control unity-container asp.net-web-api
我有一个asp.net web api应用程序,它使用来自MS Unity.AspNet.WebApi和Unity nuget包的Unity依赖注入库.此外,该应用程序使用ORM的Entity Framework版本6数据库上下文和通用存储库.
Api控制器使用自定义服务类型.自定义服务类使用EF数据库上下文和通用存储库.
我的问题是:HierarchicalLifetimeManager和ContainerControlledLifetimeManager是否适用于我的web api应用程序的终身管理器?
我的应用程序的UnityConfig类中的代码:
using System;
using System.Configuration;
using System.Data.Entity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using App.Api.Models;
using App.Dal;
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
/// <summary>
/// Gets the configured Unity container.
/// </summary>
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
/// <summary>Registers the type mappings with the Unity container.</summary>
/// <param name="container">The …Run Code Online (Sandbox Code Playgroud) dependency-injection inversion-of-control unity-container repository-pattern asp.net-web-api
我正在使用Unity进行DI,并且遇到以下错误:-
“尝试创建类型为'UploadController'的控制器时发生错误。请确保该控制器具有无参数的公共构造函数。”
我有以下UnityResolver:
public class UnityResolver : IDependencyResolver, IDependencyScope, System.Web.Http.Dependencies.IDependencyResolver
{
protected IUnityContainer container;
public UnityResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType);
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType);
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
public IDependencyScope BeginScope()
{
var child = container.CreateChildContainer();
return new UnityResolver(child);
}
public void …Run Code Online (Sandbox Code Playgroud)