0

我得到了这个例外......

System.ArgumentNullException: Value cannot be null.
Parameter name: typeName
   at HomeInventory2.Services.Factory.GetService(String servicename) in C:\Users\Protego\Documents\Visual Studio 2010\Projects\HomeInventory2\HomeInventory2\Services\Factory.cs:line 37
   at HomeInventory2.Business.Manager.GetService(String name) in C:\Users\Protego\Documents\Visual Studio 2010\Projects\HomeInventory2\HomeInventory2\Business\Manager.cs:line 14
   at HomeInventory2.Business.InventoryMngr.Create(CreateInventory inv) in C:\Users\Protego\Documents\Visual Studio 2010\Projects\HomeInventory2\HomeInventory2\Business\InventoryMngr.cs:line 19
   at HomeInventory2.Form1.submitButton_Click(Object sender, EventArgs e) in C:\Users\Protego\documents\visual studio 2010\Projects\HomeInventory2\HomeInventory2\Form1.cs:line 52
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

源于此代码 -

private void submitButton_Click(object sender, EventArgs e)
        {
            CreateInventory create = new CreateInventory();
            create.ItemAmount = textBoxAmount.Text;
            create.ItemCategory = textBoxCategories.Text;
            create.ItemProperties = textBoxValue.Text;
            create.ItemValue = textBoxValue.Text;

            InventoryMngr invtryMngr = new InventoryMngr();
            invtryMngr.Create(create);

        }

该代码应该以正在创建的文件结束,这些值被保存到文本文件中。显然有些事情正在发生故障 - 我假设在 InventoryMngr 文件中看起来像这样。

namespace HomeInventory2.Business
{
    public class InventoryMngr : Manager
    {
        /// <summary>
        /// persists the inventory information
        /// </summary>
        /// <param name="inv"></param>
        public void Create(CreateInventory inv)
        {
            InventorySvc inventorySvc =
            (InventorySvc)GetService(typeof(InventorySvc).Name);
            inventorySvc.CreateInventory(inv);
        }
    }
}

业务经理得到服务

namespace HomeInventory2.Business
{
    public abstract class Manager
    {
        private Factory factory = Factory.GetInstance();
        protected IService GetService(String name)
        {
            return factory.GetService(name);
        }
    }
}

和工厂 getservice

public IService GetService(string servicename)
{
    Type type;
    Object obj = null;
    try
    {
        type = Type.GetType(GetImplName(servicename));
        obj = Activator.CreateInstance(type);
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception occured: {0}", e);
        throw e;
    }
    return (IService)obj;
}

获取ImplName

private string GetImplName(string servicename)
        {
            NameValueCollection settings =
                ConfigurationManager.AppSettings;
            return settings.Get(servicename);
        }
4

2 回答 2

2

这是你的问题:

NameValueCollection settings = ConfigurationManager.AppSettings;
return settings.Get(servicename);

确保您的 app.config/web.config 具有正确的AppSettings值。 settings.Get(servicename)没有找到servicename.

因此,如果服务名称是“AwesomeService”,您需要在 appSettings 中添加一个元素,例如:

<add key="AwesomeService" value="somevalue"/>

于 2012-10-19T22:40:35.083 回答
-1

我修改你的代码如下:

public void Create(CreateInventory inv)
{
    InventorySvc inventorySvc =
        (InventorySvc)GetService((typeof(InventorySvc)).toString());
    inventorySvc.CreateInventory(inv);
}
于 2012-10-19T22:43:10.493 回答