3

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arun Rana
  • 8,426
  • 14
  • 67
  • 107

1 Answers1

6

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.

mythz
  • 141,670
  • 29
  • 246
  • 390
  • this is solid answer and great framwork you guys have built, one query is if i need to consumer it from another web application i need to add RestIntro.ServiceModel reference, right? and how can i convert that response in to json format? – Arun Rana Nov 03 '11 at 09:01
  • What i need to do is how can i consume this service without depending on ServiceModel (here customer class) ?? – Arun Rana Nov 03 '11 at 09:38
  • @ArunRana The same Dtos you use to develop your web service with can be used for all of ServiceStack's serializers/formats which is what the JsonServiceClient does for you. – mythz Nov 03 '11 at 13:41
  • @ArunRana what ServiceModel customer class? do you mean WCF ServiceModel or your own DTOs? If you don't want to depend on your own DTOs than you can use your web services XSDs (on the /metadata page, e.g http://www.servicestack.net/ServiceStack.Hello/servicestack/metadata?xsd=1) to generate C# classes with the XSD.exe utility. But honestly that just creates a copy of your ServiceModel assembly on the client so it's an extra step/friction point that I avoid doing by just copying the dll over. – mythz Nov 03 '11 at 13:45
  • @ArunRana if you're consuming ServiceStack web services via a web site than you don't need any javascript libraries as you can just use jQuery's $.getJSON / $.post ajax classes. All http://www.servicestack.net online examples do this. – mythz Nov 03 '11 at 13:47
  • thanks for your reply, i got it your idea let me think and do some work on this and will let you know about that, using jquery is little bit confuse me because not that much knowledge about that :) – Arun Rana Nov 04 '11 at 06:58
  • i have posted another question http://stackoverflow.com/questions/8007232/need-help-on-servicestack-implementation can you please answer of that.. – Arun Rana Nov 04 '11 at 09:36