This topic describes how to prepare your environment to send and receive messages by using the SDK for .NET with ApsaraMQ for RocketMQ.
We recommend that you use the latest RocketMQ 5.x SDKs. These SDKs are fully compatible with ApsaraMQ for RocketMQ 5.x brokers and provides more functions and enhanced features. For more information, see C++ SDK version information.
Alibaba Cloud only maintains RocketMQ 4.x, 3.x, and TCP client SDKs. We recommend that you use them only for existing business.
Windows .NET SDK
The SDK for .NET is a managed wrapper built on top of the C++ version of the ApsaraMQ for RocketMQ client. This design ensures that the SDK operates independently of the standard Windows .NET public libraries. Internally, it uses C++ multithreaded concurrent processing to ensure high performance and stability.
When you develop .NET applications and class libraries in Visual Studio (VS), the default target platform is "Any CPU". This setting allows the application to run on either x86 or x64 platforms, depending on the processor architecture. At runtime, the Just-In-Time (JIT) compiler of the Common Language Runtime (CLR) converts the Intermediate Language (IL) code into native x86 or x64 machine code. In contrast, a DLL generated by a C/C++ compiler is already machine code, with the target platform determined at compile time. By setting the appropriate compilation options, the C/C++ project is compiled into an x64 DLL. Therefore, this SDK provides a 64-bit DLL in release mode, compiled with Visual Studio 2015 and .NET Framework 4.5.2. This DLL is also compatible with other versions of Visual Studio.
-
The SDK for .NET supports only 64-bit Windows operating systems.
-
The C++ DLL files require the Visual C++ 2015 runtime environment. If the Visual C++ 2015 runtime is not installed on your system, run the
vc_redist.x64.exefile provided in the SDK package to install it.
Download the Windows .NET SDK
Download the latest version of the SDK. For the download link to the latest version of the SDK for .NET, see Release notes.
The decompressed package contains the following directories and files:
-
lib/
Contains the underlying C++ DLL files and the Visual C++ 2015 runtime installer. If the Visual C++ 2015 runtime is not installed, you must run
vc_redist.x64.exeto install it.64/ ONSClient4CPP.lib ONSClient4CPP.dll ONSClient4CPP.pdb vc_redist.x64.exe -
demo/
Contains sample code for sending and consuming normal, one-way, and ordered messages.
-
interface/
Contains the PInvoke wrapper code that you must include in your project.
-
SDK_GUIDE.pdf
Contains documentation on how to prepare the SDK environment and a list of frequently asked questions (FAQ).
-
changelog
Lists the bug fixes and new features included in each release.
Configure the .NET SDK in Visual Studio 2015
-
Create a project in Visual Studio 2015. Open Visual Studio 2015 and click New Project.... In the New Project dialog box, navigate to Installed > Templates > Visual C# > Windows. Select Console Application, set the target framework to .NET Framework 4.5.2, specify a project name, and then click OK.
-
Right-click the project, select Add > Existing Item..., and add all files from the interface directory of the downloaded SDK package.
-
Right-click the project and select . Set Active solution configuration to Release and Active solution platform to x64.
-
Write your test program and compile it. To run the program, place the SDK's DLL files in the same directory as the executable or in a system directory. After compilation, you can find the required runtime files, such as ONSClient4CPP.dll, in the output directory, which is typically
bin\x64\Release.NoteThe SDK includes a pre-configured demo project. You can open and compile this project directly. Before running the application, copy the required DLL files to the same directory as the executable.
The files to copy are
ONSClient4CPP.dll,ONSClient4CPP.lib, andONSClient4CPP.pdb. The destination directory, such asbin\Debugorbin\Release, depends on your build configuration and target platform.
Configure ASP.NET in Visual Studio 2015 to use the ApsaraMQ for RocketMQ SDK
-
Create an ASP.NET Web Forms project in Visual Studio 2015. In Visual Studio 2015, click New Project. In the template tree on the left, select Installed > Templates > Visual C#. From the template list, select ASP.NET Web Application, specify the project name (for example,
WebApplication1), set the framework version to .NET Framework 4.5.2, and then click OK. -
Right-click the project and select . Set Active solution configuration to Release and Active solution platform to x64.
-
Right-click the project, select , and add all files from the interface directory of the downloaded SDK package.
For more information, see Step 2 in the "Configure the SDK for .NET in Visual Studio 2015" section.
-
Add the code to start and shut down the SDK in the Global.asax.cs file.
ImportantWrap the SDK code in a singleton class. This prevents the garbage collector from prematurely collecting the object when it goes out of scope. The
exampledirectory in the SDK package includesExample.cs, which provides a simple singleton implementation. You must includeExample.csin your project to use it.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 for a simple wrapper class for the SDK. See Example.cs in the SDK example directory. 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 { // Code to start the SDK. The following code uses a simple wrapper. OnscSharp.CreateProducer(); OnscSharp.StartProducer(); } catch (Exception ex) { // Handle exceptions. } } protected void Application_End(object sender, EventArgs e) { try { // Code to shut down the SDK. OnscSharp.ShutdownProducer(); } catch (Exception ex) { // Handle exceptions. } } } } -
Write your test program and compile it.
-
To run the program, place the SDK's DLL files in the project's bin directory. After compilation, verify that the project's output bin directory, such as
Projects\WebApplication4\WebApplication4\bin, contains theONSClient4CPP.pdb,ONSClient4CPP.dll, andONSClient4CPP.libfiles. -
Navigate to , and then select the Use the 64-bit version of IIS Express for websites and projects checkbox.