This article describes how to use MNS SDK for C# to manage topics. You can create a topic, create a subscription, enable an HTTP endpoint, publish a message, receive the message on the HTTP endpoint, delete the subscription, and delete the topic.

Step 1: Prepare the environment

  1. Download the latest version of the SDK for C#, decompress the package, and then add the folder to Visual Studio as a solution.
  2. The solution contains four projects. Right-click the AliyunSDK_MNS project and select Rebuild. The Aliyun.MNS.dll file is generated in the bin directory of the project.
    Note Specify the Aliyun.MNS.dll file as the reference of the other three projects.
  3. Specify the SyncTopicOperations.cs file in the AliyunSDK_MNS_Sample project.
    1. Set the AliyunSDK_MNS_Sample project as the startup project and the SyncTopicOperations file as the startup object.
    2. Open the SyncTopicOperations.cs file and specify the following information:
      • AccessKey ID and AccessKey secret
        • The AccessKey pair that is used to call the API operations of Alibaba Cloud.
        • If you are using an Alibaba Cloud account, go to the AccessKey Management page of the Alibaba Cloud Management Console to create and view your AccessKey pair.
        • If you are a Resource Access Management (RAM) user, log on to the RAM console to view your AccessKey pair.
      • Endpoint
        • The endpoint that is used to access Message Service (MNS). To view the endpoint of the MNS service, log on to the MNS console, and click Get Endpoint in the upper-right corner.
        • The endpoint varies by region.

Step 2: Create a topic

If no topic is available, you must create a topic. The default name of the new topic is TestCSharpTopic. You can also specify another topic name.

// 1.Generate a request to create a topic. You can also specify other parameters of the topic. 
var createTopicRequest = new CreateTopicRequest
{
    TopicName = _topicName
};

Topic topic = null;
try
{
    topic = client.CreateTopic(createTopicRequest);
    Console.WriteLine("Create topic successfully, topic name: {0}", topic.TopicName);
}
catch (MNSException me)
{
    // 2.The topic may fail to be created if a network error occurs or the topic name already exists. You can call the CatchException or Catch TopicAlreadyExistException operation to identify and resolve the failure. 
    Console.WriteLine("CreateTopic Failed! ErrorCode: " + me.ErrorCode);
}
catch (Exception ex)
{
    Console.WriteLine("Create topic failed, exception info: " + ex.Message);
    return;
}          

Step 3: Enable an HTTP endpoint

  • Set the MNS_CSharp_SDK_Test project as the startup project and the SampleHttpServer.cs file as the startup object.
    Note The sample HTTP endpoint runs based on .NET Framework 4.5.
  • Make sure that the HTTP endpoint has a public IP address.
  • Features of the HTTP endpoint:
    • Signs the request that is sent from MNS for verification.
    • Parses the request body.
    • Returns the HTTP status code 200.
  • For more information about the sample code, see the SampleHttpServer.cs file in the SDK.

Step 4: Create a subscription

Create a subscription so that the MNS server can push messages to the specified endpoint. In this example, the specified endpoint is an HTTP endpoint.

try
{
    // 1.You can specify the parameters of the subscription. The second parameter specifies the endpoint that is used to receive messages. Replace XXXX with the IP address and port of the endpoint that is enabled in Step 3. For information about the supported types of endpoints, see Subscription. 
    SubscribeResponse res = topic.Subscribe(_subscriptionName, "http://XXXX");
    // 2.The subscription is created. 
    Console.WriteLine("Subscribe succeed");
}
catch (MNSException me)
{
    // 3.The subscription may fail to be created if a network error occurs or the subscription name already exists. You can call the CatchException operation to identify and resolve the failure. 
    Console.WriteLine("Subscribe Failed! ErrorCode: " + me.ErrorCode);
}
catch (Exception ex)
{
    Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
}            

Step 5: Publish a message

You can publish a message to the topic. MNS pushes the message from the topic to the HTTP endpoint.

try
{
    // 1.If you need to send a message to an email address, call the PublishMessageRequest operation to specify the parameters of the message. 
    var response = topic.PublishMessage("message here </asdas\">");
    // 2.The message is published. 
    Console.WriteLine("PublishMessage succeed! " + response.MessageId);
}
catch (MNSException me)
{
    // 3.The message may fail to be published if a network error occurs. You can call the CatchException operation to identify and resolve the failure. 
    Console.WriteLine("PublishMessage Failed! ErrorCode: " + me.ErrorCode);
}
catch (Exception ex)
{
    Console.WriteLine("PublishMessage failed, exception info: " + ex.Message);
}           

Step 6: View the received message on the HTTP server

  1. MNS pushes the message that is published to the topic in Step 5 to the HTTP endpoint that is enabled in Step 3.
  2. After the push request of the message is sent, the HTTP endpoint prints the push result to the console.

Step 7: Delete the subscription

If you no longer need to receive messages from the topic, you can delete the subscription.

try
{
    topic.Unsubscribe(_subscriptionName);
    Console.WriteLine("Unsubscribe succeed!");
}
catch (Exception ex)
{
    Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
}           

Step 8: Delete the topic

You can use the following sample code to delete the topic:

try
{
    client.DeleteTopic(_topicName);
    Console.WriteLine("Delete topic succeed");
}
catch (Exception ex)
{
    Console.WriteLine("Delete topic failed, exception info: " + ex.Message);
}