EventBridge provides two C# SDKs:
Management API SDK: Manage EventBridge resources programmatically, such as creating event buses and rules.
Data API SDK: Publish events to EventBridge by calling the PutEvents operation.
Prerequisites
Before you begin, ensure that you have:
Environment requirements
| Requirement | Minimum version |
|---|---|
| .NET Core | 2.0 or later |
| Visual Studio | 2010 or later |
To verify your .NET Core version, run the following command:
dotnet --versionManagement API SDK
Use the management API SDK to manage EventBridge resources such as event buses and rules.
Install the management API SDK
Install the AlibabaCloud.SDK.Eventbridge20200401 NuGet package.
Option 1: .NET CLI (recommended)
dotnet add package AlibabaCloud.SDK.Eventbridge20200401 --version 1.0.0Option 2: NuGet Package Manager Console in Visual Studio
Choose Tools > NuGet Package Manager > Package Manager Console.
Run the following command:
Install-Package AlibabaCloud.SDK.Eventbridge20200401 -Version 1.0.0Sample code
Generate sample code for management API operations in OpenAPI Explorer. For more information, see Automatic generation of SDK examples.
Data API SDK
Use the data API SDK to publish events to EventBridge. This SDK supports only the PutEvents operation.
Install the data API SDK
Install the AlibabaCloud.SDK.Eventbridge NuGet package.
Option 1: .NET CLI (recommended)
dotnet add package AlibabaCloud.SDK.EventbridgeOption 2: NuGet Package Manager Console in Visual Studio
Choose Tools > NuGet Package Manager > Package Manager Console.
Run the following command:
Install-Package AlibabaCloud.SDK.EventbridgePublish events with PutEvents
The following example creates an EventBridge client and publishes a CloudEvent to a custom event bus named demo-bus.
Replace the <endpoint> placeholder with your EventBridge endpoint. Set the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables before you run the code.
using System;
using System.Collections.Generic;
using Tea;
namespace Alibabacloud.Sample
{
public class Client
{
/// <summary>
/// Initialize the EventBridge client with credentials from environment variables.
/// </summary>
public static AlibabaCloud.SDK.EventBridge.EventBridgeClient CreateClient()
{
AlibabaCloud.SDK.EventBridge.Models.Config config = new AlibabaCloud.SDK.EventBridge.Models.Config();
// Read credentials from environment variables to avoid hardcoding secrets.
config.AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID");
config.AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
config.Endpoint = "<endpoint>";
return new AlibabaCloud.SDK.EventBridge.EventBridgeClient(config);
}
/// <summary>
/// Publish a CloudEvent to EventBridge using PutEvents.
/// </summary>
public static void PutEvents(AlibabaCloud.SDK.EventBridge.EventBridgeClient client)
{
AlibabaCloud.SDK.EventBridge.Models.CloudEvent event_ = new AlibabaCloud.SDK.EventBridge.Models.CloudEvent();
event_.Datacontenttype = "application/json";
event_.Data = AlibabaCloud.TeaUtil.Common.ToBytes("test");
event_.Id = "a5074581-7e74-4e4c-868f-47e7afdf****";
event_.Source = "acs.oss";
event_.Specversion = "1.0";
event_.Type = "oss:ObjectCreated:PostObject";
event_.Time = "2020-08-24T13:54:05.965Asia/Shanghai";
event_.Subject = "1.0";
event_.Type = "acs:oss:cn-hangzhou:1234567:xls-papk/game_apk/123.jpg";
event_.Extensions = new Dictionary<string, object>
{
{ "aliyuneventbusname", "demo-bus" },
};
try
{
AlibabaCloud.SDK.EventBridge.Models.PutEventsResponse resp = client.PutEvents(new List<AlibabaCloud.SDK.EventBridge.Models.CloudEvent>
{
event_
});
Console.WriteLine("--------------------Publish event to the aliyun EventBus--------------------");
Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(resp.ToMap()));
}
catch (TeaException error)
{
Console.WriteLine(error.Message);
}
catch (Exception _error)
{
TeaException error = new TeaException(new Dictionary<string, object>
{
{ "message", _error.Message }
});
Console.WriteLine(error.Message);
}
}
static void Main(string[] args)
{
AlibabaCloud.SDK.EventBridge.EventBridgeClient client = Client.CreateClient();
Client.PutEvents(client);
Console.ReadKey();
}
}
}CloudEvent properties
The following table describes the CloudEvent properties used in the sample code.
| Property | Type | Description | Example |
|---|---|---|---|
Datacontenttype | string | Content type of the event data. | "application/json" |
Data | byte[] | Event payload, converted to bytes. | AlibabaCloud.TeaUtil.Common.ToBytes("test") |
Id | string | Unique event identifier. | "a5074581-7e74-4e4c-868f-47e7afdf****" |
Source | string | Event source identifier. | "acs.oss" |
Specversion | string | CloudEvents specification version. | "1.0" |
Type | string | Event type. | "oss:ObjectCreated:PostObject" |
Time | string | Timestamp of the event. | "2020-08-24T13:54:05.965Asia/Shanghai" |
Subject | string | Subject of the event. | "1.0" |
Extensions | Dictionary | Custom extension attributes. Set aliyuneventbusname to the name of the target event bus. | { "aliyuneventbusname", "demo-bus" } |
Error handling
The sample code catches two exception types:
| Exception | Description |
|---|---|
TeaException | SDK-specific errors returned by EventBridge, such as authentication failures or invalid parameters. |
Exception | General .NET exceptions, such as network timeouts. The code wraps these in a TeaException for consistent error reporting. |