Arun Rana
Arun Rana

Reputation: 8606

Convert service from WCF to ServiceStack Framework

I have developed one WCF application, and it is working as a middle layer between the database and my web application. Now my client wants to transfer this WCF to REST-based using ServiceStack.

I have looked around it on GitHub and tried to build a demo. I have created a start up template using NuGet, so it includes a Hello & Todo example.

  1. How can I transfer my logic as object based (DTO)? Because most of the functions I have with different parameters and return the result as a dataset.

  2. How can I make a client in C#? And which reference do I need to add?

  3. When I hosted sample application on IIS after adding the startup template using NuGet, I could not able to find any resources. Is there a specific setting I need to do when I need to host it on IIS?

Upvotes: 3

Views: 2737

Answers (1)

mythz
mythz

Reputation: 143399

If you haven't already done so go through the Creating REST Services with ServiceStack presentation.

1) If you've seen ServiceStack's Hello World example it shows you that the only steps needed to do to create a web service is to just provide:

//1. A Request DTO
public class Hello : IReturn<HelloResponse> { 
    public string Name { get; set; }
}

//2. A Response DTO
public class HelloResponse {
    public string Result { get; set; }
}

//3. The web service implementation that takes a Request DTO and returns a Response DTO
public class HelloService : Service
{
    public object Any(Hello request)
    {
        return new HelloResponse { Result = "Hello, " + request.Name };
    }
}

The above example shows all the code needed to create the Hello web service. You should be able to re-use a lot of your existing type and logic from your WCF method and just copy it in the Any() method.

2) One of the benefits of ServiceStack is that you don't need to add a ServiceReference, i.e. you can re-use the same generic Service Client and your DTOs for all your web services. e.g:

//Using JSON:
IServiceClient client = new JsonServiceClient("http://localhost/path/to/servicestack");

//Using XML:
IServiceClient client = new XmlServiceClient("http://localhost/path/to/servicestack");

var response = client.Send(new Hello { Name = "Arun" });
Console.WriteLine("Received: " + response.Result); 

On the /metadata page there is also a link to your webservices WSDL where you could create generated service clients should you wish. However this is not the recommended approach since it requires much more friction then just using your existing DTOs.

3) ServiceStack Web Services are already an ASP.NET application, i.e. ServiceStack is just a set of IHttpHandler's you can configure to run inside of a normal ASP.NET or MVC web application by adding a Web.config mapping to your web applications Web.config.

Basically you can treat a ServiceStack web service as a normal ASP.NET web application, in fact the Hello World Tutorial shows you how to do this from creating an empty ASP.NET application.

You may also be interested in checking out The Starter Templates example projects which shows you the minimum about of setup required to configure ServiceStack to run in a variety of different hosting options, i.e. ASP.NET / Windows Service / Console Application, etc.

Upvotes: 6

Related Questions