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.");
Process.Start("http://localhost:8080/api/xx");
Console.ReadLine();
}
}
 public class xxController: Controller
{
public IEnumerable<string> Get()
{
return new string[] { "", "" };
}
}

OwinSelfHost

//  OWIN host
        //**********************************************************************************
        static void Main(string[] args)
        {
            string baseAddress = "http://localhost:9000/";
            // Start OWIN host
            using (WebApp.Start<Startup>(url: baseAddress))
            {
                // Create HttpCient and make a request to api/values
                HttpClient client = new HttpClient();

var response = client.GetAsync(baseAddress + "api/xx").Result;

Console.WriteLine(response);
                Console.WriteLine(response.Content.ReadAsStringAsync().Result);
                Console.ReadLine();
            }
        }

public class Startup
        {
            // This code configures Web API. The Startup class is specified as a type
            // parameter in the WebApp.Start method.
            public void Configuration(IAppBuilder appBuilder)
            {
                // Configure Web API for self-host.
                HttpConfiguration config = new HttpConfiguration();
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );

appBuilder.UseWebApi(config);
            }
        }

//**********************************************************************************

cross-origin support

var baseAddress = ConfigurationManager.AppSettings["baseAddress"];
var config = new HttpSelfHostConfiguration(baseAddress);
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var cors = new EnableCorsAttribute("*", "*", "*");//**********
config.EnableCors(cors);
var server = new HttpSelfHostServer(config);
server.OpenAsync().Wait();
class Startup
{
// Hack from http://stackoverflow.com/a/17227764/19020 to load controllers in
// another assembly. Another way to do this is to create a custom assembly resolver
Type valuesControllerType = typeof(OWINTest.API.ValuesController); // This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{
// Configure Web API for self-host.
HttpConfiguration config = new HttpConfiguration(); // Enable attribute based routing
// http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
); appBuilder.UseWebApi(config);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http; namespace OWINTest.Service.API
{
[RoutePrefix("api/testing")]
public class RoutedController : ApiController
{
[Route("getall")]
public IEnumerable<string> GetAllItems()
{
return new string[] { "value1", "value2" };
}
}
} public partial class APIServiceTest : ServiceBase
{
public string baseAddress = "http://localhost:9000/";
private IDisposable _server = null; public APIServiceTest()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
_server = WebApp.Start<Startup>(url: baseAddress);
} protected override void OnStop()
{
if(_server != null)
{
_server.Dispose();
}
base.OnStop();
}
}

https://github.com/danesparza/OWIN-WebAPI-Service

Self寄宿的更多相关文章

  1. WCF : 如何将NetTcpBinding寄宿在IIS7上

    摘要 : 从IIS 7 开始, IIS增加了对非HTTP协议的支持. 因此, 自IIS 7之后, 可以将NetTcpBinding等非HTTP协议的Bindings直接寄宿在IIS上面. 本文将介绍如 ...

  2. WCF学习之旅—WCF寄宿前的准备(八)

    一.WCF服务应用程序与WCF服务库 我们在平时开发的过程中常用的项目类型有“WCF 服务应用程序”和“WCF服务库”. WCF服务应用程序,是一个可以执行的程序,它有独立的进程,WCF服务类协定的定 ...

  3. WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) 七 WCF服务的Windows 服务程序寄宿 这种方式的服务寄宿,和IIS一样有一个一样 ...

  4. WCF学习之旅—WCF服务的WAS寄宿(十二)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) 八.WAS宿主 IIS ...

  5. WCF学习之旅—WCF服务的批量寄宿(十三)

    上接    WCF学习之旅—WCF服务部署到IIS7.5(九) WCF学习之旅—WCF服务部署到应用程序(十) WCF学习之旅—WCF服务的Windows 服务程序寄宿(十一) WCF学习之旅—WCF ...

  6. 在webapi2中使用OWIN 自寄宿模式

    OWIN  自寄宿模式说的直白一点就是不需要IIS了,直接通过路由访问cs模式的服务 敲了一遍官方的例子,首先安装Microsoft.AspNet.WebApi.OwinSelfHost,注意不要安装 ...

  7. Web APi入门之Self-Host寄宿及路由原理(二)

    前言 刚开始表面上感觉Web API内容似乎没什么,也就是返回JSON数据,事实上远非我所想,不去研究不知道,其中的水还是比较深,那又如何,一步一个脚印来学习都将迎刃而解. Self-Host 我们知 ...

  8. 轻松搞定Win8 IIS支持SVC 从而实现IIS寄宿WCF服务

    写在前面 为了尝试在IIS中寄宿WCF服务,需要配置IIS支持SVC命令,于是便有了在DOS命令中用到ServiceModelReg.exe注册svc命令. 坑爹的是注册成功后就开始报错.无奈之下两次 ...

  9. WCF服务自我寄宿 Windows服务

    WCF寄宿有自我寄宿跟IIS寄宿 服务代码: [ServiceContract] ---服务契约 public interface ICustomerService { [OperationContr ...

  10. 【C#进阶系列】22 CLR寄宿和AppDomain

    关于寄宿和AppDomain 微软开发CLR时,将它实现成包含在一个DLL中的COM服务器. 任何Windows应用程序都能寄宿(容纳)CLR.(简单来讲,就是CLR在一个DLL中,通过引用这个DLL ...

随机推荐

  1. linux: ubuntu 14.04 和16.04 快速下载

    由于官网服务器在国外,下载速度奇慢,所以我们可以利用阿里云镜像下载ubuntuubuntu 14.04:http://mirrors.aliyun.com/ubuntu-releases/14.04/ ...

  2. 目标检测标注工具labelImg安装及使用

    目标检测中,原始图片的标注过程是非常重要的,它的作用是在原始图像中标注目标物体位置并对每张图片生成相应的xml文件表示目标标准框的位置.本文介绍一款使用方便且能够标注多类别并能直接生成xml文件的标注 ...

  3. 使用kindeditor获取不到富文本框中的值

    获取不到富文本框中的值,网上一搜一堆,但最终没有几个能解决问题的,折腾一番最终解决.注意就是红色代码,加上之后就可以解决问题了. KindEditor.ready(function (K) { var ...

  4. 如何:创建返回 UI 的外接程序

    https://msdn.microsoft.com/zh-cn/library/bb909849(v=vs.100).aspx

  5. vs Qt mysql 打包程序 Driver not loaded Driver not loaded

    vs下开发Qt连接mysql程序,开发过程中操作MySQL没有问题,但打包以后安装在别的电脑上发现竟然无法连接MySQL,打包的时候,所需的libmysql.dll等dll文件拷贝到exe同级目录了, ...

  6. 报错:flutter: Another exception was thrown: Could not find a generator for route RouteSettings

    原因是一个工程中多次使用MaterialApphttps://stackoverflow.com/questions/49132299/could-not-find-a-generator-for-r ...

  7. ES6深入浅出-7 新版的类(上集)-2.介绍JS中的类

    声明对象原型,公有属性. obj对象,它用一个属性__proto__记录了自己的原型 改掉它的原型为公有属性.那么obj这个对象及有了hi的方法.因为obj自己没有hi.那么就去自己的原型上去找了. ...

  8. 研究 node lzma 的压缩解压缩

    / eslint-disable / // 压缩为 lzma var fs = require('fs'); var lzma = require('lzma-native'); var compre ...

  9. RabbitMQ 入门教程(PHP版) 简单Demo

    RabbitMQ的关键字说明 (1)Broker:经纪人.提供一种传输服务,维护一条从生产者到消费者的传输线路,保证消息数据能按照指定的方式传输.粗略的可以将图中的RabbitMQ Server当作B ...

  10. 增强篇1 PO保存增强

    公众号文章地址:https://mp.weixin.qq.com/s?__biz=Mzg4MzI1OTM0OA==&mid=2247484108&idx=7&sn=90e561 ...