使用Unity.Mvc5通过MVC控制器中的ChannelFactory调用WCF服务

时间:2017-01-31 21:38:43

标签: asp.net-mvc wcf unity-container

我有以下项目:

  • Poco(列出所有潜在对象/ dtos)
  • BusinessServices(包含所有逻辑并与数据库通信)
  • WcfServices(调用业务服务并返回Pocos)
  • MVC5(调用WcfServices)

现在,我试图在不使用代理的情况下调用WcfServices,但是使用ChannelFactory并通过Unity初始化它,我收到错误:“没有为此对象定义无参数构造函数”。请帮忙。

UnityConfig.cs

public static void RegisterComponents()
{
    var container = new UnityContainer();

    // Register WCF
    container.RegisterType<IPersonService>(
        new ContainerControlledLifetimeManager(),
        new InjectionFactory((c) => new ChannelFactory<IPersonService>("WSHttpBinding_IPersonService").CreateChannel()));

    DependencyResolver.SetResolver(new UnityDependencyResolver(container));
}

PersonController.cs

public class PersonController : Controller
{
    private readonly IPersonService _personService;

    public PersonController(IPersonService personService)
    {
        _personService = personService;
    }
    ...
    ...
    ...
}

0 个答案:

没有答案