zoukankan      html  css  js  c++  java
  • c# ServiceStack web 搭建

    用的是4.5的.net版本

    构建model

        /// <summary>
        /// 通过id获取资料
        /// </summary>
        //[Route("/GetStudentInfo", "POST")]
        public class StudentInfoRequest : IReturn<StudentInfo>
        {
            public int id { get; set; }
        }
    
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        [Route("/GetAllStudentInfo", "POST")]
        public class GetAllStudentInfoRequest : IReturn<StudentInfo>
        {
    
        }
    
       public class StudentInfo
        {
            public int id { get; set; }
            public string name { get; set; }
            public int age { get; set; }
            public string grade { get; set; }
        }
    

     添加mvc web应用,创建service

    public interface IStudentService
        {
            int Post(StudentInfo model);
            StudentInfo Get(StudentInfoRequest request);
            bool Delete(StudentInfoRequest request);
            List<StudentInfo> Any(GetAllStudentInfoRequest request);
            bool Put(StudentInfo model);
        }
    
    
     public class StudentService : Service, IStudentService
        {
            private static List<StudentInfo> data = new List<StudentInfo>();
    
            public int Post(StudentInfo model)
            {
                if (data.Count <= 0)
                {
                    model.id = 1;
                    data.Add(model);
                }
                else
                {
                    model.id = data.Select(it => it.id).Max() + 1;
                    data.Add(model);
                }
                return model.id;
            }
    
            public bool Delete(StudentInfoRequest request)
            {
                var selectData = data.Where(it => it.id == request.id).FirstOrDefault();
                if (selectData == null)
                {
                    return false;
                }
                else
                {
                    data.Remove(selectData);
                    return true;
                }
            }
    
            public StudentInfo Get(StudentInfoRequest request)
            {
                return data.Where(it => it.id == request.id).FirstOrDefault();
            }
    
            public List<StudentInfo> Any(GetAllStudentInfoRequest request)
            {
                return data;
            }
    
            public bool Put(StudentInfo model)
            {
                var selectData = data.Where(it => it.id == model.id).FirstOrDefault();
                if (selectData == null)
                {
                    data.Add(model);
                }
                else
                {
                    data.Remove(selectData);
                    data.Add(model);
                }
                return true;
            }
        }

    web全局文件配置

    protected void Application_Start()
            {
                new APIServiceHost().Init();
            }
    
            public class APIServiceHost : AppHostBase
            {
                //Register your web service with ServiceStack.
                public APIServiceHost()
                    : base("API服务", typeof(APIServiceHost).Assembly)
                {
                    //Routes.Add(typeof(int), "/Student", "POST", "添加学生信息", "");
                    //Routes.Add(typeof(bool), "/Student", "DELETE", "删除学生信息", "");
                    //Routes.Add<StudentInfo>("/Student", "GET");
                    //Routes.Add<List<StudentInfo>>("/Student", "GET");
                    //Routes.Add<bool>("/Student", "PUT");
    
                    //Routes.Add<int>("/Grade", "POST")
                    //      .Add<bool>("/Grade/{Id}", "DELETE")
                    //      .Add<GradeInfo>("/Grade/{Id}", "GET")
                    //      .Add<List<GradeInfo>>("/Grade", "GET")
                    //      .Add<bool>("/Grade", "PUT"); 
    
                    //启用请求参数合法性验证功能:
                    Plugins.Add(new ValidationFeature());
    
                    //Plugins.Add(new ProtoBufFormat());
    
                    //JsConfig.EmitCamelCaseNames = false;
                    //Plugins.Add(new SwaggerFeature());
                    Plugins.Add(new CorsFeature("*.test.com"));
                }
    
                public override void Configure(Funq.Container container)
                {
                    //Register any dependencies your services use here.
                }
            }
    

     web.config的配置文件必须加上  红色标记的路径

    <system.webServer>
        <modules>
          <remove name="TelemetryCorrelationHttpModule" />
          <add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
          <remove name="ApplicationInsightsWebTracking" />
          <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
        </modules>
        
        <validation validateIntegratedModeConfiguration="false" />
        <handlers>
          <!--<add path="*.aspx" name="DefaultHttpApplication" type="System.Web.UI.PageHandlerFactory" verb="*" ></add>-->
          <add path="*" name="ServiceStack.Factory" type="ServiceStack.HttpHandlerFactory, ServiceStack" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
        </handlers>
      </system.webServer>

    然后对接服务

     protected const string listenOnUrl = "http://localhost:2285/";//web的连接
    
    
    对接接口
    StudentInfo model = new StudentInfo();
                GetStudent(model);
                using (JsonServiceClient client = new JsonServiceClient(listenOnUrl))
                {
                    model.id = client.Post<int>(model);
                }

    启动后的样子

  • 相关阅读:
    学习tornado:介绍
    【Unity Shaders】Using Textures for Effects介绍
    高性能C++网络库libtnet实现:http
    理解WebKit和Chromium: 硬件加速之RenderLayer树到合成树
    【Unity Shaders】Diffuse Shading——使用2D ramp texture来创建一个假的BRDF(双向反射分布函数)
    【Unity Shaders】Diffuse Shading——漫反射光照改善技巧
    ③ 设计模式的艺术-16.访问者(Visitor)模式艺术
    ② 设计模式的艺术-15.观察者(Observer)模式
    ① 设计模式的艺术-14.职责链(Chain of Responsibility)模式
    ⑦ 设计模式的艺术-13.代理(Proxy)模式
  • 原文地址:https://www.cnblogs.com/shuaimeng/p/11475603.html
Copyright © 2011-2022 走看看