-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonValueDemo.cs
127 lines (116 loc) · 4.78 KB
/
JsonValueDemo.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Json;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Microsoft.ApplicationServer.Http;
using Microsoft.ApplicationServer.Http.Description;
using Microsoft.ApplicationServer.Http.Dispatcher;
namespace WcfWebApi.Preview5.Explorations.SelfHostedDemos
{
public class Model
{
public string x { get; set; }
public int y { get; set; }
public double z { get; set; }
}
class JsonValueDemo
{
[ServiceContract]
class TheService
{
[WebInvoke(Method="post",UriTemplate = "")]
public HttpResponseMessage Post(ObjectContent<Model> c)
{
Model m = c.ReadAsAsync().Result;
Console.WriteLine("x = {0}, y = {1}, z = {2}",m.x,m.y,m.z);
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
};
}
[WebInvoke(Method = "post", UriTemplate = "v2")]
[JsonExtract]
public HttpResponseMessage Post2(HttpRequestMessage<JsonValue> req, string x, int y, double z)
{
Console.WriteLine("x = {0}, y = {1}, z = {2}", x, y, z);
return new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK
};
}
}
public static void Run()
{
var config = new HttpConfiguration();
config.RequestHandlers += (coll, ep, desc) =>
{
if (
desc.Attributes.Any(a => a.GetType() == typeof(JsonExtractAttribute))
)
{
coll.Add(new JsonExtractHandler(desc));
}
};
using (var sh = new HttpServiceHost(typeof(TheService), config, "http://localhost:8080"))
{
sh.Open();
Console.WriteLine("host is opened");
var client = new HttpClient();
dynamic data = new JsonObject();
data.x = "a string";
data.y = "13";
data.z = "3.14";
var resp = client.PostAsync("http://localhost:8080/v2", new ObjectContent<JsonValue>(data, "application/json")).Result;
Console.WriteLine(resp.StatusCode);
}
}
}
internal class JsonExtractAttribute : Attribute
{
}
internal class JsonExtractHandler : HttpOperationHandler
{
private readonly HttpParameter[] _prms;
public JsonExtractHandler(HttpOperationDescription desc)
{
_prms =
desc.InputParameters.Where(
p => p.ParameterType.IsPrimitive
|| p.ParameterType == typeof(string)
).ToArray();
}
protected override IEnumerable<HttpParameter> OnGetInputParameters()
{
yield return new HttpParameter("request", typeof(HttpRequestMessage<JsonValue>));
}
protected override IEnumerable<HttpParameter> OnGetOutputParameters()
{
return _prms;
}
protected override object[] OnHandle(object[] input)
{
var req = input[0] as HttpRequestMessage<JsonValue>;
var value = req.Content.ReadAsAsync().Result;
var output = new object[_prms.Length];
for (int i = 0; i < _prms.Length; ++i)
{
JsonValue m = value.GetValue(_prms[i].Name);
output[i] = m.ReadAs(_prms[i].ParameterType, null);
if (output[i] == null && _prms[i].ParameterType.IsValueType)
throw new HttpResponseException(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.BadRequest,
Content =
new StringContent(string.Format("{0} is required",
_prms[i].Name))
});
}
return output;
}
}
}