This topic describes how to prepare the environment for accessing ApsaraMQ for RocketMQ by using the TCP client SDK for .NET so that you can use the SDK for .NET to send and receive messages.
- An application that uses ApsaraMQ for RocketMQ must be deployed on Alibaba Cloud Elastic Compute Service (ECS).
- Go to the console to create the topics and group IDs that are referred to in the code. Message tags can be specified by application users. For more information about how to create a message tag, see Create resources.
Windows SDK for .NET overview
The SDK for .NET provided by Alibaba Cloud is a managed wrapper for the ApsaraMQ for RocketMQ SDK for C++. This ensures that the SDK for .NET is completely independent of the public library of the Windows SDK for .NET. C++ multi-thread concurrent processing is used to ensure the efficiency and stability of the SDK for .NET.
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.
- 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 version of the SDK for .NET. For information about the download URL for the latest version of the SDK for .NET, see Release notes.
- 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. The actual SDK file may vary based on the SDK version that you downloaded.
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
- Use Visual Studio 2015 to create your project.
- Right-click the project and choose Add > Add Existing Item to add all files in the interface directory of the downloaded SDK package.
- Right-click the project and choose Active solution configuration to Release and set Active solution platform to x64. . Set
- 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
- Create a Web Forms project for ASP.NET by using Visual Studio 2015.
- Right-click the project and choose Active solution configuration to Release and set Active solution platform to x64. . Set
- Right-click the project and choose
For more information about how to configure a common .NET project, see Step 2.
to add all files in the interface directory of the downloaded SDK package. - Add the code for starting and stopping the SDK to the Global.asax.cs file.
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 classes of a wrapper SDK are located. 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) { // Exception handling. } } protected void Application_End(object sender, EventArgs e) { try { // The code for stopping the SDK. OnscSharp.ShutdownProducer(); } catch (Exception ex) { // Exception handling. } } } }
- 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 run the program.
- Choose Use the 64 bit version of IIS Express for websites and projects check box. . Then, select the