title: FabricServiceNotFoundException: Service does not exist
body: <p>I am trying to learn Microsft Azure service fabric using the Pluralsight course (<a href="https://app.pluralsight.com/library/courses/azure-service-fabric-programming-models/table-of-contents" rel="nofollow noreferrer">See here</a>) and Microsoft Documentation (<a href="https://github.com/Azure/service-fabric-issues/issues/986" rel="nofollow noreferrer">here</a>) but getting an exception 
  FabricServiceNotFoundException</p>
  
  <p>See codbase - See codebase - <a href="https://github.com/tarunbhatt9784/AzureServiceFabric.git" rel="nofollow noreferrer">Here</a></p>
  
  <p>I will place bit and places of the source code here as well, in case someone does not want to download the whole solution.</p>
  
  <p>Design
  <a href="https://i.stack.imgur.com/0HkMp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0HkMp.png" alt="enter image description here"></a></p>
  
  <p><strong>Issue</strong> 
  The issue I am facing currently is in the interaction between the stateless service (ECommerce.API) and stateful service (Ecommerce.ProductCatalog). ECommerce.API calls Ecommerce.ProductCatalog to display products.</p>
  
  <p><strong>Relevant Files</strong></p>
  
  <p>Ecommerce.ProductCatalog -> ProductCatalog.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Fabric;
  using System.Linq;
  using System.Threading;
  using System.Threading.Tasks;
  using ECommerce.ProductCatalog.Model;
  using Microsoft.ServiceFabric.Data.Collections;
  using Microsoft.ServiceFabric.Services.Communication.Runtime;
  using Microsoft.ServiceFabric.Services.Remoting;
  using Microsoft.ServiceFabric.Services.Remoting.V2.FabricTransport.Runtime;
  using Microsoft.ServiceFabric.Services.Runtime;
  using ECommerceProductCatalog.Common;
  
  namespace ECommerce.ProductCatalog
  {
      /// &lt;summary&gt;
      /// An instance of this class is created for each service replica by the Service Fabric runtime.
      /// &lt;/summary&gt;
      internal sealed class ProductCatalog : StatefulService, IProductCatalogService
      {
          private IProductRepository _repo;
  
          public ProductCatalog(StatefulServiceContext context)
              : base(context)
          { }
  
          public async Task AddProduct(Product product)
          {
              await _repo.AddProduct(product);
          }
  
          public async Task&lt;IEnumerable&lt;Product&gt;&gt; GetAllProducts()
          {
              return await _repo.GetAllProducts();
          }
  
          /// &lt;summary&gt;
          /// Optional override to create listeners (e.g., HTTP, Service Remoting, WCF, etc.) for this service replica to handle client or user requests.
          /// &lt;/summary&gt;
          /// &lt;remarks&gt;
          /// For more information on service communication, see https://aka.ms/servicefabricservicecommunication
          /// &lt;/remarks&gt;
          /// &lt;returns&gt;A collection of listeners.&lt;/returns&gt;
          protected override IEnumerable&lt;ServiceReplicaListener&gt; CreateServiceReplicaListeners()
          {
              return new[]
              {
                  new ServiceReplicaListener((context) =&gt;
                  {
                      return new FabricTransportServiceRemotingListener(context, this, serializationProvider: new CustomDataContractProvider());
                  })
              };
          }
  
          /// &lt;summary&gt;
          /// This is the main entry point for your service replica.
          /// This method executes when this replica of your service becomes primary and has write status.
          /// &lt;/summary&gt;
          /// &lt;param name="cancellationToken"&gt;Canceled when Service Fabric needs to shut down this service replica.&lt;/param&gt;
          protected override async Task RunAsync(CancellationToken cancellationToken)
          {
              _repo = new ServiceFabricProductRepository(this.StateManager);
  
              var product1 = new Product
              {
                  Id = Guid.NewGuid(),
                  Name = "Dell Monitor",
                  Description = "Computer Monitor",
                  Price = 500,
                  Availability = 100
              };
  
              var product2 = new Product
              {
                  Id = Guid.NewGuid(),
                  Name = "Surface Book",
                  Description = "Microsoft's Latest Laptop, i7 CPU, 1Tb SSD",
                  Price = 2200,
                  Availability = 15
              };
  
              var product3 = new Product
              {
                  Id = Guid.NewGuid(),
                  Name = "Arc Touch Mouse",
                  Description = "Computer Mouse, bluetooth, requires 2 AAA batteries",
                  Price = 60,
                  Availability = 30
              };
  
              await _repo.AddProduct(product1);
              await _repo.AddProduct(product2);
              await _repo.AddProduct(product3);
  
              IEnumerable&lt;Product&gt; all = await _repo.GetAllProducts();
          }
      }
  }
  </code></pre>
  
  <p>Ecommerce.ProductCatalog -> ProductCatalog.cs</p>
  
  <pre><code>    using System;
  using System.Diagnostics;
  using System.Threading;
  using System.Threading.Tasks;
  using Microsoft.ServiceFabric.Services.Runtime;
  using Microsoft.ServiceFabric.Services.Remoting.V2;
  using ECommerceProductCatalog;
  using ECommerce.ProductCatalog;
  
  namespace Ecommerce.ProductCatalog
  {
      internal static class Program
      {
          /// &lt;summary&gt;
          /// This is the entry point of the service host process.
          /// &lt;/summary&gt;
          private static void Main()
          {
              try
              {
                  // The ServiceManifest.XML file defines one or more service type names.
                  // Registering a service maps a service type name to a .NET type.
                  // When Service Fabric creates an instance of this service type,
                  // an instance of the class is created in this host process.
  
                  ServiceRuntime.RegisterServiceAsync("ProductCatalogType",
      context =&gt; new ECommerce.ProductCatalog.ProductCatalog(context)).GetAwaiter().GetResult();
  
                  ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(ECommerce.ProductCatalog.ProductCatalog).Name);
  
                  // Prevents this host process from terminating so services keep running.
                  Thread.Sleep(Timeout.Infinite);
              }
              catch (Exception e)
              {
                  ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <p>Ecommerce.ProductCatalog -> ServiceManifest.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ServiceManifest Name="Ecommerce.ProductCatalogPkg"
                   Version="1.0.0"
                   xmlns="http://schemas.microsoft.com/2011/01/fabric"
                   xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
    &lt;ServiceTypes&gt;
      &lt;!-- This is the name of your ServiceType. 
           This name must match the string used in RegisterServiceType call in Program.cs. --&gt;
      &lt;StatefulServiceType ServiceTypeName="ProductCatalogType" HasPersistedState="true" /&gt;
    &lt;/ServiceTypes&gt;
  
    &lt;!-- Code package is your service executable. --&gt;
    &lt;CodePackage Name="Code" Version="1.0.0"&gt;
      &lt;EntryPoint&gt;
        &lt;ExeHost&gt;
          &lt;Program&gt;Ecommerce.ProductCatalog.exe&lt;/Program&gt;
        &lt;/ExeHost&gt;
      &lt;/EntryPoint&gt;
    &lt;/CodePackage&gt;
  
    &lt;!-- Config package is the contents of the Config directoy under PackageRoot that contains an 
         independently-updateable and versioned set of custom configuration settings for your service. --&gt;
    &lt;ConfigPackage Name="Config" Version="1.0.0" /&gt;
  
    &lt;Resources&gt;
      &lt;Endpoints&gt;
        &lt;!-- This endpoint is used by the communication listener to obtain the port on which to 
             listen. Please note that if your service is partitioned, this port is shared with 
             replicas of different partitions that are placed in your code. --&gt;
        &lt;Endpoint Name="ServiceEndpoint" /&gt;
  
        &lt;!-- This endpoint is used by the replicator for replicating the state of your service.
             This endpoint is configured through a ReplicatorSettings config section in the Settings.xml
             file under the ConfigPackage. --&gt;
        &lt;Endpoint Name="ReplicatorEndpoint" /&gt;
      &lt;/Endpoints&gt;
    &lt;/Resources&gt;
  &lt;/ServiceManifest&gt;
  </code></pre>
  
  <p>ECommerce.API -> ProductController.cs</p>
  
  <pre><code>    using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Mvc;
  using ECommerce.API.Model;
  using ECommerce.ProductCatalog.Model;
  using Microsoft.ServiceFabric.Services.Remoting.Client;
  using Microsoft.ServiceFabric.Services.Client;
  using ECommerce.ProductCatalog.Model;
  
  namespace ECommerce.API.Controllers
  {
      [Route("api/[controller]")]
      public class ProductsController : Controller
      {
          private readonly IProductCatalogService _catalogService;
  
          public ProductsController()
          {
              _catalogService = ServiceProxy.Create&lt;IProductCatalogService&gt;(
                   new Uri("fabric:/ECommerce/ProductCatalog"),
                   new ServicePartitionKey(0));
          }
  
          [HttpGet]
          public async Task&lt;IEnumerable&lt;ApiProduct&gt;&gt; Get()
          {
  
              IEnumerable&lt;Product&gt; allProducts = await _catalogService.GetAllProducts();
  
              return allProducts.Select(p =&gt; new ApiProduct
              {
                  Id = p.Id,
                  Name = p.Name,
                  Description = p.Description,
                  Price = p.Price,
                  IsAvailable = p.Availability &gt; 0
              });
              //return new[] { new ApiProduct() { Id = Guid.NewGuid(), Description = "fake" } };
          }
  
          [HttpPost]
          public async Task Post([FromBody] ApiProduct product)
          {
              var newProduct = new Product()
              {
                  Id = Guid.NewGuid(),
                  Name = product.Name,
                  Description = product.Description,
                  Price = product.Price,
                  Availability = 100
              };
  
              await _catalogService.AddProduct(newProduct);
          }
      }
  }
  </code></pre>
  
  <p>ECommerce.API -> Program.cs</p>
  
  <pre><code>    using Microsoft.ServiceFabric.Services.Runtime;
  using System;
  using System.Diagnostics;
  using System.Threading;
  using System.Threading.Tasks;
  
  namespace ECommerce.API
  {
      internal static class Program
      {
          /// &lt;summary&gt;
          /// This is the entry point of the service host process.
          /// &lt;/summary&gt;
          private static void Main()
          {
              try
              {
                  // The ServiceManifest.XML file defines one or more service type names.
                  // Registering a service maps a service type name to a .NET type.
                  // When Service Fabric creates an instance of this service type,
                  // an instance of the class is created in this host process.
  
                  ServiceRuntime.RegisterServiceAsync("ECommerce.APIType",
                      context =&gt; new API(context)).GetAwaiter().GetResult();
  
                  ServiceEventSource.Current.ServiceTypeRegistered(Process.GetCurrentProcess().Id, typeof(API).Name);
  
                  // Prevents this host process from terminating so services keeps running. 
                  Thread.Sleep(Timeout.Infinite);
              }
              catch (Exception e)
              {
                  ServiceEventSource.Current.ServiceHostInitializationFailed(e.ToString());
                  throw;
              }
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/aWMJo.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aWMJo.png" alt="Error"></a></p>
  
  <p><strong>ApplicationManifest.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ApplicationManifest xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" ApplicationTypeName="EcommerceType" ApplicationTypeVersion="1.0.0" xmlns="http://schemas.microsoft.com/2011/01/fabric"&gt;
    &lt;Parameters&gt;
      &lt;Parameter Name="ECommerce.API_ASPNETCORE_ENVIRONMENT" DefaultValue="" /&gt;
      &lt;Parameter Name="ECommerce.API_InstanceCount" DefaultValue="-1" /&gt;
    &lt;/Parameters&gt;
    &lt;!-- Import the ServiceManifest from the ServicePackage. The ServiceManifestName and ServiceManifestVersion 
         should match the Name and Version attributes of the ServiceManifest element defined in the 
         ServiceManifest.xml file. --&gt;
    &lt;ServiceManifestImport&gt;
      &lt;ServiceManifestRef ServiceManifestName="ECommerce.APIPkg" ServiceManifestVersion="1.0.0" /&gt;
      &lt;ConfigOverrides /&gt;
      &lt;EnvironmentOverrides CodePackageRef="code"&gt;
        &lt;EnvironmentVariable Name="ASPNETCORE_ENVIRONMENT" Value="[ECommerce.API_ASPNETCORE_ENVIRONMENT]" /&gt;
      &lt;/EnvironmentOverrides&gt;
    &lt;/ServiceManifestImport&gt;
    &lt;ServiceManifestImport&gt;
      &lt;ServiceManifestRef ServiceManifestName="Ecommerce.ProductCatalogPkg" ServiceManifestVersion="1.0.0" /&gt;
      &lt;ConfigOverrides /&gt;
    &lt;/ServiceManifestImport&gt;
    &lt;DefaultServices&gt;
      &lt;!-- The section below creates instances of service types, when an instance of this 
           application type is created. You can also create one or more instances of service type using the 
           ServiceFabric PowerShell module.
  
           The attribute ServiceTypeName below must match the name defined in the imported ServiceManifest.xml file. --&gt;
      &lt;Service Name="ECommerce.API" ServicePackageActivationMode="ExclusiveProcess"&gt;
        &lt;StatelessService ServiceTypeName="ECommerce.APIType" InstanceCount="[ECommerce.API_InstanceCount]"&gt;
          &lt;SingletonPartition /&gt;
        &lt;/StatelessService&gt;
      &lt;/Service&gt;
    &lt;/DefaultServices&gt;
  &lt;/ApplicationManifest&gt;
  </code></pre>
  
  <p><strong>Investigation</strong></p>
  
  <p>The below snapshot is giving me an indication that the stateful service was not published successfully. The service fabric explorer is not showing the stateful service. 
  <a href="https://i.stack.imgur.com/VrSjA.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VrSjA.png" alt="enter image description here"></a></p>