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);
            }

启动后的样子

 

posted @ 2019-09-06 16:32  世人皆萌  阅读(852)  评论(0编辑  收藏  举报