This topic describes how to use sample code in the C# SDK to create a topic, create a subscription, enable an HTTP endpoint, publish a message, view the received message on the HTTP server, and delete the topic.

Step 1: Prepare the environment

  1. Download the latest version of the C# SDK, decompress the package, and add the folder to Visual Studio as a solution.
  2. The solution contains four projects, in which AliyunSDK_MNS is the project where the SDK is located. Right-click the project name and select Rebuild. You can view the generated Aliyun.MNS.dll file in the bin directory of the project.

    Add references to other projects so that the projects can reference the Aliyun.MNS.dll file.

  3. To view sample code that you can use to manage topics, open the SyncTopicOperations.cs file in the AliyunSDK_MNS_Sample project.
    1. Set AliyunSDK_MNS_Sample as the startup project and SyncTopicOperations as the startup object.
    2. Open the SyncTopicOperations.cs file and configure the AccessKey ID, AccessKey secret, and endpoint in the beginning rows of the file.
      • AccessKey ID and AccessKey secret
        • The AccessKey pair that is used to access the Alibaba Cloud API.
        • If you are using an Alibaba Cloud account, go to the Security Management page of the Alibaba Cloud console to create and view AccessKey pairs.
        • If you are a RAM user, log on to Alibaba Cloud RAM console to view AccessKey pairs.
      • Endpoint
        • The endpoint that is used to access MNS. Log on to the MNS console, and click Get Endpoint in the upper-right corner to view endpoints.
        • The endpoint of MNS changes based on regions.

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 a topic name.

// 1. Generate a request to create a topic. You can specify the parameters of a 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 already exists. You can query the cause and solve the problem.
    // 2.1 You can also specify code to determine whether a network error has occurred or the topic already exists.
    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

  • Find the SampleHttpServer.cs file in the MNS_CSharp_SDK_Test project. Set MNS_CSharp_SDK_Test as a startup project and run the code. The sample HTTP server depends on .NET Framework 4.5.
  • Ensure that the HTTP server has a public IP address. Otherwise, MNS cannot push messages to the HTTP server by using the HTTP endpoint.
  • Procedure
    • Signs the request that is sent from MNS for verification.
    • Parses request bodies.
    • Returns the following 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 a subscription. The second parameter specifies the endpoint that is used to receive messages. Replace XXXX with the actual IP address and port.
    // 1.1 In this example, the HTTP endpoint that has been enabled in Step 3 is used.
    // 1.2 For more information about supported endpoint types, visit help.aliyun.com/document_detail/27479.html.
    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 already exists. You can query the cause and solve the problem.
    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 automatically pushes the message from the topic to the HTTP endpoint.

try
{
    // 1.1 If you need to send a message to an email address, you must create a request to publish the message and specify the parameters of the message.
    var response = topic.PublishMessage("message here </asdas\">");
    Console.WriteLine("PublishMessage succeed! " + response.MessageId);
}
catch (MNSException me)
{
    // 3. The message may fail to be published if a network error occurs. You can query the cause and solve the problem.
    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

In Step 5, a message has been published to the topic. MNS automatically pushes the message to the HTTP endpoint that has been enabled in Step 3. The response whether the message is received by the HTTP server is printed in your IDE console.

Step 7: Delete the subscription

If you do not 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);
}