So here's the deal. I have my solution in which there are several projects:
- A wrapper project is just a console application that currently runs on a Windows service during debugging.
- Working draft - this contains guts of code. This way I can easily debug code for a Windows service without a headache.
- Plugin library project - contains the factory plugin for a new concrete instance of the plugin.
- Plugin project - contains a specific implementation of my plugin.
My wrapper application contains the app.config file, which my plugin should reference directly to its own configuration. Thus, my wrapper application does not need to know anything except how it needs to call the factory adapter for a new instance of the plugin.
<configuration>
<appSettings>
<add key="Plugin" value="Prototypes.BensPlugin" />
<add key="Prototypes.BensPlugin.ServiceAddress" value="http://localhost/WebServices/Plugin.asmx" />
<add key="Prototypes.BensPlugin.Username" value="TestUserName" />
<add key="Prototypes.BensPlugin.Password" value="TestPassword" />
</appSettings>
</configuration>
Project Wrapper:
using Worker;
public class Program
{
public static void Main()
{
var serviceProc = new ServiceProcess();
serviceProc.DoYourStuff();
}
}
Working draft:
using PluginLibrary;
namespace Worker
{
public class ServiceProcess
{
public string GetRequiredAppSetting(string settingName)
{
}
public void DoYourStuff()
{
string pluginTypeName = GetRequiredAppSetting("Plugin");
Type type = Type.GetType(pluginTypeName);
if (type != null)
{
IPlugin plugin = PluginFactory.CreatePlugin(type);
if (plugin != null)
plugin.DoWork();
}
}
}
}
Plugin Library:
namespace PluginLibrary
{
public interface IPlugin
{
void DoWork();
}
public class PluginFactory
{
public IPlugin CreatePlugin(Type pluginType)
{
return (IPlugin)Activator.CreateInstance(pluginType);
}
}
}
Plugin:
using PluginLibrary;
namespace Prototypes
{
public class BensPlugin : IPlugin
{
public string ServiceAddress { get; protected set; }
public string username { get; protected set; }
public string password { get; protected set; }
public void DoWork()
{
Trace.WriteLine("Ben plugin is working.");
}
public BensPlugin()
{
}
}
}
Good, so it sets the scene. Where I will stick up in my working draft when I refer to Type.GetType(pluginTypeName). My pluginTypeNameis retrieved from the configuration correctly, but Type.GetType(pluginTypeName)returns null. I tried updating the instance of my plugin right in the same place in the code:
var obj = new BensPlugin();
This works fine, but obj.GetType().ToString()returns the same string as in app.config.
Can someone tell me why I can create a specific object at this point in the code, but Type.GetType(pluginTypeName)failed?