对于以下“项目”,在解析 Unity 进行 DI 时,我遇到了一个非常烦人且无法解释的错误。
InvalidOperationException - 无法构造 LogWriter 类型。 您必须配置容器来提供此值。
?例如消息; “依赖关系解析失败,类型 = \“WindowsFormsApplication1.Performance\”,名称= \“(没有任何)\”。 while:解决时发生异常。 例外 是: InvalidOperationException - LogWriter 类型不能是 建。您必须配置容器来提供此 价值。 ----------------------------------------------------------- 在 异常发生时,容器为: 解决 WindowsFormsApplication1.性能,(无) 解析参数 构造函数的“lw” WindowsFormsApplication1.Performance(Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter 长, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager 嗯) 解决 Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(无) ”
?例如StackTrace; “ 在 Microsoft.Practices.Unity.UnityContainer.DoBuildUp(类型 t,对象 现有的,字符串名称,IEnumerable
1resolverOverrides)在 e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 第485章 在 Microsoft.Practices.Unity.UnityContainer.Resolve(类型 t, 字符串名称,ResolverOverride[]resolverOverrides)中 e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:line 173 在 Microsoft.Practices.Unity.UnityContainerExtensions.Resolve[T](IUnityContainer 容器,ResolverOverride[] 覆盖) e:\Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainerExtensions.cs:line 504 在 WindowsFormsApplication1.Form1.OnLoad(EventArgs e) 中 D:\Devzone\Tasking\WindowsFormsApplication1\Form1.cs:第 33 行”1 resolverOverrides) in e:\\Builds\\Unity\\UnityTemp\\Compile\\Unity\\Unity\\Src\\UnityContainer.cs:line 515\r\n at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name, IEnumerable
以形式:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
try
{
IUnityContainer container = new UnityContainer();
Performance p = container.Resolve<Performance>();
}
catch (Exception ex)
{
}
}
依赖类:
public class Performance
{
public Performance(LogWriter lw, ExceptionManager em)
{
}
}
配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="loggingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.LoggingSettings, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="exceptionHandling" type="Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.ExceptionHandlingSettings, Microsoft.Practices.EnterpriseLibrary.ExceptionHandling, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<loggingConfiguration name="" tracingEnabled="true" defaultCategory="General">
<listeners>
<add name="Event Log Listener" type="Microsoft.Practices.EnterpriseLibrary.Logging.TraceListeners.FormattedEventLogTraceListener, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
listenerDataType="Microsoft.Practices.EnterpriseLibrary.Logging.Configuration.FormattedEventLogTraceListenerData, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
source="Enterprise Library Logging" formatter="Text Formatter"
log="" machineName="." traceOutputOptions="None" />
</listeners>
<formatters>
<add type="Microsoft.Practices.EnterpriseLibrary.Logging.Formatters.TextFormatter, Microsoft.Practices.EnterpriseLibrary.Logging, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
template="Timestamp: {timestamp}{newline}
Message: {message}{newline}
Category: {category}{newline}
Priority: {priority}{newline}
EventId: {eventid}{newline}
Severity: {severity}{newline}
Title:{title}{newline}
Machine: {localMachine}{newline}
App Domain: {localAppDomain}{newline}
ProcessId: {localProcessId}{newline}
Process Name: {localProcessName}{newline}
Thread Name: {threadName}{newline}
Win32 ThreadId:{win32ThreadId}{newline}
Extended Properties: {dictionary({key} - {value}{newline})}"
name="Text Formatter" />
</formatters>
<categorySources>
<add switchValue="All" name="General">
<listeners>
<add name="Event Log Listener" />
</listeners>
</add>
<add switchValue="All" name="Category2" />
</categorySources>
<specialSources>
<allEvents switchValue="All" name="All Events" />
<notProcessed switchValue="All" name="Unprocessed Category" />
<errors switchValue="All" name="Logging Errors & Warnings">
<listeners>
<add name="Event Log Listener" />
</listeners>
</errors>
</specialSources>
</loggingConfiguration>
<exceptionHandling>
<exceptionPolicies>
<add name="Policy">
<exceptionTypes>
<add name="All Exceptions" type="System.Exception, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
postHandlingAction="NotifyRethrow" />
</exceptionTypes>
</add>
</exceptionPolicies>
</exceptionHandling>
</configuration>
您需要将企业库扩展添加到您的容器中。如果没有它,容器就不会读取配置文件,因此不知道如何创建 Entlib 对象,例如 LogWriter。
尝试获取类型 LogWriter 的实例时发生激活错误,键为“”
检查您的配置文件,它必须具有正确的默认“loggingConfiguration”部分
为了让 Unity 构建 Performance 类,它需要知道如何构建 ILogWriter 的实现。
我在你的代码中看不到任何地方告诉 Unity 为 ILogWriter 接口创建什么类,所以我怀疑你可能需要添加这个。
试试这个:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
try
{
IUnityContainer container = new UnityContainer();
container.RegisterType<ILogWriter, LogWriter>();
container.RegisterType<ExceptionManager>();
container.RegisterType<Performance>(new InjectionConstructor(typeof(ILogWriter), typeof(ExceptionManager));
Performance p = container.Resolve<Performance>();
}
catch (Exception ex)
{
}
}
LogWriter 没有空构造函数或具有所有具体类型参数的构造函数:LogWriter 构造函数。
因此 Unity 无法构建它,正如它所说,它将需要您的帮助来配置容器以提供实现。
作为确认,ExceptionManager 可能会被正确解析,因为它只有一个构造函数,而且也是无参数的:)
我遇到了这个问题,只需重新启动电脑即可让这个问题“消失”。 作为重启的一部分,PC 进行了 MS 更新。 没什么解决方案,但也许对某人有用的评论。