public async Task <IActionResult> PostAsync([FromBody] ToDo payload)
        {
            var contentType = "application/json";

            var ce = CloudEventFactory.Create(contentType, payload);

            ce.EventType        = "org.aliencube.ToDos.OnToDoCreated";
            ce.EventTypeVersion = "1.0";
            ce.Source           = "/subscriptions/[SUBSCRIPTION_ID]/resourceGroups/[RESOURCE_GROUP_NAME]/providers/microsoft.eventgrid/topics/[TOPIC_NAME]#[EVENT_SUBJECT_NAME]";

            ce.EventId     = Guid.NewGuid().ToString();
            ce.EventTime   = DateTimeOffset.UtcNow;
            ce.ContentType = "application/json";

            var requestUri = "[EVENT_GRID_TOPIC_ENDPOINT_URL]";

            using (var client = new HttpClient())
                using (var content = CloudEventContentFactory.Create(ce))
                {
                    content.Headers.Add("AEG-SAS-KEY", "EVENT_GRID_TOPIC_ACCESS_TOKEN");

                    var response = await client.PostAsync(requestUri, content).ConfigureAwait(false);

                    return(new OkObjectResult(response.ReasonPhrase));
                }
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            var todo = new ToDo()
            {
                Title = "sample todo"
            };
            var contentType = "application/json";

            var ce = CloudEventFactory.Create(contentType, todo);

            ce.EventType        = "org.aliencube.ToDos.OnToDoCreated";
            ce.EventTypeVersion = "1.0";
            ce.Source           = (new Uri("http://localhost")).ToString();
            ce.EventId          = Guid.NewGuid().ToString();
            ce.EventTime        = DateTimeOffset.UtcNow;

            var requestUri = "http://localhost:5604/api/events";

            using (var client = new HttpClient())
                using (var content = CloudEventContentFactory.Create(ce))
                {
                    var body = GetResponseAsync(client, requestUri, content).Result;

                    Console.WriteLine(body);
                }
        }
        public void Given_CloudEvent_Should_Return_BinaryCloudEventContent()
        {
            var contentType = "text/plain";
            var data        = "hello world";

            var ce = new AnotherFakeEvent();

            ce.EventType          = "com.example.someevent";
            ce.CloudEventsVersion = "0.1";
            ce.Source             = (new Uri("http://localhost")).ToString();
            ce.EventId            = Guid.NewGuid().ToString();
            ce.ContentType        = contentType;
            ce.Data = data;

            var content = CloudEventContentFactory.Create(ce);

            content.Should().BeOfType <BinaryCloudEventContent <string> >();
        }
        public void Given_CloudEvent_Should_Return_StructuredCloudEventContent()
        {
            var contentType = "application/json";
            var data        = new FakeData()
            {
                FakeProperty = "hello world"
            };

            var ce = new TheOtherFakeEvent();

            ce.EventType          = "com.example.someevent";
            ce.CloudEventsVersion = "0.1";
            ce.Source             = (new Uri("http://localhost")).ToString();
            ce.EventId            = Guid.NewGuid().ToString();
            ce.ContentType        = contentType;
            ce.Data = data;

            var content = CloudEventContentFactory.Create(ce);

            content.Should().BeOfType <StructuredCloudEventContent <FakeData> >();
        }