尝试让 C# 依赖注入示例正常工作
我从以下位置下载了此代码: 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);
这是堆栈跟踪:
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>
I downloaded this code from: http://msdn.microsoft.com/en-us/magazine/cc164657.aspx but its VS 2003. I'm trying to get it to work in VS 2010. There seems to have been some changes. For example, I needed to instantiate a resource object in 2010. Here is the main code:
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();
}
}
}
It always crashes on this line:
XmlObjectFactory xmlObjectFactory = new XmlObjectFactory(resource);
Here is 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:
Any help would be appreciated.
Note the config file is:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object name="DomainObjectImplementationClass"
singleton="false"
type="ImplementationClass1, SpringDIExample" />
</objects>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试
type="SpringDIExample.ImplementationClass1, SpringDIExample"
(带有 命名空间 的类名(如果不是SpringDIExample,请输入正确的名称) )
Try
type="SpringDIExample.ImplementationClass1, SpringDIExample"
(class name with the namespace (put the correct one, if it's notSpringDIExample
)它告诉您在指定的程序集中找不到 ImplementClass1。
检查该类是否确实存在、拼写、命名空间和程序集名称。
It tells you that it cannot find ImplementationClass1 in your assembly specified.
Check if that class actually exists, spelling, namespace and name of assembly.
您可能想查看 Spring.NET 新的基于代码的配置模型(称为 CodeConfig),作为 XML 的替代方案。如果您使用 XML,还有一个 VS.NET 2010 扩展可以捕获所有这些拼写错误,并为您提供类名和属性的补全。
主页有链接 - http://www.springframework.net/
期刊 http://www.opensource-central.com/ 有一篇关于 CodeConfig 的文章。
标记
You might like to check out Spring.NET new code based configuration model, called CodeConfig, as an alternative to XML. If you use XML there is also a VS.NET 2010 extension that will catch all those typos and also give you completion for class names and properties.
The main page has links to it - http://www.springframework.net/
The journal http://www.opensource-central.com/ has an article on CodeConfig.
Mark