0

是否可以使用 MEF 编写 WCF RIA 域服务?

假设我在域服务中有以下属性:

[Import(RequiredCreationPolicy = CreationPolicy.Shared)]
public TestClass Dependency{ get; set; }

和TestClass..

[Export]
public class TestClass
{
    public void Do()
    {

    }
}

我有一个 DomainServiceFactory 类:

public class DomainServiceFactory : IDomainServiceFactory
{
    private readonly ComposablePartCatalog _catalog;
    private readonly CompositionContainer _container;

    public DomainServiceFactory()
    {
        _catalog = new TypeCatalog(typeof(DomainService1), typeof(DomainService2));

        _container = new CompositionContainer(_catalog);
        //What should I call here? _container.SatisfyImportsOnce(this);
    }

    #region Implementation of IDomainServiceFactory

    public DomainService CreateDomainService(Type domainServiceType, DomainServiceContext context)
    {
        DomainService domainService;
        var export = _container.GetExports(domainServiceType, null, null).FirstOrDefault();
        if(export != null)
        {
            domainService = (DomainService) export.Value;
        }
        else
        {
            domainService = (DomainService)Activator.CreateInstance(domainServiceType);
        }


        domainService.Initialize(context);

        return domainService;

    }

    public void ReleaseDomainService(DomainService domainService)
    {
        domainService.Dispose();
    }

    #endregion
}

SatisfyImportsOnce 是构建容器的方法吗?输入参数是一个ComposablePart,我应该传入什么?

问候。

4

1 回答 1

0

当您调用时,您已经在构建容器:

_container = new CompositionContainer(_catalog);

而且由于您没有任何进口商品,DomainServiceFactory因此没有什么意义_container.SatisfyImportsOnce(this);

当您调用export.Value时,它将自动满足您的域服务类型的任何导入。你错过/试图实现什么?

于 2012-07-27T15:38:53.397 回答