In this article, we will create interface
which will enable ASP.NET core Web API and Azure Service App fabric to
communicate. In Getting Started with Azure Service Fabric we created Service
Fabric and in adding Web API in Service Fabric we added ASP.Net core Web API.
Step 1: Right click on Solution and click
Add -> New Project
Step 2: Select Class Library from installed
Visual C# project if you are planning to target for Windows only.
Step 3: Right click on the added class
library and select Manage Nuget Package. Search “Microsoft.ServiceFabric.Services” and install the package in class
library project.
ServiceProxy needs IService which is
available in Microsoft.ServiceFabric.Services nugget package.
Step 4: Create an interface and name it MyStatefulWebAPIInterface, add Microsoft.ServiceFabric.Services.Remoting
namespace and create an interface ICounter with method GetCountAsync. using System.Threading.Tasks; using
Microsoft.ServiceFabric.Services.Remoting; namespace MyStatefulWebAPIInterface { class MyStatefulAndWebAPIInterface { public interface ICounter : IService { Task<long>
GetCountAsync(); } } }
Step 5: Build
MyStatefulWebAPIInterface and add reference of MyStatefulWebAPIInterface in MyStateFulService
project.
Step 6: Now open MyStateFulService.cs file
in MyStateFulService project.
Inherit ICounter interface in
MyStateFulService and implement GetCountAsync method. public async Task<long>
GetCountAsync() { var myDictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, long>>("myDictionary"); using (var tx = this.StateManager.CreateTransaction()) { var result = await myDictionary.TryGetValueAsync(tx,
"Counter"); return
result.HasValue ? result.Value : 0; } }
Step 7: Add
namespace of Microsoft.ServiceFabric.Services.Remoting.Runtime in MyStateFulService.cs file. We need to add this namespace to create
RPC endpoint using CreateServiceRemotingListener method which enables client to
invoke using service proxy. Now modify the existing CreateServiceReplicaListeners with
below code. protected override IEnumerable<ServiceReplicaListener> CreateServiceReplicaListeners() { return new List<ServiceReplicaListener>() { new ServiceReplicaListener(
(context) => this.CreateServiceRemotingListener(context)) }; }
Step 8: Now Add
the reference of interface project in the web application.
Step 9: Open ValuesController.cs in
Controllers folder of MyWebAPIService and below namespace. using MyStatefulWebAPIInterface; using Microsoft.ServiceFabric.Services.Remoting.Client; using
Microsoft.ServiceFabric.Services.Client;
Step 10: Modify
the Get method with below code. public async Task<IEnumerable<string>> Get() { ICounter counter = ServiceProxy.Create<ICounter>(new Uri("fabric:/MyServiceApplication/MyStateFulService"), new ServicePartitionKey(0)); long count = await
counter.GetCountAsync(); return new string[] {
count.ToString() }; }
Step 11: Open
configuration manage from build menu; configuration will be like below image.
Click on the AnyCPU dropdown of
MyStatefulWebAPIInterface and Select New. Make New Platform as X64 and Copy
Settings from to AnyCPU. Make sure Create
new solution platforms is unchecked.
The new configuration should be like below
image.
Step 12: Now run the application, the
application will open in browser where you will see the number which will
change on every refresh.
This ends the article of creating interface
to communicate between Service App Fabric and ASP.NET Web API application.
|