Unity 2.0:如何解决构造函数依赖项

时间:2012-07-21 22:57:16

标签: c# unity-container

我有一个 IRequestManager 类,它解析为 RequestManager 类,但我收到System.IO.FileLoadException未被用户代码Message =给定的程序集名称或代码库无效。 (来自HRESULT的异常:0x80131047)

public class RequestManager : IRequestManager
{
    private IRepository<Request> _requestRepository;
    public RequestManager(IRepository<Request> requestRepository)
    {
        _requestRepository = requestRepository;
    }
    // ...
}

我在web.config中注册IRequestManager时遇到问题;我是否需要在web.config中创建显式注册IRepository?

这是我的web.config

<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias alias="PerOperationContext" type="UnityWcfExtensions.UnityOperationContextLifetimeManager, UnityWcfExtensions" />
    <container>
      <register type="MRF.Repository.IDbContext, MRF.Repository" mapTo="MRF.Repository.EntityFrameworkCodeFirst.MRFDbContext, MRF.Repository.EntityFrameworkCodeFirst">
        <lifetime type="PerOperationContext" />
        <constructor>
          <param name="nameOrConnectionString" value="MRFContext" type="string" />
        </constructor>
      </register>

      <register type="MRF.Repository.IUnitOfWorkFactory, MRF.Repository" mapTo="MRF.Repository.EntityFrameworkCodeFirst.MRFDbContext, MRF.Repository.EntityFrameworkCodeFirst">
        <lifetime type="PerOperationContext" />
        <constructor>
          <param name="nameOrConnectionString" value="MRFContext" type="string" />
        </constructor>
      </register>

      <register type="MRF.Repository.IRepository`1, MRF.Repository" mapTo="MRF.Repository.EntityFrameworkCodeFirst.GenericRepository`1, MRF.Repository.EntityFrameworkCodeFirst" />
      <register type="MRF.Business.Interfaces.IRequestManager, MRF.Business.Interfaces" mapTo="MRF.Business.RequestManager, MRF.Business">
        <constructor>
          <param name="requestRepository" type="MRF.Repository.IRepository`1[[MRF.Entities.Request, MRF.Entities]]">
            <dependency />
          </param>
        </constructor>

      </register>
      <register type="MRF.Mapping.IMapper`2[[MRF.Entities.Region, MRF.Entities],[MRF.Service.DataContract.Region, MRF.Service.DataContract]], MRF.Mapping" mapTo="MRF.Mapping.AutoMapper.RegionMapper, MRF.Mapping.AutoMapper" />
      <register type="MRF.Mapping.IMapper`2[[MRF.Entities.User, MRF.Entities],[MRF.Service.DataContract.User, MRF.Service.DataContract]], MRF.Mapping" mapTo="MRF.Mapping.AutoMapper.UserMapper, MRF.Mapping.AutoMapper" />
      <register type="MRF.Mapping.IMapper`2[[MRF.Entities.UserPreference, MRF.Entities],[MRF.Service.DataContract.UserPreference, MRF.Service.DataContract]], MRF.Mapping" mapTo="MRF.Mapping.AutoMapper.UserPreferenceMapper, MRF.Mapping.AutoMapper" />
      <register type="MRF.Mapping.IMapper`2[[MRF.Entities.Status, MRF.Entities],[MRF.Service.DataContract.Status, MRF.Service.DataContract]], MRF.Mapping" mapTo="MRF.Mapping.AutoMapper.StatusMapper, MRF.Mapping.AutoMapper" />
      <register type="MRF.Mapping.IMapper`2[[MRF.Entities.Request, MRF.Entities],[MRF.Service.DataContract.Request, MRF.Service.DataContract]], MRF.Mapping" mapTo="MRF.Mapping.AutoMapper.RequestMapper, MRF.Mapping.AutoMapper" />
      <register type="MRF.Mapping.IMapper`2[[MRF.Entities.Series, MRF.Entities],[MRF.Service.DataContract.Series, MRF.Service.DataContract]], MRF.Mapping" mapTo="MRF.Mapping.AutoMapper.SeriesMapper, MRF.Mapping.AutoMapper" />
    </container>
  </unity>

我目前的工作是在我的WCF服务中手动创建一个新的RequestManager实例,并传递已解析的构造函数参数,如下所示:

public DC.Request GetRequest(int requestId)
{
    var requestRepository = this.Container.Resolve<IRepository<BE.Request>>();
    var requestManager = new RequestManager(requestRepository);
    var request = requestManager.GetRequest(requestId);
    var userMapper = this.Container.Resolve<IMapper<BE.User, DC.User>>();
    var seriesMapper = this.Container.Resolve<IMapper<BE.Series, DC.Series>>();
    var statusMappger = this.Container.Resolve<IMapper<BE.Status, DC.Status>>();
    var mapper = this.Container.Resolve<IMapper<BE.Request, DC.Request>>();
    return mapper.Map(request);
}

1 个答案:

答案 0 :(得分:0)

原来这是一个愚蠢的错误...... 在MRF.Interfaces.IRequestManager的web.config中的register节点中,我使用了命名空间而不是项目名称!

在我更正了web.config后,我没有添加任何额外的配置,以下代码工作正常!

public DC.Request GetRequest(int requestId)
{
    var requestManager = this.Container.Resolve<IRequestManager>();
    var request = requestManager.GetRequest(requestId);
    var userMapper = this.Container.Resolve<IMapper<BE.User, DC.User>>();
    var seriesMapper = this.Container.Resolve<IMapper<BE.Series, DC.Series>>();
    var statusMappger = this.Container.Resolve<IMapper<BE.Status, DC.Status>>();
    var mapper = this.Container.Resolve<IMapper<BE.Request, DC.Request>>();
    return mapper.Map(request);
}

所以,而不是

<register type="MRF.Business.Interfaces.IRequestManager, MRF.Business.Interfaces" mapTo="MRF.Business.RequestManager, MRF.Business" />

我必须做以下事情:

<register type="MRF.Business.Interfaces.IRequestManager, MRF.Business" mapTo="MRF.Business.RequestManager, MRF.Business" />