2012-08-14 4 views
7

मैं रनटाइम पर प्लगइन लोड करने और उनकी कॉन्फ़िगरेशन फ़ाइलों तक पहुंचने का प्रयास कर रहा हूं। कॉन्फ़िगरेशन एलीमेंट कोलेक्शन, कॉन्फ़िगरेशन एलिमेंटेशन और कॉन्फ़िगरेशनसेक्शन से व्युत्पन्न कक्षाओं में उनकी कॉन्फ़िगरेशन फ़ाइलों में कॉन्फ़िगरेशन अनुभाग मैप किए जाते हैं। प्लगइन्स और उनकी कॉन्फ़िगरेशन फ़ाइलें "प्लगइन्स" नामक सबफ़ोल्डर में स्थान हैं।एमईएफ प्लगइन अपनी विन्यास फाइलों के साथ?

समस्या यह है कि मैं प्लगइन कॉन्फ़िगरेशन डेटा लोड नहीं कर सकता और इसे अपने संबंधित वर्गों में सही तरीके से deserialize।

private static System.Configuration.Configuration config = null; 
    public static System.Configuration.Configuration CurrentConfiguration 
    { 
     get 
     { 
      if (config == null) 
      { 
       Assembly assembly = Assembly.GetAssembly(typeof(EmailPlugin)); 
       string directory = Path.GetDirectoryName(assembly.CodeBase); 
       string filename = Path.GetFileName(assembly.CodeBase); 
       string assemblyPath = Path.Combine(directory, filename); 
       config = ConfigurationManager.OpenExeConfiguration(new Uri(assemblyPath).LocalPath); 
      } 

      return config; 
     } 
    } 

यह त्रुटि में परिणाम है:

An error occurred creating the configuration section handler for EmailConfigurationSection: Could not load file or assembly 'EmailPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. 

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="EmailConfigurationSection" type="Foo.Plugins.EmailConfigurationSection, EmailPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true"/> 
    </configSections> 
    <EmailConfigurationSection server="192.168.0.10"> 
    <EmailSettings> 
     <add keyword="ERROR" 
        sender="[email protected]" 
        recipients="[email protected], [email protected]" 
        subject = "Error occurred" 
        body = "An error was detected" 
      /> 
    </EmailSettings> 
    </EmailConfigurationSection> 
</configuration> 

मैं इस इस कोड का उपयोग लोड:

यहाँ प्लगइन EmailPlugin.dll के लिए एक प्लगइन config का एक उदाहरण है

मैंने इसे कॉन्फ़िगरेशन फ़ाइल के शीर्ष पर जोड़ा:

<runtime> 
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <probing privatePath="Plugins"/> 
    </assemblyBinding> 
</runtime> 

तो DLL पाया जाता है, लेकिन जब मैं इसे पुनः प्राप्त करने का प्रयास करें यह उचित वर्ग में ढाला नहीं नहीं करता है:

EmailConfigurationSection defaults = CurrentConfiguration.Sections["EmailConfigurationSection"] as EmailConfigurationSection; 

यह हमेशा अशक्त देता है। मैं जानता हूँ कि यह सही स्थान और विन्यास फाइल पर है क्योंकि मैं इस कोड का उपयोग एक्सएमएल प्राप्त कर सकते हैं के लिए देख रहा है:

var section = CurrentConfiguration.Sections["EmailConfigurationSection"]; 
string configXml = section.SectionInformation.GetRawXml(); 

हालांकि, जब मैं इस कोड के साथ यह deserialize करने की कोशिश:

var serializer = new XmlSerializer(typeof(EmailConfigurationSection)); 
object result; 

EmailConfigurationSection defaults; 
using (TextReader reader = new StringReader(configXml)) 
{ 
    defaults = (EmailConfigurationSection)serializer.Deserialize(reader); 
} 

...

:

There was an error reflecting type 'Foo.Plugins.EmailConfigurationSection'. 

यह InnerException की सामग्री है: मैं एक अपवाद मिल

मैं इसे वर्ग EmailConfigElementCollection की चर्चा करते हुए है, लेकिन उसके बाद संदेश मतलब नहीं है क्योंकि इस वर्ग के एक डिफ़ॉल्ट एक्सेसर है मान: यहां तक ​​कि साथ

public EmailConfigElement this[int index] 
    { 
     get 
     { 
      return (EmailConfigElement)BaseGet(index); 
     } 
     set 
     { 
      if (BaseGet(index) != null) 
      { 
       BaseRemoveAt(index); 
      } 
      BaseAdd(index, value); 
     } 
    } 

मैं अन्य परियोजनाओं में सफलतापूर्वक इस कोड का उपयोग किया है (अलग डीएलएल/कॉन्फ़िगरेशन), लेकिन यह पहली बार है जब मैं इसे एमईएफ के साथ उपयोग करने की कोशिश कर रहा हूं। क्या किसी को पता है कि समस्या क्या है, या एक उपयुक्त कामकाज?

मैं .NET 4.5

+0

मैं कहीं पढ़ा है कि आप बस ExecutingAssebly उपयोग कर सकते हैं:

चाल है कि मैं एक्सएमएल विन्यास की तह तक क्रम टैग ले जाने के लिए किया था।आपके OpenExeConfiguration – Luke

+0

में स्थान [एमईएफ निर्यात असेंबली में कस्टम कॉन्फ़िगरेशन अनुभागों का संभावित डुप्लिकेट] (http://stackoverflow.com/questions/4845801/custom-configuration-sections-in-mef-exporting-assemblies) –

+0

यह इतना नहीं है एक डुप्लिकेट, लेकिन http://social.msdn.microsoft.com/Forums/en-US/16df81ea-270b-47e2-bd30-877e0e32c88c/mef-plugins-with-their-own-configuration-files –

उत्तर

5

उपयोग कर रहा हूँ मैं निम्नलिखित संशोधन के साथ इस तय: Custom configuration sections in MEF exporting assemblies:

public static System.Configuration.Configuration CurrentConfiguration 
{ 
    get 
    { 
     if (config == null) 
     { 
      // Added the next bit 
      AppDomain.CurrentDomain.AssemblyResolve += (o, args) => 
      { 
       var loadedAssemblies = AppDomain.CurrentDomain.GetAssemblies(); 
       return loadedAssemblies.Where(asm => asm.FullName == args.Name) 
              .FirstOrDefault(); 
      }; 

      Assembly assembly = Assembly.GetAssembly(typeof(EmailPlugin)); 
      string directory = Path.GetDirectoryName(assembly.CodeBase); 
      string filename = Path.GetFileName(assembly.CodeBase); 
      string assemblyPath = Path.Combine(directory, filename); 
      config = ConfigurationManager.OpenExeConfiguration(new Uri(assemblyPath).LocalPath); 
     } 

     return config; 
    } 
} 

मैं इस सवाल से मिला है। मैंने वास्तव में सफलता के बिना पहले कोशिश की थी।

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <configSections> 
    <section name="EmailConfigurationSection" type="Foo.Plugins.EmailConfigurationSection, EmailPlugin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" allowDefinition="Everywhere" allowExeDefinition="MachineToApplication" restartOnExternalChanges="true"/> 
    </configSections> 
    <EmailConfigurationSection server="255.255.255.1"> 
    <EmailSettings> 
     <clear /> 
     <add keyword="FOO" 
        sender="[email protected]" 
        recipients="[email protected]" 
        subject = "Foo occurred" 
        body = "Hello" 
      /> 
    </EmailSettings> 
    </EmailConfigurationSection> 
    <runtime> 
     <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> 
     <probing privatePath="Plugins"/> 
     </assemblyBinding> 
    </runtime> 
</configuration> 
+0

आपको कॉन्फ़िगरेशन फ़ाइल में रनटाइम अनुभाग की भी आवश्यकता नहीं है। –