Search code examples
azure-service-fabricservice-fabric-stateful

Client is trying to connect to invalid address on Service Fabric ServiceProxy


I have an Asp.net Core 2.0 stateless Service and an Asp.net Core 2.0 stateful service on Service Fabric 6 with 10 partition count.

I followed this tutorial

https://learn.microsoft.com/en-us/azure/service-fabric/service-fabric-add-a-web-frontend

I followed all steps except that I used the templates in Visual Studio where CreateServiceReplicaListeners is using KestrelCommunicationListener

 protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new ServiceReplicaListener[]
        {
            new ServiceReplicaListener(serviceContext =>
                new KestrelCommunicationListener(serviceContext, (url, listener) =>
                {
                    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();
                }))
        };
    }

When I call my service in the Asp.net Core 2.0 Stateless service using this code:

var service = ServiceProxy.Create<IStorageService>(new Uri("fabric:/MyCluster/Storage"), new ServicePartitionKey(Fnv1aHashCode.Get64bitHashCode(User.Id)));
await service.MyMethod();

An Exception is raised when calling "MyMethod"

Client is trying to connect to invalid address http://localhost:58352/etc..

The url present in the exception is present in the Service Fabric Explorer, and the Port and PartitionKey is correct. No Endpoints are set in the ServiceManifest, since the Stateful service is basically just the template with the addition of an IService Interface and the MyMethod method as per tutorial above.

What I'm missing here? The documentation is not up to date for Asp.net Core 2.0.

I tried to not use partitions, setting ServicePartitionKey(0) but same result.

I don't know how to proceed.


Solution

  • You're mixing two completely different communication stacks:

    You can't mix the two. You need to use an HTTP client to talk to your service's HTTP endpoints. There is an HTTP reverse proxy in Service Fabric that will perform service discover and request forwarding for you to make it easier. There is also a DNS service that allows you to address other services using simple DNS names.

    In the tutorial you reference, the back-end service uses a Service Remoting listener that exposes an RPC endpoint for ServiceProxy to connect to:

    protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners()
    {
        return new List<ServiceReplicaListener>()
        {
            new ServiceReplicaListener(
                (context) =>
                    this.CreateServiceRemotingListener(context))
        };
    }