Self-Host

寄宿Web API 不一定需要IIS 的支持,我们可以采用Self Host 的方式使用任意类型的应用程序(控制台、Windows Forms 应用、WPF 应用甚至是Windows Service)作为宿主。

方法:

Nuget上安装Microsoft.AspNet.WebApi.SelfHost库

或者 OWIN来承载WebAPI服务

或者 引用:

System.Net.Http.dll

C:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Stack 5\Packages:

packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll

packages\Microsoft.AspNet.WebApi.SelfHost.5.2.3\lib\net45\System.Web.Http.SelfHost.dll

packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll

Newtonsoft.Json

例如控制台:

public class ValuesController : ApiController
    {
        public IEnumerable<string> Get()
        {
            return new string[] { "111", "222" };
        }
    }

class Program
    {
        static void Main(string[] args)
        {

//Assembly.Load("WebApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null");    //加载外部程序集
            var config = new HttpSelfHostConfiguration("http://localhost:8080");

config.Routes.MapHttpRoute(
                "API Default", "api/{controller}/{id}",
                new { id = RouteParameter.Optional });

using (var server = new HttpSelfHostServer(config))
            {
                server.OpenAsync().Wait();
                Console.WriteLine("Press Enter to quit.");
                Console.ReadLine();
            }
        }
    }

时间: 2024-08-01 07:23:35

Self-Host的相关文章

XShell 连接虚拟机中的服务器 失败 、连接中断(Connection closed by foreign host.)

在使用XShell连接虚拟机中的服务器时,报以下错误并断开连接,之前连接还是挺稳定的,忽然就这样了 Last login: Thu Aug 10 21:28:38 2017 from 192.168.1.102 [[email protected] ~]# Socket error Event: 32 Error: 10053. Connection closing...Socket close. Connection closed by foreign host. Disconnected f

thinkphp U方法生成链接没有host

今天将自己代码传到线上服务器,页面上用到很多thinkphp U() 方法生成的链接.本地测试没啥问题,到线上发现链接生成的不对,每个都没有了host 每个链接多个index,然后查看tp的U方法: $url    =   __APP__.'/'.($module?$module.MODULE_PATHINFO_DEPR:'').implode($depr,array_reverse($var)); 是因为__APP__这里的原因,然后找到__APP__赋值的地方: define('__APP_

VMware虚拟机安装Redhat7提示piix4_smbus:Host SMBus错误解决办法

错误: 系统开机出现错误提示:piix4_smbus 0000:00:007.3: Host SMBus controller not enabled 原因: 系统装入i2c_piix4模块所致,因为系统找不到这个模块,所以报错 处理方法: 1.查明装入模块的确切名字( lsmod | grep i2c_piix4 ),显示输出的结果是模块的确切名字:i2c_piix4 2.将该模块列入不装入名单.编辑文件vim /etc/modprobe.d/blacklist.conf,在末尾加入black

ssh无密码登录设置方法以及出现问题 ECDSA host key 和IP地址对应的key不同的解决

最近在做hadoop,因为要求各主机之间的用户必须相同,且为方便远程登录,需配置无密码登录 先附上ssh无密码登录设置方法: 先生成密钥并配置无ssh无密码登录本机,输入命令: ssh-keygen -t dsa -P '' -f ~/.ssh/id_dsa cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys 将文件拷贝到其他主机相同的文件夹内,输入命令: scp authorized_keys hostname1:~/.ssh/ scp autho

mysql启动报错:Fatal error: Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist

mysql在首次启动的时候可能会报错:Can’t open and lock privilege tables: Table ‘mysql.host’ doesn’t exist 这时候可以执行脚本 mysql_install_db –user=mysql –ldata=数据存放的路径

设置virtual host

在http.conf确认已载入相关模块 LoadModule vhost_alias_module modules/mod_vhost_alias.so 在httpd-vhosts.conf或者在/home/http/conf/httpd.conf 添加以下配置. <VirtualHost *:80> DocumentRoot "f:/git/minyun" ServerName minyun <Directory "f:/git/minyun"&

Windows下Nginx Virtual Host多站点配置详解

Windows下Nginx Virtual Host多站点配置详解 此教程适用于Windows系统已经配置好Nginx+Php+Mysql环境的同学. 如果您还未搭建WNMP环境,请查看 windows7配置Nginx+php+mysql教程. 先说明一下配置多站点的目的:在生产环境中,如果将系统所有代码文件都放在公开目录中,则很容易被查看到系统源码,这样是很不安全的,所以需要只公开index.php的入口文件目录.而同一个服务器中,可能运行多个系统,这样就必须公开多个入口文件目录,以便用不同的

好记心不如烂笔头,ssh登录 The authenticity of host 192.168.0.xxx can&#39;t be established. 的问题

The authenticity of host 'git.coding.net (123.59.85.184)' can't be established.RSA key fingerprint is SHA256:jok3FH7q5LJ6qvE7iPNehBgXRw51ErE77S0Dn+Vg/Ik.Are you sure you want to continue connecting (yes/no)? 用ssh登录一个机器(换过ip地址),提示输入yes后,屏幕不断出现y,只有按ctr

create-react-app之Invalid Host Header

[create-react-app之Invalid Host Header] 1.When you enable the `proxy` option, you opt into a more strict set of host checks. This is necessary because leaving the backend open to remote hosts makes your computer vulnerable to DNS rebinding attacks. yo

承载(Host)通用语言执行时

承载(Host)通用语言执行时(CLR) 还有一种使用COM 的方法是是把须要集成的 F# 代码与已有的 C/C++ 应用程序集成到一起.开成自己定义的承载通用语言执行时.通用语言执行时就是 C++ 应用程序,且有一些可用的库(.lib)文件.能够在标准的 C++ 应用程序中链接它. 承载通用语言执行时的代码要比载入 COM 库的代码稍许复杂一点,但不须要复杂的注冊 COM 库了:这种方法还可能以很细粒度地控制通用语言执行时的行为.尽管.我们会发现默认的行为对于大多数情况已经很好了:可是,这样的