服务织物通用服务 [英] Service Fabric with Generic Services
问题描述
我期待有一个泛型类型的服务,即 -
公共接口IFooService< T>
{
任务< T>得到(中间体ID);
}
然而,服务的织物不允许泛型类或泛型方法。我也试着像
public接口INinjaService:IService,IFooService< SuperNinja>
{
}
但它确实不接继承接口说明
服务类型Omni.Fabric.Services.NinjaService不实现
的服务接口。服务接口是从派生Microsoft.ServiceFabric.Services.Remoting.IService式
的人。
块引用>
我似乎无法找到对服务织物文档或计算器泛型任何引用。无论是它仍然太新或可能我领导下错了路。有没有人有任何运气实施这种模式的?能不能做到?如果不能做到?
NinjaService的要求
公共类NinjaService:StatelessService,INinjaService
{
公共NinjaService(StatelessServiceContext serviceContext):基地(serviceContext)
{
}
保护覆盖的IEnumerable< ServiceInstanceListener> CreateServiceInstanceListeners()
{
返回新的[] {新ServiceInstanceListener(上下文=> this.CreateServiceRemotingListener(上下文))};
}
公共任务< SuperNinja>得到(中间体ID)
{
返回Task.FromResult(新SuperNinja());
}
}
消费代码(从Owin的WebAPI称为服务
公共异步任务< SuperNinja>获取(INT键)
{
试
{
INinjaService服务= ServiceProxy.Create< INinjaService>(新的URI(面料:/Omni.Fabric/Services));
变种T =等待service.Get (键).ConfigureAwait(假);
返回吨;
}
赶上(异常前)
{
罚球;
}
}
解决方案服务在服务织物的可以的实施通用接口:
接口IMyService< T>
{$ b $(b T)的Foo ();
}
类Stateful1:StatefulService,IMyService<串>
{
公共Stateful1(StatefulServiceContext上下文)
:基地(上下文)
{}
公共字符串美孚()
{
//做富
}
}
这是很好。
什么的不是的支持为远程过程调用(RPC)的通用接口。这是特定于服务远程通信协议栈,这是你与IService和远程通信侦听器。
所以你的情况,不,仿制药尚不支持。但是,这是特定服务通信栈的限制不是一般的服务,当然你可以使用的你想要的任何通信栈。
I am looking to have a generic type service ie -
public interface IFooService<T> { Task<T> Get(int id); }
However, service fabric does not allow generic classes or generic methods. I have also tried something like
public interface INinjaService : IService, IFooService<SuperNinja> { }
but it does not pick up inherited interfaces stating
The service type 'Omni.Fabric.Services.NinjaService' does not implement any service interfaces. A service interface is the one that derives from 'Microsoft.ServiceFabric.Services.Remoting.IService' type.
I can't seem to find any reference to generics on Service Fabric Documentation or stackoverflow. Either it is still too new or possibly I am headed down the wrong path. Has anyone had any luck implementing this sort of pattern? Can it be done? Should it be done?
NinjaService as requested
public class NinjaService : StatelessService, INinjaService { public NinjaService(StatelessServiceContext serviceContext) : base(serviceContext) { } protected override IEnumerable<ServiceInstanceListener> CreateServiceInstanceListeners() { return new[] { new ServiceInstanceListener(context => this.CreateServiceRemotingListener(context)) }; } public Task<SuperNinja> Get(int id) { return Task.FromResult(new SuperNinja()); } }
Consuming Code (called from an Owin WebApi Service
public async Task<SuperNinja> Get(int key) { try { INinjaService service = ServiceProxy.Create<INinjaService>(new Uri("fabric:/Omni.Fabric/Services")); var t = await service.Get(key).ConfigureAwait(false); return t; } catch (Exception ex) { throw; } }
解决方案Services in Service Fabric can implement generic interfaces:
interface IMyService<T> { T Foo(); } class Stateful1 : StatefulService, IMyService<string> { public Stateful1(StatefulServiceContext context) : base(context) { } public string Foo() { // do foo } }
This is fine.
What isn't supported is generic interfaces for Remote Procedure Call (RPC). This is specific to the Service Remoting communication stack, which is what you have with IService and the Remoting Communication Listener.
So in your case, no, generics are not yet supported. But this is a limitation of that specific service communication stack, not of services in general, and of course you can use any communication stack you want.
这篇关于服务织物通用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!