-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathGlobal.asax.cs
135 lines (115 loc) · 3.99 KB
/
Global.asax.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
128
129
130
131
132
133
134
135
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using Funq;
using ServiceStack;
namespace HelloWorld
{
//Configure AppHost
public class AppHost : AppHostBase
{
public AppHost() : base("Hello ServiceStack", typeof(Hello).Assembly) {}
public override void Configure(Container container) {}
}
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
}
//Define Request and Response DTOs
[Route("/hellotext/{Name}")]
public class HelloText
{
public string Name { get; set; }
}
[Route("/hellohtml/{Name}")]
public class HelloHtml
{
public string Name { get; set; }
}
[Route("/hellofile")]
public class HelloFile { }
[Route("/helloimage/{Name}")]
public class HelloImage
{
public string Name { get; set; }
public int? Width { get; set; }
public int? Height { get; set; }
public int? FontSize { get; set; }
public string Foreground { get; set; }
public string Background { get; set; }
}
[Route("/hello/{Name}")]
public class Hello : IReturn<HelloResponse>
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
//Implementation
public class HelloService : Service
{
[AddHeader(ContentType = MimeTypes.Html)]
public object Get(HelloHtml request)
{
return "<h1>Hello, HTML!</h1>"
+ "<div><a href='{0}'>Hello File</a></div>".Fmt(new HelloFile().ToGetUrl())
+ "<div><a href='{0}'>Hello Image</a></div>".Fmt(new HelloImage
{
Name = "World",
Background = "Blue",
Foreground = "Yellow",
FontSize = 48,
Height = 600,
Width = 800,
}.ToGetUrl());
}
[AddHeader(ContentType = "text/plain")]
public object Get(HelloText request)
{
return "<h1>Hello, {0}!</h1>".Fmt(request.Name);
}
public object Get(HelloFile request)
{
byte[] imageBytes = "https://www.google.com/images/srpr/logo11w.png".GetBytesFromUrl();
return new HttpResult(imageBytes, "image/png");
}
public object Get(HelloImage request)
{
var width = request.Width.GetValueOrDefault(640);
var height = request.Height.GetValueOrDefault(360);
var bgColor = request.Background != null ? Color.FromName(request.Background) : Color.ForestGreen;
var fgColor = request.Foreground != null ? Color.FromName(request.Foreground) : Color.White;
var image = new Bitmap(width, height);
using (var g = Graphics.FromImage(image))
{
g.Clear(bgColor);
var drawString = "Hello, {0}!".Fmt(request.Name);
var drawFont = new Font("Times", request.FontSize.GetValueOrDefault(40));
var drawBrush = new SolidBrush(fgColor);
var drawRect = new RectangleF(0, 0, width, height);
var drawFormat = new StringFormat
{
LineAlignment = StringAlignment.Center,
Alignment = StringAlignment.Center
};
g.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
base.Response.ContentType = "image/png";
var ms = new MemoryStream();
image.Save(ms, ImageFormat.Png);
return ms;
}
}
public object Get(Hello request)
{
return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) };
//C# client can call with:
//var response = client.Get(new Hello { Name = "ServiceStack" });
}
}
}