有人可以帮忙吗?
我创建了一个WCF库(不是应用程序),我将在SVC IIS页面中托管它.
但我想在通用的地方加载统一的东西...我可以加载它在global.asax但然后它绑定到IIS Asp.net容器,当做TDD这部分不会执行所以不是我的决议会工作.
WCF库是一个纯类,所以我怀疑我可以使用像OnStartup等任何事件?
唯一的方法我想到了服务从基类继承的内容以及调用某种类型的静态类并为统一进行引导的接口.
我有点失落,这是唯一的方式,还是有一些我不知道的方式.
基本上这个想法是......谁曾经调用过wcf库,那么统一注入需要发生..
有任何想法吗?
谢谢
尝试使用Unity和Prism初始化模块时出现以下错误.该DLL被发现
return new DirectoryModuleCatalog() { ModulePath = @".\Modules" };
Run Code Online (Sandbox Code Playgroud)
找到dll并找到名称
#region Constructors
public AdminModule(
IUnityContainer container,
IScreenFactoryRegistry screenFactoryRegistry,
IEventAggregator eventAggregator,
IBusyService busyService
)
: base(container, screenFactoryRegistry)
{
this.EventAggregator = eventAggregator;
this.BusyService = busyService;
}
#endregion
#region Properties
protected IEventAggregator EventAggregator { get; set; }
protected IBusyService BusyService { get; set; }
#endregion
public override void Initialize()
{
base.Initialize();
}
#region Register Screen Factories
protected override void RegisterScreenFactories()
{
this.ScreenFactoryRegistry.Register(ScreenKeyType.ApplicationAdmin, typeof(AdminScreenFactory));
}
#endregion
#region Register Views and Various Services
protected override …Run Code Online (Sandbox Code Playgroud) 我正在WPF中构建一个类似Visual Studio的应用程序,我在识别组件的最佳架构设计组织时遇到了一些问题.我计划使用Unity作为我的依赖注入容器和Visual Studio单元测试框架,并且可能使用moq来模拟库.
我将首先描述我的解决方案的结构,然后我的问题:
我有一个WPF项目,其中包含:
另一个名为ViewModel的项目包含:
我的初始化逻辑如下:
var window = Container.Resolve<MainView>();
window.Show();
我的MainView构造函数在其构造函数中接收MainViewModel对象:
public MainView(MainViewModel _mvm)
Run Code Online (Sandbox Code Playgroud)
我的MainViewModel为每个面板都有一个Child ViewModel:
public ToolboxViewModel ToolboxVM{get; set;}
public SolutionExplorerViewModel SolutionExplorerVM { get; set; }
public PropertiesViewModel PropertiesVM { get; set; }
public MessagesViewModel MessagesVM { get; set; }
Run Code Online (Sandbox Code Playgroud)我打算创建一个初始化每个面板的InitializePanels()方法.
现在我的问题是:我的MainViewModel.InitializePanels()如何初始化所有这些面板?给出以下选项:
选项1:手动初始化ViewModels:
ToolboxVM = new ToolboxViewModel();
//Same for the rest of VM...
Run Code Online (Sandbox Code Playgroud)
缺点:
选项2:通过注释我的属性来使用setter注入:
[Dependency]
public ToolboxViewModel ToolboxVM{get; set;}
//... Same for rest …Run Code Online (Sandbox Code Playgroud) 有没有办法解决Unity 3的这个问题?
我已经尽一切可能绕过这个消息错误,但我无法解决; 我已经做了我在googles搜索中看到的所有内容.
我几乎放弃并尝试另一种DI解决方案.
我的配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
</configSections>
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="Biblioteca" />
<assembly name="Biblioteca.Contracts" />
<assembly name="Biblioteca.Business" />
<namespace name="Biblioteca" />
<namespace name="Biblioteca.Contracts" />
<namespace name="Biblioteca.Business" />
<container>
<register type="Biblioteca.Contracts.IManterCategoriaBO" mapTo="Biblioteca.Business.ManterCategoriaBO" />
</container>
</unity>
</configuration>
Run Code Online (Sandbox Code Playgroud)
我的界面:
using Biblioteca.Transport;
using System.Linq;
namespace Biblioteca.Contracts
{
public interface IManterCategoriaBO
{
IQueryable<CategoriaDTO> GetAll();
CategoriaDTO GetById(int id);
void Insert(CategoriaDTO dto);
}
}
Run Code Online (Sandbox Code Playgroud)
我的具体课程:
using Biblioteca.Contracts;
using Biblioteca.Transport;
using Biblioteca.Data;
using System;
using System.Linq;
namespace …Run Code Online (Sandbox Code Playgroud) 我使用以下类作为依赖项解析器.参考了http://www.asp.net/web-api/overview/advanced/dependency-injection
public class UnityWebAPIResolver : IDependencyResolver
{
protected IUnityContainer container;
public UnityWebAPIResolver(IUnityContainer container)
{
if (container == null)
{
throw new ArgumentNullException("container");
}
this.container = container;
}
public object GetService(Type serviceType)
{
try
{
return container.Resolve(serviceType); **// This line throws error**
}
catch (ResolutionFailedException)
{
return null;
}
}
public IEnumerable<object> GetServices(Type serviceType)
{
try
{
return container.ResolveAll(serviceType); **// This line throws error**
}
catch (ResolutionFailedException)
{
return new List<object>();
}
}
public IDependencyScope BeginScope()
{
var child = …Run Code Online (Sandbox Code Playgroud) 我在.NET 4.5应用程序中使用了以下代码:
UnityConfig.cs
public static class UnityConfig
{
public static void RegisterComponents()
{
var container = new UnityContainer();
container.RegisterType<a_interfaces.IAMessageRepository, ARepository.AMessageRepository>();
DependencyResolver.SetResolver(new Unity.Mvc5.UnityDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
}
}
Run Code Online (Sandbox Code Playgroud)
Global.asax中
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
UnityConfig.RegisterComponents();
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
var _repo = (a_interfaces.IAMessageRepository)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(a_interfaces.IAMessageRepository));
}
}
Run Code Online (Sandbox Code Playgroud)
我现在需要针对.NET 4,我下载/安装了Unity.Mvc4(版本1.2).我在这一行得到以下错误:
GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);
The type or namespace name 'WebApi' does not exist in the namespace 'Unity' (are you missing an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
有谁知道.NET 4的等效代码是什么?
我这样ApplicationUserManager定义:
public class ApplicationUserManager : UserManager<ApplicationUser, int>
{
public ApplicationUserManager(IUserStore<ApplicationUser, int> store)
: base(store)
{
}
public override Task<IdentityResult> CreateAsync(ApplicationUser user, string password)
{
var result = base.CreateAsync(user, password);
//...
Repository.DoSomething(...); //Repository is null here
//..
}
[Dependency]
public IRepository Repository { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我的存储库没有被注入.Repository总是null
我的统一配置中也有这一行
.RegisterType<ApplicationUserManager>(new HierarchicalLifetimeManager())
Run Code Online (Sandbox Code Playgroud)
如何注射?
更新N:
这是我的控制器的代码; 我是怎么得到的UserManager:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
} …Run Code Online (Sandbox Code Playgroud) dependency-injection unity-container asp.net-identity asp.net-web-api2
我刚开始使用Unity Container,我的注册看起来像这样:
static void UnityRegister()
{
_container = new UnityContainer();
_container.RegisterType<IBook, Book>();
_container.RegisterType<IBookRepository, BookRepository>("Book");
_container.RegisterType<IBookService, BookService>();
_container.RegisterType<IBookRepository, DatabaseRepository>("Database");
}
Run Code Online (Sandbox Code Playgroud)
现在当我尝试解决这个问题:
var service = _container.Resolve<IBookService>("Database");
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
解决依赖关系失败,type ="UnityConsoleEx.IBookService",name ="Database".在解决时发生异常:例外情况是:InvalidOperationException - 当前类型UnityConsoleEx.IBookService是一个接口,无法构造.你错过了类型映射吗?
At the time of the exception, the container was:
Resolving UnityConsoleEx.IBookService,Database
Run Code Online (Sandbox Code Playgroud)
谁能指出我做错了什么?
我有一个基于Template10的应用程序,并希望使用IoC处理我的依赖注入.我倾向于使用Unity来实现这个目标.我的应用程序分为三个程序集:
我有这些问题:
我已经阅读了很多关于DI和IoC的内容,但我需要知道如何在实践中应用所有理论,特别是在Template10中.
注册代码:
// where should I put this code?
var container = new UnityContainer();
container.RegisterType<ISettingsService, RoamingSettingsService);
Run Code Online (Sandbox Code Playgroud)
然后是检索实例的代码:
var container = ???
var settings = container.Resolve<ISettingsService>();
Run Code Online (Sandbox Code Playgroud) 我有一个使用c#和ASP.NET MVC 5编写的应用程序.我也使用Unity.Mvc进行依赖注入.
与许多其他类一起,该类MessageManager在IoC容器中注册.但是,MessageManager该类依赖于TempDataDictionary执行其工作的实例.此类用于为视图编写临时数据.
为了解决MessageManager我需要也注册一个实例的实例TempDataDictionary.我需要能够TempDataDictionary从MessageManager类中添加值,然后我需要从视图中访问临时数据.因此,我需要能够访问TempDataDictionary视图中的相同实例,以便我可以将消息写出给用户.
此外,如果控制器将用户重定向到其他地方,我不想丢失消息,我仍然希望能够在下一个视图上显示消息.
我尝试了以下注册TempDataDictionary和MessageManager:
Container.RegisterType<TempDataDictionary>(new PerThreadLifetimeManager())
.RegisterType<IMessageManager, MessageManager>();
Run Code Online (Sandbox Code Playgroud)
然后在我看来,我有以下解决方案的实例 IMessageManager
var manager = DependencyResolver.Current.GetService<IMessageManager>();
Run Code Online (Sandbox Code Playgroud)
但是,由于某种原因,消息会丢失.也就是说,当我解决时manager,TempDataDictionary不包含MessageManager从控制器添加的任何消息.
如何才能正确注册实例,TempDataDictionary以便数据在查看之前一直存在?
更新
这是我的IMessageManager界面
public interface IMessageManager
{
void AddSuccess(string message, int? dismissAfter = null);
void AddError(string message, int? dismissAfter = null);
void AddInfo(string message, int? dismissAfter = null); …Run Code Online (Sandbox Code Playgroud) unity-container ×10
c# ×8
.net ×1
architecture ×1
asp.net ×1
asp.net-mvc ×1
modularity ×1
mvvm ×1
prism ×1
tdd ×1
template10 ×1
unit-testing ×1
uwp ×1
wcf ×1
wpf ×1