Add ServiceStack components (ServiceStack.dll, ServiceStack.Interfaces.dll):
Get the DLLs at: https://github.com/ServiceStack/ServiceStack/downloads
Add this line to Web.config's httpHandlers section:
Add this line to Web.config's httpHandlers section:
<add path="yourServicestack*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" />
Put this inside configuration section:
<location path="servicestack"> <system.web> <httpHandlers> <add path="*" type="ServiceStack.WebHost.Endpoints.ServiceStackHttpHandlerFactory, ServiceStack" verb="*" /> </httpHandlers> </system.web> </location>
Add these lines on RegisterRoutes:
routes.IgnoreRoute ("yourServicestack"); routes.IgnoreRoute ("yourServicestack/{*pathInfo}");
Add these Request,Response,Service classes in your Models directory:
class Hello { public string Name { get; set; } } class HelloResponse { public string Result { get; set; } } class HelloService : ServiceStack.ServiceHost.IService<Hello> { public object Execute(Hello request) { return new HelloResponse { Result = "Good morning " + request.Name + "!" }; } }
And add these other Request,Response,Service classes in Models directory too:
class Subtraction { public decimal Minuend { get; set; } public decimal Subtrahend { get; set; } } class SubtractionResponse { public decimal Difference { get; set; } } class SubtractionService : ServiceStack.ServiceHost.IService<Subtraction> { public object Execute(Subtraction request) { return new SubtractionResponse { Difference = request.Minuend - request.Subtrahend }; } }
Add these code in Global.asax.cs:
protected void Application_Start () { RegisterRoutes (RouteTable.Routes); new NextBillionAppHost().Init(); } public class NextBillionAppHost : ServiceStack.WebHost.Endpoints.AppHostBase { //Tell Service Stack the name of your application and where to find your web services public NextBillionAppHost() : base("Billionaire Web Services", typeof(DemoServiceStack.Models.SubtractionService).Assembly) { } public override void Configure(Funq.Container container) { //register user-defined REST-ful urls Routes .Add<DemoServiceStack.Models.Hello>("/como-esta") .Add<DemoServiceStack.Models.Hello>("/como-esta/{Name}"); Routes .Add<DemoServiceStack.Models.Subtraction>("/solve-subtraction") .Add<DemoServiceStack.Models.Subtraction>("/solve-subtraction/{Minuend}/{Subtrahend}"); } }
Then run (shortcut key: command+option+enter), then type in address bar:
http://127.0.0.1:8080/yourServicestack
,then you shall see this:
http://127.0.0.1:8080/yourServicestack
,then you shall see this:
Then under Operations, click the JSON tag of Subtraction operation, you shall see this:
Then type this url in the address bar:
http://127.0.0.1:8080/yourServicestack/solve-subtraction/2011/1955
You shall see this:
http://127.0.0.1:8080/yourServicestack/solve-subtraction/2011/1955
You shall see this:
Then try to click the JSON, XML, etc, see the output.
You can use ServiceStack instead of WCF. REST-ful services is easier with ServiceStack. ServiceStack has a good programming model, i.e. the Request, Response and their Service has good cohesion
No comments:
Post a Comment