0

我有一个解析为RequestManager类的IRequestManager类,但我得到 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);
}
4

1 回答 1

0

结果证明这是一个愚蠢的错误......在 MRF.Interfaces.IRequestManager 的 web.config 中的注册节点中,我使用的是命名空间而不是项目名称!

在我更正了 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" />
于 2012-07-21T23:32:28.750 回答