All Products
Search
Document Center

EventBridge:SDK for C#

Last Updated:Nov 07, 2023

This topic describes how to install the SDK for C# provided by EventBridge and how to use the SDK for C# to publish events. Sample code is also provided in this topic.

Overview

EventBridge SDKs are classified into management API SDKs and data API SDKs. Sample code that is used by different types of SDKs varies.

  • Management API SDKs: the SDKs that are used to perform operations on the EventBridge console.

  • Data API SDKs: the channel for event data. Only the PutEvents API operation is called by using this type of SDK.

Before you begin

Perform the following operations:

Environment preparations

  • Environment requirements

  • Check the .NET Core version

    Run the dotnet --version command to check the .NET Core version.

Management API SDKs

Install the SDK for C#

Install the SDK for C# in Visual Studio.

  1. Choose Tools > NuGet Package Manager > Package Manager Console.

  2. Run the following command to install the SDK for C#:

    Install-Package AlibabaCloud.SDK.Eventbridge20200401 -Version 1.0.0

Sample code

You can call the corresponding operation in OpenAPI Explorer to automatically generate sample code. For more information, see Automatic generation of SDK examples.

Data API SDKs

Install the SDK for C#

Install the SDK for C# in Visual Studio.

  1. Choose Tools > NuGet Package Manager > Package Manager Console.

  2. Run the following command to install the SDK for C#:

    Install-Package AlibabaCloud.SDK.Eventbridge

Sample code

Data API SDKs support only the PutEvents API operation. If you want to use the SDK for C# to publish one or more events, refer to the following sample code:

using System;
using System.Collections.Generic;

using Tea;

namespace Alibabacloud.Sample
{
    public class Client
    {

        /**
         * Use the CreateClient() function to initialize common request parameters. 
         */
        public static AlibabaCloud.SDK.EventBridge.EventBridgeClient CreateClient()
        {
            AlibabaCloud.SDK.EventBridge.Models.Config config = new AlibabaCloud.SDK.EventBridge.Models.Config();
            SetAccessKeyId(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")).
            SetAccessKeySecret(os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")).
            SetEndpoint("<endpoint>")
            return new AlibabaCloud.SDK.EventBridge.EventBridgeClient(config);
        }

        /**
         * PutEvents
         */
        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();
        }

    }
}