Unity(在PRISM中)确实(不)奇怪的解决...... ?? !!

时间:2009-12-04 01:12:34

标签: unity-container prism ioc-container

我正在使用PRISM,在Bootstrapper类中,我确实覆盖了ConfigureContainer()方法。只有这些方面没有什么花哨的东西:

protected override void ConfigureContainer()
{       
    Container.RegisterType<IDataContext, SQLDataContext>(new InjectionConstructor(@"Server=localhost\SQLExpress;User Id=sa;Password=xxxxx;Database=MyDatabase"));
        base.ConfigureContainer();
}

在“debug-time”我尝试调用Container.Resolve()但这会给我以下错误:

  

{“依赖项的解析失败,类型= \”Photo.DAL.Abstract.IDataContext \“,name = \”\“。异常消息是:当前构建操作(构建密钥构建密钥[Photo.DAL。 Concrete.SQLDataContext,null])失败:值不能为空。\ r \ nParameter name:stream(策略类型BuildPlanStrategy,索引3)“} System.Exception {Microsoft.Practices.Unity.ResolutionFailedException}

但是当我做的时候

Container.IsTypeRegistered(typeof(IDataContext))

我明白了!!!

  • 我缺少什么???

以下作为答案发布的信息,应该是对该问题的修改:

这是完整的堆栈:

Microsoft.Practices.Unity.ResolutionFailedException: Resolution of the dependency failed, type = "Photo.DAL.Abstract.IDataContext", name = "". Exception message is: The current build operation (build key Build Key[Photo.DAL.Concrete.SQLDataContext, null]) failed: Value cannot be null.
Parameter name: stream (Strategy type BuildPlanStrategy, index 3) ---> Microsoft.Practices.ObjectBuilder2.BuildFailedException: The current build operation (build key Build Key[Photo.DAL.Concrete.SQLDataContext, null]) failed: Value cannot be null.
Parameter name: stream (Strategy type BuildPlanStrategy, index 3) ---> System.ArgumentNullException: Value cannot be null.
Parameter name: stream
   at System.Data.Linq.Mapping.XmlMappingSource.FromStream(Stream stream)
   at Photo.DAL.Mapping.GetMapping() in C:\Users\Savvas\Documents\Visual Studio 2008\Projects\Photo\Photo.DAL\Mapping.cs:line 18
   at Photo.DAL.Concrete.SQLDataContext..ctor(String connectionString) in C:\Users\Savvas\Documents\Visual Studio 2008\Projects\Photo\Photo.DAL\Concrete\SQLDataContext.cs:line 52
   at BuildUp_Photo.DAL.Concrete.SQLDataContext(IBuilderContext )
   at Microsoft.Practices.ObjectBuilder2.DynamicMethodBuildPlan.BuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.BuildPlanStrategy.PreBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.ObjectBuilder2.StrategyChain.ExecuteBuildUp(IBuilderContext context)
   at Microsoft.Practices.ObjectBuilder2.Builder.BuildUp(IReadWriteLocator locator, ILifetimeContainer lifetime, IPolicyList policies, IStrategyChain strategies, Object buildKey, Object existing)
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
   --- End of inner exception stack trace ---
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, Object existing, String name)
   at Microsoft.Practices.Unity.UnityContainer.DoBuildUp(Type t, String name)
   at Microsoft.Practices.Unity.UnityContainer.Resolve(Type t, String name)
   at Microsoft.Practices.Unity.UnityContainerBase.Resolve(Type t)
   at Microsoft.Practices.Unity.UnityContainerBase.Resolve[T]()
   at Photo.Desktop.Bootstrapper.ConfigureContainer() in C:\Users\Savvas\Documents\Visual Studio 2008\Projects\Photo\Photo.Desktop\Bootstrapper.cs:line 42

我注意到错误不是来自实际解析类,而是来自调用方法GetMapping(),定义为

public static class Mapping
    {
        public static XmlMappingSource GetMapping()
        {
            XmlMappingSource mapping;
            using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Photo.DAL.Entities.Entities.map"))
            {
                mapping = XmlMappingSource.FromStream(stream);
            }
            return mapping;
        }
    }
  • Unity无法做到这一点吗? (它与温莎合作得很好!!)

1 个答案:

答案 0 :(得分:0)

这可能是两件事之一......从您的例外情况来看,并不清楚......

  1. 可能还有另一种内心 可能被屏蔽的异常?

  2. 否则可能就是你的 SqlDataContext类有一个 接受流的构造函数?

  3. 同样,异常还不够清楚(至少对我而言)。如果这些建议都没有帮助,你可以在你的问题中发布$ exception.ToString()的完整输出吗?

    编辑: 根据您的完整堆栈跟踪,代码行失败就是这一行:

    using (Stream stream = Assembly.
                           GetExecutingAssembly().
                           GetManifestResourceStream("Photo.DAL.Entities.Entities.map"))
    {
        //This line is failing with null argument
        mapping = XmlMappingSource.FromStream(stream);
    }
    

    您的资源流返回null,表示您的资源不存在或无法加载程序集(有时这指的是附属程序集)。我会先怀疑前者...检查以确保.map文件中的Build设置正确。