This topic describes how to publish an event to EventBridge by using SDK for C# to call the required API operation.

Publish an event

Sample code:

using System;
using System.Collections.Generic;

using Tea;

namespace Alibabacloud.Sample
{
    public class Client
    {

        /**
         * Uses 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();
            // Your AccessKey ID.
            config.AccessKeyId = "<accessKeyId>";
            // Your AccessKey secret.
            config.AccessKeySecret = "<accessKeySecret>";
            // Your endpoint.
            config.Endpoint = "<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();
        }

    }
}