・Invoke Reliable Actor from stateless Web API

In previous article, the Reliable Actor is incorporated to stateless web API. In this article, create a feature that invoke the Reliable Actor from stateless web API. And in this article, the Reliable Actor has only a function which return stub data.

Sample solution

First step to create communication between stateless web API and the Reliable Actor is put the Microsoft.ServiceFabric.Actors namespace into stateless web API project. Select stateless web API on the solution explorer of Visual Studio and show context menu then select [Manage NuGet package…]. Input text ‘servicefabric.actor’ to inputbox of NuGet package manager and select [Microsoft.ServiceFabric.Actors] package then install it.

Actor service project and its interface project incorporated to stateless web API project has been referred from Azure deploy package project. Then execute the solution.

Actor service project and its interface project incorporated to stateless web API project has been referred from Azure deploy package project. Then execute the solution.

Next step is refer interface project of the Reliable Actor project. Select [Refer] and show context menu then select [Add reference], and select interface project of reference manager window.

Actor service project and its interface project incorporated to stateless web API project has been referred from Azure deploy package project. Then execute the solution.

Then open the ValueController class in the Controllers folder of solution explorer. Put the below code at class root as global scope of the class. Add [using namespace] statements by refactoring. The Service Fabric Local Cluster Manager teachs the service Uri.

using InquiryActor.Interfaces;
using Microsoft.ServiceFabric.Actors;
using Microsoft.ServiceFabric.Actors.Client;
using System;
using System.Collections.Generic;
using System.Web.Http;
namespace InquiryAsWebScrap.Controllers
{
[ServiceRequestActionFilter]
public class ValuesController : ApiController
{
private static Uri serviceUri = new Uri(“fabric:/DigUpSongs/InquiryActorService”);
private static ActorId actorId = ActorId.CreateRandom();
private static IInquiryActor inquiryActor = ActorProxy.Create<IInquiryActor>(actorId, serviceUri);

Last step is modify the Get method of the controller as below. Add [using namespace] statements by refactoring. And the ‘RunSynchronously’ of below code will change at next article, because the Actor model should not be guaranteed synchronous execution.

using System.Threading;
using System.Threading.Tasks;

public IEnumerable<string> Get()
{
int result = 0;
CancellationToken token = new CancellationToken();
new Task(async () => { result = await inquiryActor.GetCountAsync(token); }).RunSynchronously();
return new string[] { “result is”, result.ToString() };
}

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *