2011-04-17 79 views
0

我正在使用反射來調用外部程序集中的方法。外部類/方法在WCF數據服務中。嘗試通過通過反射來調用它的方法時在外部程序集中調用方法時加載外部web.config變量

的WCF數據服務使用

<configSections> 
    <section name="myCustomSection" type="MyWcfService.MyCustomSection, MyWcfService" /> 
</configSections> 

加載配置變量在WCF服務工作正常,但不是在web.config中的自定義配置部分加載信息,一個單獨的應用程序。我試着把配置信息放在本地的app.config中,但是我得到了同樣的錯誤。

這是在本地應用程序的代碼:

Assembly assembly = Assembly.LoadFile 
      ("C:\\MyProject\\MyWcfService.dll"); 

     Type[] t = assembly.GetTypes(); 

     foreach (var v in t) 
     { 
      if (v.Name == "MyType") 
      { 
       var instance = Activator.CreateInstance(v); 
       v.InvokeMember("MyMethod", BindingFlags.InvokeMethod, null, instance, null); 
      } 
     } 

這是從外部組件(WCF服務),其產生錯誤的代碼,

MyCustomSection configSection = ConfigurationManager.GetSection("myCustomSection") 
      as MyCustomSection ; 

configSection快到了null - '未將對象引用設置爲對象的實例'。

如果它在本地應用程序app.config而不是web.config中查找,那麼在本地添加相同的配置信息應該可以工作,我想。

謝謝。

回答