Set up your Windows development environment to send and receive messages through the ApsaraMQ for RocketMQ .NET SDK.
For new projects, use the latest RocketMQ 5.x SDKs. The 5.x SDKs are fully compatible with ApsaraMQ for RocketMQ 5.x brokers and provide more features than earlier versions. For details, see Release notes. Alibaba Cloud maintains the RocketMQ 4.x, 3.x, and TCP client SDKs for existing business only.
Prerequisites
Before you begin, make sure that you have:
-
A Windows 64-bit operating system
-
Visual Studio 2015 or later with x64 build support
-
.NET Framework 4.5.2 or later
-
The Visual C++ 2015 runtime (if not installed, run the
vc_redist.x64.exeinstaller bundled in the SDK package)
How the .NET SDK works
The .NET SDK is a managed wrapper built on the ApsaraMQ for RocketMQ C++ client. It operates independently from the Windows .NET public library and uses C++ multithreaded processing internally for performance and stability.
The underlying C++ DLL is compiled as a 64-bit binary in release mode, built with Visual Studio 2015 and .NET Framework 4.5.2. Your .NET project must target the x64 platform. The default AnyCPU target in Visual Studio does not work with this SDK.
The .NET SDK supports Windows 64-bit only.
Download the SDK
Download the latest SDK package from the Release notes page. Decompress the package to get the following directory structure:
| Directory or file | Description |
|---|---|
lib/ |
C++ DLL files and the Visual C++ 2015 runtime installer. Contains 64/ONSClient4CPP.lib, 64/ONSClient4CPP.dll, 64/ONSClient4CPP.pdb, and vc_redist.x64.exe. |
demo/ |
Sample code for sending and consuming normal, one-way, and ordered messages. |
interface/ |
PInvoke wrapper code. Include these files in your project. |
SDK_GUIDE.pdf |
SDK documentation and FAQ. |
changelog |
Bug fixes and new features in each release. |
Set up a .NET project in Visual Studio
-
Create a project in Visual Studio.
-
Right-click the project and choose Add > Add Existing Item to add all files from the
interfacedirectory of the SDK package. -
Right-click the project and choose . Set Active solution configuration to Release and Active solution platform to x64.
-
Write and compile your program. Copy the SDK DLL files to the output directory of the executable, then run the program.
NoteThe SDK includes a preconfigured demo project. Open it, compile, and copy the DLL files to the executable directory to run it directly.
Set up an ASP.NET project in Visual Studio
ASP.NET Web Forms projects require additional lifecycle configuration to start and stop the SDK correctly.
-
Create a Web Forms project for ASP.NET in Visual Studio.
-
Right-click the project and choose . Set Active solution configuration to Release and Active solution platform to x64.
-
Right-click the project and choose to add all files from the
interfacedirectory of the SDK package. -
Add SDK startup and shutdown code to Global.asax.cs.
ImportantWrap the SDK code in a singleton class to prevent the garbage collector from reclaiming it due to scope issues. The SDK
demodirectory includes anExample.csfile that implements a basic singleton pattern. 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; // SDK namespace using test; // Namespace for the singleton wrapper. See Example.cs in the SDK demo 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 { // Initialize and start the SDK producer OnscSharp.CreateProducer(); OnscSharp.StartProducer(); } catch (Exception ex) { // Handle errors } } protected void Application_End(object sender, EventArgs e) { try { // Shut down the SDK producer OnscSharp.ShutdownProducer(); } catch (Exception ex) { // Handle errors } } } } -
Write and compile your program.
-
Copy the SDK DLL files to the output directory of the executable or to a system directory, then run the program.
-
In Visual Studio, choose . Select the Use the 64 bit version of IIS Express for websites and projects check box.