c#中使用ServiceStack创建服务简单示例


http://www.cnblogs.com/yangecnu/p/Introduce-ServiceStack.html(转)
http://www.cnblogs.com/woxpp/p/5012947.html(转)


--------------------------------------例子--------------------------------
//在NuGet安装 ServiceStack包
//packages.confing文件有每一个ServiceStack dll 的版本和运行时信息。
//ServiceStack有三种请求方法,Get、POST、Any(post/get)
//它以请求对象(DTO)的名称来区分调用的方法

namespace ServiceStack1
{
    /// 
    /// 创建DTO响应实体
    /// 
    public class TicketResponse
    {
        /// 
        /// id
        /// 
        public int TicketId { get; set; }
        public int TableNumber { get; set; }
        public int ServerId { get; set; }
        public DateTime Timestamp { get; set; }
        public List Orders { get; set; }
    }

    public class OrderResponse
    {
        public int OrderId { get; set; }
    }
}



namespace ServiceStack1
{
    /// 
    /// 实现接口方法
    /// 
    public class TicketService:ServiceStack.Service
    {
        /// 
        /// 返回集合
        /// 
        /// 
        /// 
        public List Any(TicketRequest request)
        {
            List result = new List();
            result.Add(new TicketResponse()

            {
                ServerId = 1,
                TableNumber = 1,
                TicketId = 1,
                Timestamp = DateTime.Now
            });
            result.Add(new TicketResponse()
            {
                ServerId = 2,
                TableNumber = 2,
                TicketId = 2,
                Timestamp = DateTime.Now
            });
            return result;
        }

        public string POST(TicketDeleteRequest deleteid)
        {
            return "true";
        }
    }
}



namespace ServiceStack1
{
    /// 
    /// 创建DTO请求实体
    /// 
    public class TicketRequest:ServiceStack.IReturn
    {
        /// 
        /// id
        /// 
        public int TicketId { get; set; }
        public int TableNumber { get; set; }
        public int ServerId { get; set; }
    }

    public class TicketDeleteRequest
    {
        public int TicketId { get; set; }
    }
}




namespace ServiceStack1
{
    /// 
    /// 实现宿主服务
    /// 
    public class TicketHost : ServiceStack.AppHostBase
    {
        public TicketHost() : base("Service1", typeof(TicketService).Assembly)
        {
        }

        public override void Configure(Container container)
        {
           // throw new NotImplementedException();
        }
    }
}



namespace ServiceStack1
{
    public class Global : System.Web.HttpApplication
    {

        public void Application_Start(object sender, EventArgs e)
        {
            //启动ServiceStack服务宿主
            new TicketHost().Init();
        }

    }
}




 "1.0" encoding="utf-8"?>


  
    "true" targetFramework="4.5.2"/>
    "4.5.2"/>
  


  
  
    "false"/>
    
      "*.aspx" name="DefaultHttpApplication" type="System.Web.UI.PageHandlerFactory" verb="*"/>
      "*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode"
        resourceType="Unspecified" allowPathInfo="true"/>
    
  




namespace ServiceStack1
{
    public partial class index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //SS中,客户端连接到服务端的方式主要有两种,通过REST方式或者Client方式,初始化客户端是相同的


            //在浏览器上请求TicketRequest方法的方式
            //http://localhost:11688//json/reply/TicketRequest

            //初始化客户端,采用json数据格式
            ServiceStack.JsonServiceClient service = new ServiceStack.JsonServiceClient("http://localhost:11688/");


            //通过REST方式访问服务
            List rest = service.Post>(new TicketRequest() { TicketId = 1, TableNumber = 2 });


            bool responseDelete = service.Post<bool>(new TicketDeleteRequest() { TicketId = 1 });


            //通过Client方式访问服务
            //TicketResponse client =service.Send(new TicketRequest()
            //{
            //    TicketId = 2,
            //    TableNumber=2
            //});
        }
    }
}

你可能感兴趣的:(web,service)