试图让C#Dependency Injection示例工作

时间:2011-06-10 00:44:30

标签: c# spring.net

我从以下代码下载了此代码:http://msdn.microsoft.com/en-us/magazine/cc164657.aspx但它的VS 2003.我试图让它在VS 2010中运行。似乎有一些变化。例如,我需要在2010年实例化一个资源对象。这是主要代码:

using System;
using System.IO;
using Spring.Objects.Factory.Xml;
using SpringDIExample;
namespace runner
{
class Class1
{

    [STAThread]
    static void Main()
    {
        // 1. Open the configuration file and create a new
        //    factory, reading in the object definitions
        using (Stream stream = File.OpenRead("config.xml"))
        {
            // 2. Create a new object factory
            Spring.Core.IO.InputStreamResource resource = new Spring.Core.IO.InputStreamResource(stream, "config");
            XmlObjectFactory xmlObjectFactory = new XmlObjectFactory(resource);

            // 3. Call my factory class with generic label for the object
            //    that is requested. 
            IDomainObjectInterface domainObjectInterface = (IDomainObjectInterface)xmlObjectFactory.GetObject("DomainObjectImplementationClass");

            // 4. Use the object just like any other concrete class.
            Console.WriteLine("My name is " + domainObjectInterface.Name);
        }



        Console.ReadLine(); 
    }
}
 }

它总是在这一行崩溃:

XmlObjectFactory xmlObjectFactory = new XmlObjectFactory(resource);

这是Stack Trace:

   Spring.Objects.Factory.ObjectDefinitionStoreException was unhandled
  Message=Unexpected exception parsing XML document from config
  Source=Spring.Core
  ObjectName=""
  ResourceDescription=""
  StackTrace:
       at Spring.Objects.Factory.Xml.XmlObjectDefinitionReader.DoLoadObjectDefinitions(Stream stream, IResource resource)
       at Spring.Objects.Factory.Xml.XmlObjectDefinitionReader.LoadObjectDefinitions(IResource resource)
       at Spring.Objects.Factory.Xml.XmlObjectFactory..ctor(IResource resource, Boolean caseSensitive, IObjectFactory parentFactory)
       at Spring.Objects.Factory.Xml.XmlObjectFactory..ctor(IResource resource)
       at runner.Class1.Main() in C:\Documents and Settings\dhood\Desktop\SpringExample\TestRunner\ConsoleRunner.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: Spring.Objects.Factory.ObjectDefinitionStoreException
       Message=Failed parsing object definition '<object name="DomainObjectImplementationClass" singleton="false" type="ImplementationClass1, SpringDIExample" xmlns="http://www.springframework.net" />'
       Source=Spring.Core
       ObjectName=""
       ResourceDescription=""
       StackTrace:
            at Spring.Objects.Factory.Xml.DefaultXmlObjectDefinitionParser.RegisterObjectDefinition(XmlElement element, ParserContext parserContext)
            at Spring.Objects.Factory.Xml.DefaultXmlObjectDefinitionParser.ParseElement(XmlElement element, ParserContext parserContext)
            at Spring.Objects.Factory.Xml.DefaultObjectDefinitionDocumentReader.ParseObjectDefinitions(XmlElement root, ObjectDefinitionParserHelper helper)
            at Spring.Objects.Factory.Xml.DefaultObjectDefinitionDocumentReader.RegisterObjectDefinitions(XmlDocument doc, XmlReaderContext readerContext)
            at Spring.Objects.Factory.Xml.XmlObjectDefinitionReader.RegisterObjectDefinitions(XmlDocument doc, IResource resource)
            at Spring.Objects.Factory.Xml.XmlObjectDefinitionReader.DoLoadObjectDefinitions(Stream stream, IResource resource)
       InnerException: Spring.Objects.Factory.ObjectDefinitionStoreException
            Message=Error registering object with name 'DomainObjectImplementationClass' defined in 'config' : Object class [ImplementationClass1, SpringDIExample] not found.
<object name="DomainObjectImplementationClass" singleton="false" type="ImplementationClass1, SpringDIExample" xmlns="http://www.springframework.net" />
            Source=Spring.Core
            ObjectName=DomainObjectImplementationClass
            ResourceDescription=config
            StackTrace:
                 at Spring.Objects.Factory.Xml.XmlReaderContext.ReportException(XmlNode node, String name, String message, Exception cause)
                 at Spring.Objects.Factory.Xml.DefaultXmlObjectDefinitionParser.ParseObjectDefinition(XmlElement element, String id, ObjectDefinitionParserHelper parserHelper)
                 at Spring.Objects.Factory.Xml.DefaultXmlObjectDefinitionParser.ParseObjectDefinition(XmlElement element, ParserContext parserContext)
                 at Spring.Objects.Factory.Xml.DefaultXmlObjectDefinitionParser.RegisterObjectDefinition(XmlElement element, ParserContext parserContext)
            InnerException: System.TypeLoadException
                 Message=Could not load type from string value 'ImplementationClass1, SpringDIExample'.
                 Source=Spring.Core
                 TypeName=""
                 StackTrace:
                      at Spring.Util.TypeResolver.ResolveType(String typeName)
                      at Spring.Util.TypeResolver.Resolve(String typeName)
                      at Spring.Util.CachedTypeResolver.Resolve(String typeName)
                      at Spring.Objects.ObjectUtils.ResolveType(String typeName)
                      at Spring.Objects.Factory.Support.DefaultObjectDefinitionFactory.CreateObjectDefinition(String typeName, String parent, AppDomain domain)
                      at Spring.Objects.Factory.Xml.DefaultXmlObjectDefinitionParser.ParseObjectDefinition(XmlElement element, String id, ObjectDefinitionParserHelper parserHelper)
                 InnerException: System.TypeLoadException
                      Message=Could not load type 'ImplementationClass1' from assembly 'SpringDIExample, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
                      Source=mscorlib
                      TypeName=ImplementationClass1
                      StackTrace:
                           at System.Reflection.RuntimeAssembly.GetType(RuntimeAssembly assembly, String name, Boolean throwOnError, Boolean ignoreCase, ObjectHandleOnStack type)
                           at System.Reflection.RuntimeAssembly.GetType(String name, Boolean throwOnError, Boolean ignoreCase)
                           at Spring.Util.TypeResolver.LoadTypeDirectlyFromAssembly(TypeAssemblyInfo typeInfo)
                           at Spring.Util.TypeResolver.ResolveType(String typeName)
                      InnerException: 

任何帮助都将不胜感激。

请注意配置文件是:

<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
    <object name="DomainObjectImplementationClass" 
            singleton="false" 
            type="ImplementationClass1, SpringDIExample" />
</objects>

3 个答案:

答案 0 :(得分:6)

尝试使用type="SpringDIExample.ImplementationClass1, SpringDIExample"(带有 名称空间 的类名称(如果不是SpringDIExample,请输入正确的名称)

答案 1 :(得分:2)

它告诉您它在指定的程序集中找不到ImplementationClass1。

检查该类是否实际存在,拼写,命名空间和程序集名称。

答案 2 :(得分:1)

您可能希望查看基于Spring.NET新代码的配置模型,称为CodeConfig,作为XML的替代方案。如果您使用XML,还有一个VS.NET 2010扩展,它将捕获所有这些拼写错误,并为您提供类名和属性的完成。

主页面包含指向它的链接 - http://www.springframework.net/

期刊http://www.opensource-central.com/有一篇关于CodeConfig的文章。

标记