2012-10-18 19 views
6

inşa edilemez DI Birliğini çözümlerken Ben çok rahatsız edici ve açıklanamaz hatası alıyorum.Birlik: tip LogWriter aşağıdaki "proje" için

InvalidOperationException - LogWriter türü yapılamaz. Bu değeri sağlamak için kabı yapılandırmanız gerekir.

ex.Message; "Bağımlılık çözümlemesi başarısız oldu, type = \" WindowsFormsApplication1.Performance \ ", name = \" (none) \ ". \ R \ nException şu sırada oluştu: çözümlenirken. \ R \ nException : InvalidOperationException - Tür LogWriter Bu değerini tedarik etmek konteyner yapılandırmanız gerekir. inşa olamaz. \ r \ n ----------------------------- ------------------ \ r \ hizmeti NAT istisna zaman, kap oldu: \ r \ n \ r \ n çözme WindowsFormsApplication1.Performance, (yok) \ r \ n kurucu WindowsFormsApplication1.Performance (Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter LW "lw \ \" parametresi karar verilmesi, Microsoft.Practices.EnterpriseLibrary.Exceptio nHandling.ExceptionManager em) \ r \ n Microsoft.Practices.EnterpriseLibrary.Logging.LogWriter çözülmesi, (yok) \ r \ n"

ex.StackTrace; " Microsoft.Practices.Unity.UnityContainer.DoBuildUp de e (Tip 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, IEnumerable 1 resolverOverrides, mevcut , dize adını Nesne): \ \ Birlik \ UnityTemp \ Derleme \ Birlik \ Birlik \ Src \ UnityContainer.cs oluşturur : satır 485 \ r \ n Microsoft.Practices.Unity.UnityContainer.Resolve e (Tip t, Yaylı adı ResolverOverride [] resolverOverrides) de: \ \ Unity \ UnityTemp \ Derleme \ Birlik \ Birlik \ Src oluşturur \ UnityContainer.cs: satır Microsoft.Practices.Unity.UnityContainerExtensions.Resolve [T] 'de 173 \ r \ n (IUnityContainer kap, ResolverOverride [] geçersiz kılar) e: \ \ Unity \ UnityTemp \ Derleme \ Birlik \ oluşturur Birlik \ Src \ UnityContainerExtensions.cs: satır \ Devzone \ Görevlendirme \ WindowsFormsApplication1 \ Form1.cs: D WindowsFormsApplication1.Form1.OnLoad (EventArgs e) de 504 \ r \ n

protected override void OnLoad(EventArgs e) 
     { 
      base.OnLoad(e); 

      try 
      { 
       IUnityContainer container = new UnityContainer(); 
       Performance p = container.Resolve<Performance>(); 
      } 
      catch (Exception ex) 
      { 

      } 
     } 

: bir formda hat 33"

Bağımlılık sınıfı:

public class Performance 
    { 
     public Performance(LogWriter lw, ExceptionManager em) 
     { 
     } 
    } 

Konfigürasyon Dosyası:

<?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}&#xA;Message: {message}{newline}&#xA;Category: {category}{newline}&#xA;Priority: {priority}{newline}&#xA;EventId: {eventid}{newline}&#xA;Severity: {severity}{newline}&#xA;Title:{title}{newline}&#xA;Machine: {localMachine}{newline}&#xA;App Domain: {localAppDomain}{newline}&#xA;ProcessId: {localProcessId}{newline}&#xA;Process Name: {localProcessName}{newline}&#xA;Thread Name: {threadName}{newline}&#xA;Win32 ThreadId:{win32ThreadId}{newline}&#xA;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 &amp; 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> 

cevap

10

Sen Enterprise Library uzantısı eklemek gerekir senin kapsayıcın. Bu olmadan, kapsayıcı yapılandırma dosyasını okumaz ve dolayısıyla LogWriter gibi Entlib nesnelerinin nasıl oluşturulacağını bilmez.

+0

Arhhhhhhhhh !!! Muhteşem. Teşekkürler dostum. – rism

+1

Başkası için yapmanız gereken tek şey bu: var container = new UnityContainer(). AddNewExtension (); – rism

+0

Bunu anlamak biraz zamanımı aldı, ama bu cevap çok yardımcı oldu. Birisi bir unity.config dosyasında bildirmek isterse, bunun nasıl yapılması gerektiği: Konrad

0

Unity'nin Performans sınıfınızı oluşturması için, ILogWriter uygulamasının nasıl oluşturulacağını bilmesi gerekir.

Sana ILogWriter arabirimi için oluşturmak için hangi sınıf Unity anlatmak kodunuzda herhangi bir yerde göremiyorum, bu yüzden bu eklemeniz gerekebilir şüpheli.

+0

yapıcısında. – Tallmaris

+0

Bu doğru DI, bu yüzden performans sınıfının kurucusunda bir LogWriter (ILogWriter somut bir uygulama) için soruyorum? – rism

+0

Evet, gerçekten öyle hayal etmiş olmalısın. Peki, LogWriter için parametresiz bir kurucu var mı? –

0

LogWriter, tüm beton türleri parametreleri olan boş bir kurucu veya yapıcıya sahip değil: LogWriter Constructor.

Yani Birlik o binada başarısız olur ve onun söylediği gibi, bir uygulama sağlamak için konteyner yapılandırarak yardımına ihtiyacım olacak.buna da tek constructor, parametresiz :)

+0

Hmm dediğiniz şey mantıklı, ama ben kendimi, Ent Lib bloklarından türler üzerinde kurucularla ilgili hatırlamıyorum. – rism

0

beri

teyidi olarak

, ExceptionManager muhtemelen Tamam çözülecektir bu deneyin:

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) 
      { 

      } 
     }