Hello,
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.AdaptToHostT at System.AddIn.Hosting.AddInActivator.ActivateInAppDomainT at System.AddIn.Hosting.AddInActivator.ActivateT at System.AddIn.Hosting.AddInActivator.ActivateT at System.AddIn.Hosting.AddInActivator.ActivateT at System.AddIn.Hosting.AddInToken.ActivateT 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?