本文介绍如何使用C# SDK在一个事件规则下创建一个或多个投递目标。

示例代码如下:

using System;
using System.Collections.Generic;
using Tea;

namespace test
{
    public class Client
    {

        /**
         * Create client初始化公共请求参数。
         */
        public static AlibabaCloud.SDK.EventBridge.EventBridgeClient CreateClient()
        {
            AlibabaCloud.SDK.EventBridge.Models.Config config = new AlibabaCloud.SDK.EventBridge.Models.Config();
            // 您的AccessKey ID。
            config.AccessKeyId = "<accessKeyId>";
            // 您的AccessKey Secret。
            config.AccessKeySecret = "<accessKeySecret>";
            // 您的接入点。
            config.Endpoint = "<endpoint>";
            return new AlibabaCloud.SDK.EventBridge.EventBridgeClient(config);
        }

        public static void CreateTargetsSample(AlibabaCloud.SDK.EventBridge.EventBridgeClient client)
        {
            try
            {
                AlibabaCloud.SDK.EventBridge.Models.CreateTargetsRequest createTargetsRequest = new AlibabaCloud.SDK.EventBridge.Models.CreateTargetsRequest();
                createTargetsRequest.EventBusName = "demo-bus";
                createTargetsRequest.RuleName = "myRule";
                AlibabaCloud.SDK.EventBridge.Models.TargetEntry targetEntry = new AlibabaCloud.SDK.EventBridge.Models.TargetEntry();
                targetEntry.Id = "1234";
                targetEntry.Endpoint = "http://www.baidu.com";
                List<AlibabaCloud.SDK.EventBridge.Models.TargetEntry> list = new List<AlibabaCloud.SDK.EventBridge.Models.TargetEntry>
                {
                    targetEntry
                };
                createTargetsRequest.Targets = list;
                AlibabaCloud.SDK.EventBridge.Models.CreateTargetsResponse response = client.CreateTargets(createTargetsRequest);
                Console.WriteLine("--------------------Create targets success--------------------");
                Console.WriteLine(AlibabaCloud.TeaUtil.Common.ToJSONString(response.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 = CreateClient();
            CreateTargetsSample(client);
            Console.ReadKey();
        }

    }
}