带有wcf和Entityframework的Structuremap

时间:2013-12-20 08:55:42

标签: wcf entity-framework asp.net-mvc-4 structuremap

我正在创建一个mvc4应用程序。解决方案结构如下:enter image description here

此处DAO层在Data.EntityFramework中实现.Service层正在为托管服务创建服务和servicehost .CORE层用于所有分层注册。

enter image description here

数据实体层是:

enter image description here

我从服务层调用UnitOfWork,如下所示

public class CandidateService : ICandidateService
        {
            private readonly IUnitOfWork _unitOfWork;

            public CandidateService(IUnitOfWork unitOfWork)
            {
                this._unitOfWork = unitOfWork;
            }

            //public IList<VW_CANDBASICSEARCH> GetAll()
            //{
            //    try
            //    {
            //        var lstCandidate = _unitOfWork.Repository<VW_CANDBASICSEARCH>().All();
            //        var list = lstCandidate.ToList<VW_CANDBASICSEARCH>();
            //        return list;
            //    }
            //    catch (Exception er)
            //    {
            //        throw er;
            //    }
            //}

            public IList<VW_CANDBASICSEARCH> GetSearchCandidate(string strName, string strLocation, string strProfession, string strSkill)
            {
                var lstCandidate = _unitOfWork.Repository<VW_CANDBASICSEARCH>().All().ToList<VW_CANDBASICSEARCH>();
                var lstSearchCan = from can in lstCandidate
                                   where ((strName == null || strName.Length == 0 || (can.CAND_NAME).ToUpper().Contains(strName.ToUpper()))
                                            && (strLocation == null || strLocation.Length == 0 || can.CAND_LOCATION.Equals(strLocation))
                                            && (strProfession == null || strProfession.Length == 0 || can.CAND_PROFESSION.Equals(strProfession))
                                            && (strSkill == null || strSkill.Length == 0 || can.CAND_SKILL.Equals(strProfession)))
                                   select can;
                return lstSearchCan.ToList<VW_CANDBASICSEARCH>();
            }


            public TBLCANDIDATE_HEADER CreateCandidate(TBLCANDIDATE_HEADER Candidate)
            {
                Candidate.ObjectState = ObjectState.Added;
                _unitOfWork.Repository<TBLCANDIDATE_HEADER>().Insert(Candidate);
                return Candidate;

            }
        }

在CORE层,我正在注册数据和业务,如下所示;

public class BusinessLogicServiceModule : Registry
        {


            public BusinessLogicServiceModule()
            {
                For(typeof(IHrmsRepository<>)).Use(typeof(Repository<>));
                For<IUnitOfWork>().Use<UnitOfWork>();


            }
        }

在服务主机中,我正在实施以下http://lostechies.com/jimmybogard/2008/07/30/integrating-structuremap-with-wcf/ 在IHRMS.WEB中,我在Global.asax.cs Application_start中编写以下内容

 protected void Application_Start()
            {
                AreaRegistration.RegisterAllAreas();

                 WebApiConfig.Register(GlobalConfiguration.Configuration);
                FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
                RouteConfig.RegisterRoutes(RouteTable.Routes);


                ObjectFactory.Configure(x => x.AddRegistry(new BusinessLogicServiceModule()));

                ObjectFactory.Configure(x => x.AddRegistry(new ControllerDependency()));
                ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory());

                BundleConfig.RegisterBundles(BundleTable.Bundles);

                AuthConfig.RegisterAuth();
            }

但是当我运行它时会出现以下错误: StructureMap异常代码:202 没有为PluginFamily定义的默认实例IHRMS.DAO.Infrastructure.IUnitOfWork,IHRMS.DAO,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null

我使用Structuremap作为DI。 有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您的配置对我来说很好。

我通常通过执行以下操作来解决这些不明确的结构图错误:

  • 验证您注入的课程有:
    • 公共建设者
    • 构造参数依赖项也已在注册表中定义
  • 逐步完成“构建”工作流程
    • 确保在为
    • 调用依赖项之前定义它们
  • 确保ObjectFactory上的AssertConfigurationIsValid和WhatDoIHave方法可视地确认您具有有效的依赖关系配置

大多数问题在工作流验证步骤中得到解决,如果在'after'之后配置了依赖关系,则表示structmap会抛出202错误。

此外只是确认IUnitOfWork被构造和IUnitOfWork依赖性是同一对象右(IE:它们是相同的命名空间和什么不)?

抱歉,目前我没有答案,但如果您有更多信息,我很乐意为您提供帮助。

谢谢, WM