Search code examples
c#microserviceslistenerazure-service-fabricservice-fabric-stateless

Service remoting / sender - receiver


I would like my stateless service (sender) sends an object to a stateful one (receiver) by service remoting. I only find the way that statefull service (receiver) creates a proxy and waits for a message from sender.

ISenderService senderClient = ServiceProxy.Create<ISenderService>(new Uri("fabric:/MyApplication/SenderService"));

string message = await senderClient.MessageAsync();

It is not that I want. I want stateless service (sender) creates proxy to the stateful one, sends messages, and stateful one (receiver) listens and do something when it receives a message.

How can I do that ? I didn't find any documentations or examples about this case.

EDIT:

If I well understood, service A (stateless) creates proxy to call service B (stateful) function.


Solution

  • You're on the right track.

    1. Determine the partitionkey of the partition you want to interact with.
    2. Create a service proxy using that info
    3. Call the service
    long partitionKey = DeterminePartitionAddressFromContext();
    var proxy = _serviceProxyFactory.CreateServiceProxy<ISenderService>(new Uri("fabric:/MyApplication/SenderService"), new ServicePartitionKey(partitionKey), TargetReplicaSelector.PrimaryReplica, RemotingListenerName);
    await proxy.MessageAsync();
    

    More info in this example, in which a stateless service calls a stateful service using SF remoting.

    The partition in the example is based by using a hash of the input object, which will return an int64 number, which can be used to address an Int64RangePartition. You can likely do a similar thing in your stateful service. See this video for more info on the example and partitioning strategies.