C# ASP.NET WebApi服务器搭建详解 - 自承载(Self Hosting)


参考文章:
C# ASP.NET WebApi服务器搭建详解 - IIS服务承载(IIS Hosting IIS宿主)
C# ASP.NET WebApi服务器搭建详解 - Win服务承载(Windows Service Hosting宿主)
搭建自承载(Self Hosting)ASP.NET WebApi服务器操作步骤:
1. 在VS解决方案建立控制台应用程序,改名:CSFramework.WebAPI.Demo.SelfHosting

2. 在CSFramework.WebAPI.Demo.SelfHosting项目,点右键管理NuGet程序包,添加以下包:
HTML Code:
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Cors" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net45" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Cors" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Host.HttpListener" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
//来源:C/S框架网(www.csframework.com) QQ:23404761
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.Cors" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.7" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.7" targetFramework="net452" />
<package id="Microsoft.AspNet.WebPages" version="3.2.7" targetFramework="net45" />
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="1.0.0" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Cors" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Host.HttpListener" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Host.SystemWeb" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Hosting" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Owin.Security" version="4.1.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
<package id="Newtonsoft.Json" version="12.0.3" targetFramework="net45" />
<package id="Owin" version="1.0" targetFramework="net45" />
</packages>
//来源:C/S框架网(www.csframework.com) QQ:23404761

3. 打开Program.cs,添加启动服务器的代码, WebApp.Start
C# Code:
class Program
{
static void Main(string[] args)
{
try
{
string serverURL = "http://localhost:17818/";
Console.WriteLine("C# ASP.NET WebApi服务器搭建详解 - 自承载(Self Hosting)");
Console.WriteLine("正在启动WebApi Server :" + serverURL + " ....");
using (WebApp.Start<StartupAppBuilder>(url: serverURL))
{
Console.WriteLine("启动WepApi 服务器成功.");
Console.WriteLine("............");
Console.ReadLine();
}
}
catch (Exception ex)
{
string msg = ex.Message;
if (ex.InnerException != null) msg = msg + "\r\n" + ex.InnerException.Message;
Console.WriteLine("启动WepApi 服务器异常:" + msg);
Console.ReadLine();
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
class Program
{
static void Main(string[] args)
{
try
{
string serverURL = "http://localhost:17818/";
Console.WriteLine("C# ASP.NET WebApi服务器搭建详解 - 自承载(Self Hosting)");
Console.WriteLine("正在启动WebApi Server :" + serverURL + " ....");
using (WebApp.Start<StartupAppBuilder>(url: serverURL))
{
Console.WriteLine("启动WepApi 服务器成功.");
Console.WriteLine("............");
Console.ReadLine();
}
}
catch (Exception ex)
{
string msg = ex.Message;
if (ex.InnerException != null) msg = msg + "\r\n" + ex.InnerException.Message;
Console.WriteLine("启动WepApi 服务器异常:" + msg);
Console.ReadLine();
}
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
4. StartupAppBuilder.cs,WebApi服务器配置
C# Code:
public class StartupAppBuilder
{
public static void Configuration(IAppBuilder appBuilder)
{
// 在应用程序启动时运行的代码
//加载WebApi服务器配置信息
HttpConfiguration config = new HttpConfiguration();
//配置路由
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.MapHttpAttributeRoutes();
//配置JSON格式化
GlobalConfiguration.Configuration.Formatters.Insert(0, new PlainTextTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm";
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
config.Formatters.Remove(config.Formatters.XmlFormatter);
// 解决json序列化时的循环引用问题
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// 对 JSON 数据使用混合大小写。驼峰式,但是是javascript 首字母小写形式.
//config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// 对 JSON 数据使用混合大小写。跟属性名同样的大小.输出
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new UnderlineSplitContractResolver();
//使用配置
appBuilder.UseWebApi(config);
appBuilder.UseStageMarker(PipelineStage.PostAcquireState);
//链接控制器
//Type C1 = typeof(CSFramework.WebAPI.Demo.DemoController);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
public class StartupAppBuilder
{
public static void Configuration(IAppBuilder appBuilder)
{
// 在应用程序启动时运行的代码
//加载WebApi服务器配置信息
HttpConfiguration config = new HttpConfiguration();
//配置路由
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
config.MapHttpAttributeRoutes();
//配置JSON格式化
GlobalConfiguration.Configuration.Formatters.Insert(0, new PlainTextTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm";
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
config.Formatters.Remove(config.Formatters.XmlFormatter);
// 解决json序列化时的循环引用问题
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
// 对 JSON 数据使用混合大小写。驼峰式,但是是javascript 首字母小写形式.
//config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// 对 JSON 数据使用混合大小写。跟属性名同样的大小.输出
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new UnderlineSplitContractResolver();
//使用配置
appBuilder.UseWebApi(config);
appBuilder.UseStageMarker(PipelineStage.PostAcquireState);
//链接控制器
//Type C1 = typeof(CSFramework.WebAPI.Demo.DemoController);
}
}
//来源:C/S框架网(www.csframework.com) QQ:23404761
5. 启动服务器,在IE内测试接口:输入http://localhost:17818/api/demo/get

....本文完....
.NET WebApi开发框架|MVC框架|后端框架|服务端框架-标准版V1.0




简介
CSFramework.WebApi是服务端快速开发框架(后端框架),借助ASP.NET WebAPI底层架构的强大编程能力,封装成为可复用的以及可定制开发的服务端软件模板,CSFramework.WebApi提供可复用的软件架构和开发包,为用户快速轻松搭建基于HTTP协议、HTTPS协议以及支持多种客户端(如:APP、B/S、C/S、微信公众号、微信小程序等)各种跨平台移动终端的服务端应用程序。
服务端应用开发、后端接口开发是软件项目重要工作环节,服务端注重业务逻辑、数据处理和数据分析、算法等方面的设计和服务,前端主要体现在用户体验、界面操作和数据采集方面。前端软件系统和后端服务架构共同搭建跨平台大型数据管理应用系统。
扫一扫加微信:
版权声明:本文为开发框架文库发布内容,转载请附上原文出处连接
NewDoc C/S框架网