C# – System.AddIn in WCF

cnetsystem.addinwcf

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?

Best Answer

Looking at where the error is being raised, it's when the addin is being adapted for the host.

The issue here is that MEF is trying to find and cast to an interface it cant find.

Are your contract assemblies in the same place as your addin assembiles?

Related Topic