ris*_*ism 6 c# unity-container
对于以下"项目",我在解析Unity for DI时会遇到一个非常烦人且无法解释的错误.
InvalidOperationException - 无法构造LogWriter类型.您必须配置容器以提供此值.
?ex.Message; "依赖项的解析失败,类型= \"WindowsFormsApplication1.Performance \",name = \"(none)\".\ r \n发生异常时:解析时.\ r \n \nException是:InvalidOperationException - LogWriter类型不能是您必须配置容器以提供此值.\ r \n ---------------------------------- -------------\r\NAT异常的时间,该容器是:\ r \n\r \n解决WindowsFormsApplication1.Performance,(无)\ r \n解决参数\ "LW \" 构造WindowsFormsApplication1.Performance的(Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter LW,Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionManager EM)\ r \n解决Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter,(无)\r \n"
?ex.StackTrace; "在Microsoft.Practices.Unity.UnityContainer.DoBuildUp(类型t,对象存在,字符串名称,IEnumerable
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, IEnumerable1 resolverOverrides)中的e:\ Builds\Unity\UnityTemp\Compile\Unity\Unity\Src\UnityContainer.cs:485行\在电子r \ñ在Microsoft.Practices.Unity.UnityContainer.Resolve(T型,字符串名称,ResolverOverride [] resolverOverrides):\构建\统一\ UnityTemp \编译\统一\统一\ SRC\UnityContainer.cs:线173 \在电子r \ñ在Microsoft.Practices.Unity.UnityContainerExtensions.Resolve [T](IUnityContainer容器,ResolverOverride []重写):\构建\统一\ UnityTemp \编译\统一\统一\ SRC\UnityContainerExtensions.cs:线504\r \ñ在WindowsFormsApplication1.Form1.OnLoad(EventArgs的发送)在d:\ Devzone \任务处理\ WindowsFormsApplication1\Form1.cs中:行33"
在一个表格中:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
try
{
IUnityContainer container = new UnityContainer();
Performance p = container.Resolve<Performance>();
}
catch (Exception ex)
{
}
}
Run Code Online (Sandbox Code Playgroud)
依赖类:
public class Performance
{
public Performance(LogWriter lw, ExceptionManager em)
{
}
}
Run Code Online (Sandbox Code Playgroud)
配置文件:
<?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>
Run Code Online (Sandbox Code Playgroud)
Chr*_*res 11
您需要将Enterprise Library扩展添加到容器中.没有它,容器不会读取配置文件,因此不知道如何创建Entlib对象,如LogWriter.