Loading external web.config variables when invoking a method in an external assembly
I am using reflection to invoke a method in an ext开发者_如何学JAVAernal 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.
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
精彩评论