Choco
Choco

Reputation: 1488

Service Fabric, Remoting V2 to Stateful Service not working

I can't get this working, I've googled and probably found every page on how to do this, all two of them!

Basically I'm just trying to get SF Remoting V2 working from a stateless .NET Core 2 MVC App' to a Statefull service.

Here's what I have done:

Client Code in Controller: (Simplified as much as Possible):

public class ValuesController : Controller
{

    [HttpGet]
    public async Task<IEnumerable<string>> Get()
    {

         // Provide certificate details.
        var serviceProxyFactory = new ServiceProxyFactory((c) => new FabricTransportServiceRemotingClientFactory());

        var proxy = serviceProxyFactory.CreateServiceProxy<ICallMe>(new Uri("fabric:/SFExperiment/Stateful1"));

        var value3 = await proxy.GetGreeeting("Bob");

        return new[] { "value1", "value2", value3 };
    }

Service Code Interface:

using System.Threading.Tasks;
using Microsoft.ServiceFabric.Services.Remoting;
using Microsoft.ServiceFabric.Services.Remoting.FabricTransport;


[assembly: FabricTransportServiceRemotingProvider(RemotingListener = 
RemotingListener.V2Listener, RemotingClient = RemotingClient.V2Client)]

namespace Stateful1.Abstractions
{

 public interface ICallMe : IService
 {
     Task<string> GetGreeeting(string name);
 }
}

Service Code:

    Public sealed class Stateful1 : StatefulService, ICallMe
{
    public Stateful1(StatefulServiceContext context)
        : base(context)
    { }

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return this.CreateServiceRemotingReplicaListeners();
    }

    public Task<string> GetGreeeting(string name)
    {
        return Task.FromResult(@"Congratulations, you have remoting working. :-) ");
    }

I have added below to ServiceManifest.xml

  <Resources>
<Endpoints>
  <!-- To enable Service remonting for remoting services V2-->
  <Endpoint Name="ServiceEndpointV2" />

  <Endpoint Name="ReplicatorEndpoint" />
</Endpoints>
</Resources>

And it doesn't work.. I get the following Exception:

Invalid partition key/ID '{0}' for selector {1}

What am I doing wrong?

Upvotes: 1

Views: 1643

Answers (1)

LoekD
LoekD

Reputation: 11470

In the call to create a service proxy, you must specify a partition key because you are connecting to a stateful service.

long partitionKey = 0L;  //TODO: Determine partition key
var proxy = serviceProxyFactory.CreateServiceProxy<ICallMe>(new Uri("fabric:/SFExperiment/Stateful1"), new ServicePartitionKey(partitionKey), TargetReplicaSelector.PrimaryReplica);

Also, make sure you reuse your service proxy factory, instead of creating a new one. Have a look at this code for example.

Upvotes: 3

Related Questions