コード例 #1
0
        public void DeserializeEvent()
        {
            var json = "{\"CustomerId\":{\"Id\":\"9154d569-0bf3-4c37-b588-ed49765c45dc\"},\"EventId\":\"3907f8bd-6b4b-4825-8a7c-b63800e7f28f\",\"AggregateId\":{\"Id\":\"23934182-ea29-4c99-8b4a-845c4b152153\"},\"AggregateVersion\":-1}";

            var contractResolver            = new PrivateSetterContractResolver();
            JsonSerializerSettings settings = new JsonSerializerSettings {
                ContractResolver = contractResolver
            };
            var type = Type.GetType("EventSourcingCQRS.Domain.CartModule.CartCreatedEvent, EventSourcingCQRS.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");

            var @event = JsonConvert.DeserializeObject(json, type, settings);

            Assert.IsType <CartCreatedEvent>(@event);
            Assert.Equal(new CustomerId("9154d569-0bf3-4c37-b588-ed49765c45dc"), ((CartCreatedEvent)@event).CustomerId);
            Assert.Equal(new CartId("23934182-ea29-4c99-8b4a-845c4b152153"), ((CartCreatedEvent)@event).AggregateId);
            Assert.Equal(new Guid("3907f8bd-6b4b-4825-8a7c-b63800e7f28f"), ((CartCreatedEvent)@event).EventId);
            Assert.Equal(-1, ((CartCreatedEvent)@event).AggregateVersion);
        }
コード例 #2
0
        /// <summary>
        /// This method asynchronously performs the Http Request to the Trustev API
        /// </summary>
        /// <typeparam name="T">The Type of the return object</typeparam>
        /// <param name="uri">The HttpMethod Uri</param>
        /// <param name="method">The Http Method</param>
        /// <param name="entity">The relevant entity</param>
        /// <param name="isAuthenticationNeeded">Does this api call require the X-Authorization header</param>
        /// <param name="requestTimeout">The timeout value of this http request in milliseconds</param>
        /// <returns></returns>
        private static async Task <T> PerformHttpCallAsync <T>(string uri, HttpMethod method, object entity, bool isAuthenticationNeeded = true, int requestTimeout = 15000)
        {
            JsonSerializerSettings  jss = new JsonSerializerSettings();
            DefaultContractResolver dcr = new PrivateSetterContractResolver();

            jss.ContractResolver = dcr;

            HttpClient client = new HttpClient();

            if (isAuthenticationNeeded)
            {
                if (uri.Contains("/session"))
                {
                    if (string.IsNullOrEmpty(PublicKey))
                    {
                        throw new TrustevGeneralException("You need to set your public key if you want to post a Session. This can be done via the SetUp method");
                    }

                    client.DefaultRequestHeaders.Add("X-PublicKey", PublicKey);
                }
                else
                {
                    client.DefaultRequestHeaders.Add("X-Authorization", UserName + " " + await GetTokenAsync());
                }
            }

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            client.Timeout = new TimeSpan(requestTimeout * TimeSpan.TicksPerMillisecond);

            HttpResponseMessage response = new HttpResponseMessage();

            string json = string.Empty;

            if (entity != null && entity.GetType() != typeof(string))
            {
                json = JsonConvert.SerializeObject(entity, jss);
            }
            else
            {
                json = (string)entity;
            }

            if (method == HttpMethod.Post)
            {
                response = await client.PostAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"));
            }
            else if (method == HttpMethod.Put)
            {
                response = await client.PutAsync(uri, new StringContent(json, Encoding.UTF8, "application/json"));
            }
            else if (method == HttpMethod.Get)
            {
                response = await client.GetAsync(uri);
            }

            string resultstring = string.Empty;

            if (response.IsSuccessStatusCode)
            {
                resultstring = await response.Content.ReadAsStringAsync();
            }
            else
            {
                resultstring = await response.Content.ReadAsStringAsync();

                throw new TrustevHttpException(response.StatusCode, resultstring);
            }

            return(JsonConvert.DeserializeObject <T>(resultstring, jss));
        }