In my last post I outlined the creation of a ServiceStack web service, pretty much as per the current documentation on the ServiceStack website.
I’m now going to create a bare minimum service from scratch (i.e. not using the ServiceStack project templates). The reason for this is that I want to deploy a service to an old server box which doesn’t support .NET greater than 4.0. Out of the box the templates don’t work with older versions of .NET.
This post will concentrate on a .NET 4.0 compatible ServiceStack, but the same steps can be applied to a bare minimum .NET 4.6 service as well – obviously just change the .NET framework to suit.
We’ll recreate the Hello World implementation as per the ServiceStack templates in this example.
Note: I’m not aiming to create a “best practices” example of code, this literally will be the bare minimum set of code and projects to implement the demo Hello World service.
- Create a ASP.NET Empty Web Application, setting the .NET framework to .NET 4 – my project is named TestStack
- Using NuGet, install the ServiceStack packages – use the 4.0.24 versions. This version is required because of a PCL issue with subsequent versions supporting .NET 4.0 (see ServiceStack NuGet update 4.0.22 to 4.0.31 caused errors on deployment)
- Add a C# class, named AppHost and include the following code
using Funq; using ServiceStack; public class AppHost : AppHostBase { public AppHost() : base("TestStack", typeof(MyServices).Assembly) { } public override void Configure(Container container) { } }
- Now add another Class named MyServices and add the following code
using ServiceStack; public class MyServices : Service { public object Any(Hello request) { return new HelloResponse { Result = "Hello, {0}!".Fmt(request.Name) }; } }
- Add another Class name Hello and add the following code
using ServiceStack; [Route("/hello/{Name}")] public class Hello : IReturn<HelloResponse> { public string Name { get; set; } }
- Now add the last class, which is named HelloResponse and add the following code
public class HelloResponse { public string Result { get; set; } }
- Now add a Global Application Class, keep it named as Global and in the code behind, remove the existing class and methids and replace with the following
public class Global : HttpApplication { protected void Application_Start(object sender, EventArgs e) { new AppHost().Init(); } }
- Finally we need to alter the Web.config to look like this
<configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpHandlers> <add path="*" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" /> </httpHandlers> <pages controlRenderingCompatibilityVersion="4.0" /> </system.web> <system.webServer> <modules runAllManagedModulesForAllRequests="true" /> <validation validateIntegratedModeConfiguration="false" /> <urlCompression doStaticCompression="true" doDynamicCompression="false" /> <handlers> <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" /> </handlers> </system.webServer> </configuration>
Run the application via Visual Studio and you should see the metadata page showing the Hello operation. To double check the operation works simply change the URL in your preferred browser to
http://localhost:49810/Hello/World
Obviously change the port to the one supplied by iisexpress.