Permalink
Switch branches/tags
Nothing to show
Find file
c143991 Mar 6, 2014
135 lines (115 sloc) 3.99 KB
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" });
}
}
}