如果您还没有这样做,请阅读使用 ServiceStack 创建 REST 服务演示文稿。
1) 如果您看过 ServiceStack 的Hello World示例,它会告诉您创建 Web 服务所需要做的唯一步骤是提供:
//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 };
}
}
上面的示例显示了创建Hello Web 服务所需的所有代码。您应该能够重用 WCF 方法中的大量现有类型和逻辑,并将其复制到Any()方法中。
2) ServiceStack 的好处之一是您不需要添加ServiceReference,即您可以为所有Web 服务重用相同的通用服务客户端和DTO。例如:
//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);
在/metadata页面上,还有一个指向您的 Web服务WSDL的链接,您可以根据需要在其中创建生成的服务客户端。但是,这不是推荐的方法,因为它比仅使用现有的 DTO 需要更多的摩擦。
3) ServiceStack Web 服务已经是一个 ASP.NET 应用程序,即 ServiceStack 只是一组 IHttpHandler,您可以通过将Web.config 映射添加到您的 Web 应用程序 Web 来配置为在普通 ASP.NET 或 MVC Web 应用程序内部运行。配置。
基本上,您可以将 ServiceStack Web 服务视为普通的 ASP.NET Web 应用程序,实际上Hello World 教程向您展示了如何通过创建一个空的 ASP.NET 应用程序来做到这一点。
您可能也有兴趣查看Starter Templates示例项目,该项目向您展示了配置 ServiceStack 以在各种不同的托管选项中运行所需的最少设置,即 ASP.NET / Windows 服务 / 控制台应用程序等。