3

I have a question about use AddIn framework, provided by .NET Framework (currently use 3.5 SP1) implemented in System.AddIn namespace. I build a prototype with simple AddIn. This AddIn is instantiated in business logic of WCF Service.

Implementation of business logic (only necessary code is shown):

internal class BusinessLayer : IBusinessLayer
{
    public object Execute(object toConvert, Operation operation)
    {
        IDictionary<string, AddInToken> tokens = AddIns.Store.GetAddInsTokens(@"c:\SomePathToStore");

        foreach (KeyValuePair<string, AddInToken> token in tokens)
        {
            if (operation.Name == token.Key && operation.Version == token.Value.Version)
            {
                ConversionHostView view = token.Value.Activate<ConversionHostView>(AddInSecurityLevel.FullTrust);

                object converted =  view.Convert(toConvert);

                AddInController.GetAddInController(view).Shutdown();

                return converted;
            }
        }

        throw new InvalidOperationException("No operation found!");
    }
    ...
}

Implementation of service (only necessary code is shown):

public class Service : IServiceContract
{
    IBusinessLayer bl;

    public Service()
    {
        bl = BL.BLFactory.GetBL();
    }

    public object Execute(object toConvert, ERES.ConversionService.Entity.Operation operation)
    {
        return bl.Execute(toConvert, operation);
    }
    ...
}

I created two Unit tests. One call direct method of business logic, other one WCF method. Direct call works fine, but if I activate AddIn from WCF i get this exception:

"Unable to cast transparent proxy to type 'ERES.ConversionService.Contract.IConversionContract'

Stack trace:

at ConversionHostViewToContractAdapter_ConstructorInvoker(Object ) at System.AddIn.Hosting.AddInActivator.AdaptToHost[T](AddInToken pipeline, IContract addInContract) at System.AddIn.Hosting.AddInActivator.ActivateInAppDomain[T](AddInToken pipeline, AppDomain domain, AddInControllerImpl controller, Boolean weOwn) at System.AddIn.Hosting.AddInActivator.Activate[T](AddInToken token, PermissionSet permissionSet, String appDomainName) at System.AddIn.Hosting.AddInActivator.Activate[T](AddInToken token, AddInSecurityLevel level, String appDomainName) at System.AddIn.Hosting.AddInActivator.Activate[T](AddInToken token, AddInSecurityLevel level) at System.AddIn.Hosting.AddInToken.Activate[T](AddInSecurityLevel trustLevel) at ERES.ConversionService.BL.BusinessLayer.Execute(Object toConvert, Operation operation) in C:\Documents and Settings\kc\My Documents\Visual Studio 2008\Projects\ConversionServiceSolution\ERES.ConversionService.BL\BusinessLayer.cs:line 44 at ERES.ConversionService.Service.Execute(Object toConvert, Operation operation) in C:\Documents and Settings\kc\My Documents\Visual Studio 2008\Projects\ConversionServiceSolution\ERES.ConversionService\Service.svc.cs:line 25 at SyncInvokeExecute(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)

Any help?

Regards Anton Kalcik

UPDATE: I was able to go around this with this code:

ConversionHostView view = token.Value.Activate<ConversionHostView>(AppDomain.CurrentDomain);

So at this case is only possible to execute AddIn only at same AppDomain as service self. But I don't understand why?

4

1 回答 1

0

查看引发错误的位置,这是在为主机调整插件时。

这里的问题是 MEF 正在尝试查找并转换到它找不到的接口。

您的合同程序集与您的插件程序集是否在同一个地方?

于 2012-01-04T12:33:05.473 回答