Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Service Fabric with Generic Services

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;
        }
    }
like image 876
Tony Avatar asked May 02 '16 15:05

Tony


People also ask

Is service fabric deprecated?

(Upcoming) Deprecation of support for .NET 5.0NET 5.0 runtime is reaching end-of-life on May 8, 2022. Service Fabric releases after that date will drop support for Service Fabric applications running with . NET 5.0 runtime. Current applications running on .

Which type of services are available in Visual Studio while creating service fabric application?

The Service Fabric SDK includes an add-in for Visual Studio that provides templates and tools for creating, deploying, and debugging Service Fabric applications. This topic walks you through the process of creating your first application in Visual Studio.

How does fabric service work?

Service Fabric can deploy applications in seconds, at high density with hundreds or thousands of applications or containers per machine. With Service Fabric, you can mix both services in processes and services in containers in the same application.

When you deploy a stateful service it is deployed as?

You can deploy existing applications as guest executables, Service Fabric stateless or stateful Reliable services or Reliable Actors in containers, and you can mix services in processes and services in containers in the same application.


1 Answers

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.

like image 90
Vaclav Turecek Avatar answered Oct 11 '22 07:10

Vaclav Turecek