All Products
Search
Document Center

ApsaraMQ for RocketMQ:Environment preparation

Last Updated:Nov 28, 2023

This topic describes how to prepare the environment for accessing ApsaraMQ for RocketMQ by using the SDK for .NET so that you can use the SDK for .NET to send and receive messages.

Important
  • We recommend that you use the latest RocketMQ 5.x SDKs. These SDKs are fully compatible with the ApsaraMQ for RocketMQ 5.x brokers and provides more functions and enhanced features. For more information, see Release notes.
  • Alibaba Cloud only maintains RocketMQ 4.x, 3.x, and TCP client SDKs. We recommend that you use them only for existing business.

Windows SDK for .NET overview

The .NET version provided by Alibaba Cloud is a hosted encapsulation based on the ApsaraMQ for RocketMQ C++ version. It ensures that .NET is completely independent of the Windows .NET public library. C++ multithread concurrent processing is used internally to ensure the efficiency and stability of the .NET version.

When Visual Studio is used to develop .NET applications and class libraries, the default target platform is AnyCPU. This means that the x86 or x64 platform can be automatically selected based on the CPU type. During running, the Just-in-Time (JIT) compiler in the common language runtime (CLR) of .NET converts the IL code into the x86 or x64 machine code. The DLL generated by the C or C++ compiler is the machine code. Therefore, a target platform is selected during compilation. When the compilation options are set, the C or C++ project is compiled as an x64 64-bit DLL. Therefore, the 64-bit DLL in release mode compiled using Visual Studio 2015 and .NET Framework 4.5.2 is provided. The 64-bit DLL in release mode is also available to other Visual Studio versions.

Important
  • The SDK for .NET supports only the Windows 64-bit operating system.

  • C++ DLL files require the installation package of the Visual C++ 2015 runtime environment. If the Visual Studio 2015 runtime environment is not installed, run the vc_redist.x64.exe program provided in the SDK.

Download the Windows SDK for .NET

We recommend that both new users and existing users that are not concerned with upgrade costs download the latest SDK. For information about the download URL for the latest version of the SDK for .NET, see Release notes.

Download and decompress the package. The following directory structure is displayed:

  • lib/

    Contains files related to the underlying C++ DLL and the installation package of the Visual C++ 2015 runtime environment. If the Visual Studio 2015 runtime environment is not installed, copy the following code to install the vc_redist.x64.exe program:

      64/
          ONSClient4CPP.lib
          ONSClient4CPP.dll
          ONSClient4CPP.pdb
        vc_redist.x64.exe                    
  • demo/

    Contains sample code for sending normal messages, one-way messages, and ordered messages. This directory also includes sample code for the consumption of normal messages and ordered messages.

  • interface/

    Encapsulates PInvoke code. The code must be included in your project code.

  • SDK_GUIDE.pdf

    Contains the documentation and frequently asked questions (FAQ) about how to prepare the SDK environment.

  • changelog

    Contains bug fixes and new features in the new releases.

Configuration for using the SDK for .NET in Visual Studio 2015

  1. Use Visual Studio 2015 to create your project.

  2. Right-click the project and choose Add > Add Existing Item to add all files in the interface directory of the downloaded SDK package.

  3. Right-click the project and choose Properties > Configuration Manager. Set Active solution configuration to Release and set Active solution platform to x64.

  4. Write and compile the test program, save the DLL file of the SDK to the directory of the executable file or to the system directory, and then run the program.

    Note

    The SDK provides a preconfigured demo project. You can directly open the project and compile it. When you run the project, copy the following related DLL file to the directory of the executable file:

Configuration for using the ApsaraMQ for RocketMQ SDK in Visual Studio 2015 ASP.NET

  1. Create a Web Forms project for ASP.NET by using Visual Studio 2015.

  2. Right-click the project and choose Properties > Configuration Manager. Set Active solution configuration to Release and set Active solution platform to x64.

  3. Right-click the project and choose Add > Add Existing Item to add all files in the interface directory of the downloaded SDK package.

    For more information about how to configure a common .NET project, see Step 2.

  4. Add the code for starting and stopping the SDK to the Global.asax.cs file.

    Important

    We recommend that you encapsulate the SDK code as a singleton class so that the code cannot be recycled by the garbage collector due to scope problems. The example directory of the SDK contains the Example.cs file for implementing a simple singleton class. To use Example.cs, you must include it in your own project.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Optimization;
    using System.Web.Routing;
    using System.Web.Security;
    using System.Web.SessionState;
    using ons;    // The namespace where the SDK is located. 
    using test;    // The namespace where the class with the roughly encapsulated SDK is located. See the Example.cs file in the example directory of the SDK. 
    namespace WebApplication4
    {
      public class Global : HttpApplication
      {
           void Application_Start(object sender, EventArgs e)
           {
               // Code that runs on application startup
               RouteConfig.RegisterRoutes(RouteTable.Routes);
               BundleConfig.RegisterBundles(BundleTable.Bundles);
               try
               {
                   // The code for starting the SDK. The following code is the code after the SDK is roughly encapsulated. 
                   OnscSharp.CreateProducer();
                   OnscSharp.StartProducer();
               }
               catch (Exception ex)
               {
               // Handle errors. 
               }
           }
           protected void Application_End(object sender, EventArgs e)
           {
               try
               {
                  // The code for stopping the SDK. 
                 OnscSharp.ShutdownProducer();
               }
               catch (Exception ex)
               { 
               // Handle errors 
               }
           }
      }
    }                  
  5. Write and compile the test program.

  6. Save the DLL file of the SDK to the directory of the executable file or to the system directory and run the program.

  7. Choose Tools > Options > Projects and Solutions > Web Projects. Then, select the Use the 64 bit version of IIS Express for websites and projects check box.