Region ID
The REGION_ID
is an abbreviated code that Google assigns
based on the region you select when you create your app. The code does not
correspond to a country or province, even though some region IDs may appear
similar to commonly used country and province codes. Including
REGION_ID.r
in App Engine URLs is optional for
existing apps and will soon be required for all new apps.
To ensure a smooth transition, we are slowly updating App Engine to use region IDs. If we haven't updated your Google Cloud project yet, you won't see a region ID for your app. Since the ID is optional for existing apps, you don't need to update URLs or make other changes once the region ID is available for your existing apps.
Learn more about region IDs.
Pub/Sub provides reliable, many-to-many, asynchronous messaging between applications. Publisher applications can send messages to a topic, and other applications can subscribe to that topic to receive the messages.
This document describes how to use the Cloud Client Library to send and receive Pub/Sub messages in a .NET app.
Prerequisites
- Follow the instructions in "Hello, World!" for .NET on App Engine to set up your environment and project, and to understand how App Engine .NET apps are structured.
- Write down and save your project ID, because you will need it to run the sample application described in this document.
- Follow the steps to create a service account and service account key to use with your application.
- Enable the Google Cloud Pub/Sub API.
Cloning the sample app
Copy the sample apps to your local machine, and navigate to the pubsub
directory:
git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples
cd dotnet-docs-samples/appengine/flexible/pubsub
Creating a topic and subscription
Create a topic and subscription, which includes specifying the endpoint to which the Pub/Sub server should send requests:
gcloud pubsub topics create YOUR_TOPIC gcloud pubsub subscriptions create YOUR_SUBSCRIPTION ` --topic YOUR_TOPIC ` --push-endpoint ` https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/pubsub/push?token=YOUR_SECRET_TOKEN ` --ack-deadline 10
Setting environment variables
Edit the appsettings.json
file to set your project ID:
Code review
The sample app uses the Cloud Client Library.
Running the sample locally
When running locally, you can use the Cloud SDK to provide authentication
to use Google Cloud APIs. Assuming you set up your environment as described in
Prerequisites, you have already run the gcloud init
command,
which provides this authentication.
To run the sample app locally:
Visual Studio
Open
dotnet-docs-samples\appengine\flexible\AppEngineFlex.sln
with Visual Studio.In Solution Explorer, right-click Pubsub, and choose Debug > Start new instance.
Command line
Run the following commands from the
dotnet-docs-samples\appengine\flexible\Pubsub
directory:dotnet restore dotnet run
In your web browser, enter the following address:
http://localhost:5000/
Simulating push notifications
The application can send messages locally, but it is not able to receive push
messages locally. You can, however, simulate a push message by making an HTTP
request to the local push notification endpoint. The sample includes the file
sample_message.json
.
To send an HTTP POST
request:
Get-Content -Raw .\sample_message.json | Invoke-WebRequest -Uri
http://localhost:5000/Push?token=your-secret-token -Method POST -ContentType
'text/json' -OutFile out.txt
After the request completes, you can refresh localhost:5000
and see the
message in the list of received messages.
Running on App Engine
To deploy the demo app to App Engine by using the gcloud
command-line
tool, you run the following command from the directory where your
app.yaml
file
is located:
Visual Studio
To deploy the Hello World app:
-
Open
dotnet-docs-samples\appengine\flexible\AppEngineFlex.sln
with Visual Studio. -
In Solution Explorer, right-click Pubsub, and choose
Publish to Google Cloud...
- Click App Engine Flex.
- Click Publish.
Command line
Run the following commands from thedotnet-docs-samples\appengine\flexible\Pubsub
directory:
dotnet restore dotnet publish cd bin\Debug\netcoreapp2.1\publish gcloud app deploy
You can now access the application at
https://PROJECT_ID.REGION_ID.r.appspot.com
.
You can use the form to submit messages, but there's no guarantee of which
instance of your application will receive the notification. You can send
multiple messages and refresh the page to see the received message.