WCF 中的 System.AddIn
我有一个关于使用 AddIn 框架的问题,该框架由 .NET Framework(当前使用 3.5 SP1)提供,在 System.AddIn 命名空间中实现。 我用简单的插件构建了一个原型。 此 AddIn 在 WCF 服务的业务逻辑中实例化。
业务逻辑的实现(仅显示必要的代码):
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!");
}
...
}
服务的实现(仅显示必要的代码):
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);
}
...
}
我创建了两个单元测试。 一种调用业务逻辑的直接方法,另一种调用WCF方法。 直接调用工作正常,但如果我从 WCF 激活 AddIn,则会出现此异常:
“无法将透明代理强制转换为类型 'ERES.ConversionService.Contract.IConversionContract'
堆栈跟踪:
at ConversionHostViewToContractAdapter_ConstructorInvoker(目的 ) 在 System.AddIn.Hosting.AddInActivator.AdaptToHost[T](AddInToken 管道,IContract addInContract) 在 System.AddIn.Hosting.AddInActivator.ActivateInAppDomain[T](AddInToken 管道、AppDomain 域、AddInControllerImpl 控制器、布尔 weOwn) 在 System.AddIn.Hosting.AddInActivator.Activate[T](AddInToken 令牌,PermissionSet 权限集,字符串 appDomainName) 在 System.AddIn.Hosting.AddInActivator.Activate[T](AddInToken 令牌,AddInSecurityLevel 级别,字符串 appDomainName) 在 System.AddIn.Hosting.AddInActivator.Activate[T](AddInToken 令牌,AddInSecurityLevel 级别) 在 System.AddIn.Hosting.AddInToken.Activate[T](AddInSecurityLevel trustLevel) 在 C:\Documents and Settings\kc\My Documents\Visual Studio 2008\Projects\ConversionServiceSolution\ERES.ConversionService.BL\BusinessLayer.cs 中的 ERES.ConversionService.BL.BusinessLayer.Execute(Object toConvert,Operation 操作):第 44 行 在 C:\Documents and Settings\kc\My Documents\Visual Studio 2008\Projects\ConversionServiceSolution\ERES.ConversionService\Service.svc.cs 中的 ERES.ConversionService.Service.Execute(Object toConvert,操作操作):第 25 行 在 SyncInvokeExecute(对象, 对象[], 对象[]) 在 System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象实例,对象 [] 输入,对象 [] 和输出) 在 System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
有帮助吗?
问候 安东·卡尔西克
更新: 我可以使用以下代码解决这个问题:
ConversionHostView view = token.Value.Activate<ConversionHostView>(AppDomain.CurrentDomain);
因此,在这种情况下,只能在与服务自身相同的 AppDomain 中执行 AddIn。 但我不明白为什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看引发错误的位置,发现该错误是在针对主机调整插件时出现的。
这里的问题是 MEF 正在尝试查找并转换到它无法找到的接口。
您的合同程序集与您的插件程序集位于同一位置吗?
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?