在外部程序集中调用方法时加载外部 web.config 变量
我正在使用反射来调用外部程序集中的方法。外部类/方法位于 WCF 数据服务中。
WCF 数据服务使用从 web.config 中的自定义配置部分加载的信息,
<configSections>
<section name="myCustomSection" type="MyWcfService.MyCustomSection, MyWcfService" />
</configSections>
加载配置变量在 wcf 服务中工作正常,但在尝试通过单独的应用程序通过反射调用其方法时则不然。我尝试将配置信息放入本地 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 中查找,则在本地添加相同的配置信息应该可行。
谢谢。
I am using reflection to invoke a method in an external assembly. The external class / method is in a WCF data service.
The WCF data service uses information loaded from a custom configuration section in the web.config,
<configSections>
<section name="myCustomSection" type="MyWcfService.MyCustomSection, MyWcfService" />
</configSections>
Loading the configuration variables works fine in the wcf service but not when trying to invoke its methods through reflection via a seperate app. I tried putting the configuration information in the local app.config but I get the same error.
This is the code in the local application:
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);
}
}
And this is the code from the external assembly (wcf service) that is producing the error,
MyCustomSection configSection = ConfigurationManager.GetSection("myCustomSection")
as MyCustomSection ;
configSection is coming up null - 'object reference not set to an instance of an object'.
If its looking in the local applications app.config rather than the web.config, adding the same config information locally should work, I would think.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要注册 AppDomain.CurrentDomain.TypeResolve 事件。此链接有一个如何使用它的示例。
http://msdn.microsoft.com/en-us/library /system.appdomain.typeresolve.aspx
You may need to register with the AppDomain.CurrentDomain.TypeResolve event. This link has an example of how to use it.
http://msdn.microsoft.com/en-us/library/system.appdomain.typeresolve.aspx