当前位置: 首页>>代码示例>>C#>>正文


C# ServiceCollection.WithHostEncoding方法代码示例

本文整理汇总了C#中ServiceCollection.WithHostEncoding方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.WithHostEncoding方法的具体用法?C# ServiceCollection.WithHostEncoding怎么用?C# ServiceCollection.WithHostEncoding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ServiceCollection的用法示例。


在下文中一共展示了ServiceCollection.WithHostEncoding方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ServiceCollectionWithHostEncoding

 public void ServiceCollectionWithHostEncoding()
 {
     ServiceCollection services = new ServiceCollection();
     int count = services.Pipeline.Encodings.Count;
     services.WithHostEncoding(new GzipDeflateEncoding());
     Assert.AreEqual(count + 1, services.Pipeline.Encodings.Count);
 }
开发者ID:ChadBurggraf,项目名称:small-fry,代码行数:7,代码来源:ServiceCollectionTests.cs

示例2: ResolvedServiceGetRequestDecoder

        public void ResolvedServiceGetRequestDecoder()
        {
            ServiceCollection services = new ServiceCollection();
            services
                .WithService("Test", "/")
                    .WithEndpoint("{action}")
                        .Post<Payload>((Payload p) => { });

            ResolvedService service = new ServiceResolver(services).Find(MethodType.Post, "foo");
            EncodingLookupResult result = service.GetRequestDecoder("gzip");
            Assert.IsNull(result);

            result = service.GetRequestDecoder(string.Empty);
            Assert.IsNotNull(result);
            Assert.AreEqual(EncodingType.Empty, result.EncodingType);
            Assert.AreEqual(new IdentityEncoding(), result.Encoding);

            services.WithHostEncoding(new GzipDeflateEncoding());
            service = new ServiceResolver(services).Find(MethodType.Post, "foo");
            result = service.GetRequestDecoder("gzip");
            Assert.IsNotNull(result);
            Assert.AreEqual(EncodingType.Parse("gzip"), result.EncodingType);
            Assert.AreEqual(new GzipDeflateEncoding(), result.Encoding);
        }
开发者ID:ChadBurggraf,项目名称:small-fry,代码行数:24,代码来源:ResolvedServiceTests.cs

示例3: ResolvedServiceReadRequestEncoded

        public void ResolvedServiceReadRequestEncoded()
        {
            ServiceCollection services = new ServiceCollection();
            services
                .WithHostEncoding(new GzipDeflateEncoding())
                .WithHostFormat(new JsonFormat())
                .WithService("Test", "/")
                    .WithEndpoint("{action}")
                        .Post<Payload>((Payload p) => { });

            byte[] encodedPayload;

            using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"date\":\"2012-09-22T18:46:00Z\",\"number\":42,\"text\":\"Hello, world!\"}")))
            {
                using (MemoryStream outputStream = new MemoryStream())
                {
                    using (GZipStream compressionStream = new GZipStream(outputStream, CompressionMode.Compress))
                    {
                        stream.CopyTo(compressionStream);
                    }

                    encodedPayload = outputStream.ToArray();
                }
            }

            ResolvedService service = new ServiceResolver(services).Find(MethodType.Post, "foo");

            using (RequestMessage request = new RequestMessage<Payload>(service.Name, service.RouteValues, new Uri("http://example.com/foo")))
            {
                request.InputStream.Write(encodedPayload, 0, encodedPayload.Length);
                request.InputStream.Position = 0;
                request.SetEncodingFilter(EncodingType.Parse("gzip"), new GzipDeflateEncoding());

                using (ResponseMessage response = new ResponseMessage())
                {
                    ReadRequestResult result = service.ReadRequest(request, encodedPayload.Length, "gzip", "application/json", request.InputStream);
                    Assert.IsNotNull(result);
                    Assert.IsTrue(result.Success);
                    Assert.IsNull(result.Exception);
                    Assert.AreEqual(StatusCode.None, result.StatusCode);
                    Assert.IsNotNull(result.RequestObject);

                    Payload payload = result.RequestObject as Payload;
                    Assert.IsNotNull(payload);
                    Assert.AreEqual(new DateTime(2012, 9, 22, 18, 46, 0, DateTimeKind.Utc), payload.Date);
                    Assert.AreEqual(42L, payload.Number);
                    Assert.AreEqual("Hello, world!", payload.Text);
                }
            }
        }
开发者ID:ChadBurggraf,项目名称:small-fry,代码行数:50,代码来源:ResolvedServiceTests.cs


注:本文中的ServiceCollection.WithHostEncoding方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。