Blazor App
Table of contents
Add Blazor App to dapr.yaml
As we will be having many dapr applications, let’s leverage the multi-run feature of Dapr to run all the applications at once. So let’s add Blazor App to the dapr.yaml file.
- Open the
dapr.yamlfile in the/folder and add a new node for the application including the environment variables.
Toggle solution
- appID: summarizer-frontend
appDirPath: ./src/frontend/
appPort: 11000
command: ["dotnet", "run"]
env:
PUBSUB_REQUESTS_NAME: "summarizer-pubsub"
PUBSUB_REQUESTS_TOPIC: "link-to-summarize"
REQUESTS_API_APP_ID: "summarizer-requests-api"
REQUESTS_API_ENDPOINT: "requests"
DOTNET_URLS: "http://*:11000"
Note: The
appIDis used to identify the application in the Dapr runtime. TheappDirPathis the path to the application folder. TheappPortis the port used by the application. Thecommandis the command used to start the application. Theenvis the environment variables used by the application.
Create Pub / Sub yaml
From the previous definition of the environment variables of our application, we can see that we will be using a pub/sub dapr component. Let’s create the yaml file for the pub/sub component.
- Create a new file named
summarizer-pubsub.yamlin the/dapr/local/componentsfolder using redis as the pub/sub component.
Toggle solution
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: summarizer-pubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
Note: The
nameis the name of the component. Thetypeis the type of the component. Theversionis the version of the component. Themetadatais the configuration of the component. We also added a query index to the state store to be able to query the state store by url. url_hashed is the hashed version of the url to ease the search query and avoid any special characters issues.
Blazor App Overview
- Open the
SummaryRequestService.csfile in the/src/Data/SummaryRequestServicefolder, and notice the different methods being used as en entry point to manage external communication with both Request Api and Pub/Sub
// Publish a new request to the pub/sub component
public async Task AddSummaryRequestAsync(NewSummaryRequestPayload newSummaryRequest) { }
// Get all the requests from the request api
public async Task<SummaryRequest[]> GetSummaryRequestsAsync() {}
Implementing pub/sub and Http Invocation methods.
-
Open the
SummaryRequestService.csfile in the/src/frontendfolder. -
Fill the
GetSummaryRequestsAsyncmethod to get all the requests from the request api. The method should return a list ofRequestobjects.
Toggle solution
public async Task<SummaryRequest[]> GetSummaryRequestsAsync()
{
HttpRequestMessage? response = this._daprClient.CreateInvokeMethodRequest(
HttpMethod.Get,
_settings.requestsApiAppId,
_settings.requestsApiEndpoint
);
return await this._daprClient.InvokeMethodAsync<SummaryRequest[]>(response);
}
- Fill the
AddSummaryRequestAsyncmethod to publish a new request to the pub/sub component.
Toggle solution
public async Task AddSummaryRequestAsync(NewSummaryRequestPayload newSummaryRequest)
{
CancellationTokenSource source = new CancellationTokenSource();
CancellationToken cancellationToken = source.Token;
await this._daprClient.PublishEventAsync<NewSummaryRequestPayload>(
_settings.PubRequestName,
_settings.PubRequestTopic,
newSummaryRequest,
cancellationToken
);
}
Validate that the request is sent to Redis
- Execute dapr run multi run command to start the application
dapr run -f .
-
Open a the blazor application, create a new request using any email and link
-
Check that the request is stored in redis
redis-cli
> keys * # See all keys