本文整理汇总了C#中ServiceCollection.WithHostFormat方法的典型用法代码示例。如果您正苦于以下问题:C# ServiceCollection.WithHostFormat方法的具体用法?C# ServiceCollection.WithHostFormat怎么用?C# ServiceCollection.WithHostFormat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ServiceCollection
的用法示例。
在下文中一共展示了ServiceCollection.WithHostFormat方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ServiceCollectionWithHostFormat
public void ServiceCollectionWithHostFormat()
{
ServiceCollection services = new ServiceCollection();
int count = services.Pipeline.Formats.Count;
services.WithHostFormat(new PlainTextFormat());
Assert.AreEqual(count + 1, services.Pipeline.Formats.Count);
}
示例2: ResolvedServiceGetRequestDeserializer
public void ResolvedServiceGetRequestDeserializer()
{
ServiceCollection services = new ServiceCollection();
services
.WithService("Test", "/")
.WithEndpoint("{action}")
.Post<Payload>((Payload p) => { });
ResolvedService service = new ServiceResolver(services).Find(MethodType.Post, "foo");
FormatLookupResult result = service.GetRequestDeserializer("application/json");
Assert.IsNull(result);
result = service.GetRequestDeserializer(string.Empty);
Assert.IsNotNull(result);
Assert.AreEqual(MediaType.Empty, result.MediaType);
Assert.AreEqual(new PlainTextFormat(), result.Format);
services.WithHostFormat(new JsonFormat());
service = new ServiceResolver(services).Find(MethodType.Post, "foo");
result = service.GetRequestDeserializer("application/json");
Assert.IsNotNull(result);
Assert.AreEqual(MediaType.Parse("application/json"), result.MediaType);
Assert.AreEqual(new JsonFormat(), result.Format);
}
示例3: ResolvedServiceWriteResponse
public void ResolvedServiceWriteResponse()
{
Payload payload = new Payload()
{
Date = new DateTime(2012, 9, 22, 18, 46, 0, DateTimeKind.Utc),
Number = 42,
Text = "Hello, world!"
};
ServiceCollection services = new ServiceCollection();
services
.WithHostFormat(new JsonFormat())
.WithService("Test", "/")
.WithEndpoint("{action}")
.Post<Payload>((Payload p) => { });
using (ResponseMessage response = new ResponseMessage())
{
response.ResponseObject = payload;
ResolvedService service = new ServiceResolver(services).Find(MethodType.Post, "foo");
WriteResponseResult result = service.WriteResponse(response, "gzip, *", "application/json, */*");
Assert.IsNotNull(result);
Assert.IsNull(result.Exception);
Assert.AreEqual(StatusCode.None, result.StatusCode);
Assert.IsTrue(result.Success);
response.OutputStream.Position = 0;
using (StreamReader reader = new StreamReader(response.OutputStream))
{
Assert.AreEqual("{\"date\":\"2012-09-22T18:46:00.0000000Z\",\"number\":42,\"text\":\"Hello, world!\"}", reader.ReadToEnd());
}
}
}
示例4: ResolvedServiceReadRequestMissingEncoding
public void ResolvedServiceReadRequestMissingEncoding()
{
ServiceCollection services = new ServiceCollection();
services
.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());
ReadRequestResult result = service.ReadRequest(request, encodedPayload.Length, "gzip", "application/json", request.InputStream);
Assert.IsNotNull(result);
Assert.IsFalse(result.Success);
Assert.IsNull(result.Exception);
Assert.AreEqual(StatusCode.UnsupportedMediaType, result.StatusCode);
Assert.IsNull(result.RequestObject);
}
}
示例5: ResolvedServiceReadRequestInvalidJson
public void ResolvedServiceReadRequestInvalidJson()
{
ServiceCollection services = new ServiceCollection();
services
.WithHostFormat(new JsonFormat())
.WithService("Test", "/")
.WithEndpoint("{action}")
.Post<Payload>((Payload p) => { });
ResolvedService service = new ServiceResolver(services).Find(MethodType.Post, "foo");
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("this is not JSON")))
{
using (RequestMessage request = new RequestMessage<Payload>(service.Name, service.RouteValues, new Uri("http://example.com/foo")))
{
ReadRequestResult result = service.ReadRequest(request, (int)stream.Length, null, "application/json", stream);
Assert.IsNotNull(result);
Assert.IsFalse(result.Success);
Assert.IsNotNull(result.Exception);
Assert.AreEqual(StatusCode.BadRequest, result.StatusCode);
Assert.IsNull(result.RequestObject);
}
}
}
示例6: ResolvedServiceReadRequest
public void ResolvedServiceReadRequest()
{
ServiceCollection services = new ServiceCollection();
services
.WithHostFormat(new JsonFormat())
.WithService("Test", "/")
.WithEndpoint("{action}")
.Get(() => { })
.Post<Payload>((Payload p) => { });
ResolvedService service = new ServiceResolver(services).Find(MethodType.Get, "foo");
ReadRequestResult result;
using (RequestMessage request = new RequestMessage(service.Name, service.RouteValues, new Uri("http://example.com/foo")))
{
result = service.ReadRequest(request, 0, null, null, null);
Assert.IsNotNull(result);
Assert.IsTrue(result.Success);
Assert.IsNull(result.Exception);
Assert.AreEqual(StatusCode.None, result.StatusCode);
Assert.IsNull(result.RequestObject);
}
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes("{\"date\":\"2012-09-22T18:46:00Z\",\"number\":42,\"text\":\"Hello, world!\"}")))
{
using (RequestMessage request = new RequestMessage<Payload>(service.Name, service.RouteValues, new Uri("http://example.com/foo")))
{
service = new ServiceResolver(services).Find(MethodType.Post, "foo");
result = service.ReadRequest(request, (int)stream.Length, null, "application/json", stream);
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);
}
}
}