0

I'm trying to have a stateless ASP.NET Core service communicate with a stateful ASP.NET core service by using the following configuration on the stateful service:

protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
        {
            return new ServiceReplicaListener[]
            {
                new ServiceReplicaListener(serviceContext =>
                    new KestrelCommunicationListener(serviceContext, (url, listener) =>
                    {
                        ServiceEventSource.Current.ServiceMessage(serviceContext, $"Starting Kestrel on {url}");

                        return new WebHostBuilder()
                            .UseKestrel()
                            .ConfigureServices(
                                services => services
                                    .AddSingleton<StatefulServiceContext>(serviceContext)
                                    .AddSingleton<IReliableStateManager>(this.StateManager))
                            .UseContentRoot(Directory.GetCurrentDirectory())
                            .UseStartup<Startup>()
                            .UseServiceFabricIntegration(listener, ServiceFabricIntegrationOptions.UseUniqueServiceUrl)
                            .UseUrls(url)
                            .Build();
                    }))
            };
        }

I know how to use the reverse proxy by using the following URL http://localhost:19081/App/Service/api/events?PartitionKey=0&PartitionKind=Int64Range but I'm confused on how to use it without the reverse proxy. If I try using fabric:/App/Service how do I specify the partition? Also, I can't seem to do this with the HttpClient class since it only accepts HTTP or HTTPs.

Ryan
  • 4,354
  • 2
  • 42
  • 78

1 Answers1

0

Communicating with a stateful service from within the cluster is usually done by using Service Fabric Remoting. It uses a proxy object that handles connections, resolving and retries.

Here's a sample project that demonstrates a stateless service talking to a stateful service. It uses ServicePartitionKey to select the right partition, and TargetReplicaSelector to select a replica; primary for read/write or secondary for read access.

long partitionKey = PartitionAddressFromWord(word);
var proxy = _dictionaryServiceProxyFactory.CreateServiceProxy<IDictionaryService>(DictionaryServiceUri, new ServicePartitionKey(partitionKey), TargetReplicaSelector.PrimaryReplica, DictionaryServiceListenerSettings.RemotingListenerName);
return proxy.Lookup(word);

Note that the code to determine the partition key to use is something that you need to create based on the way you have partitioned the service data. More info here.

LoekD
  • 11,402
  • 17
  • 27
  • I understand the removing aspect but removing isn't standard communication for all those of services. If I have a container calling a stateful service how would I do that? This was the reason I wanted to do http based communication – Ryan Jan 28 '19 at 16:03
  • If the container can use the SDK it can use remoting as well. If you want to use HTTP, you can use the built-in DNS service. https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-dnsservice#using-dns-in-your-services – LoekD Jan 29 '19 at 07:05