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. 阿里druid连接池监控数据自定义存储

    如何将druid连接池监控到的sql执行效率,连接池资源情况等进行持久化存储,方便系统运维分析优化,以下案例初步测试成功. 第一部: 新建MyDruidStatLogger类实现接口 extends ...

  2. 【转载】 DeepMind发表Nature子刊新论文:连接多巴胺与元强化学习的新方法

    原文地址: baijiahao.baidu.com/s?id=1600509777750939986&wfr=spider&for=pc 机器之心 18-05-15   14:26 - ...

  3. ISO/IEC 9899:2011 条款6.8——语句和语句块

    6.8 语句和语句块 语法 1.statement: labeled-statement compound-statement         expression-statement         ...

  4. invalid application of ‘sizeof’ to incomplete type

    sizeof 后面所跟的数据类型没有定义,或者找不到定义的地方 eg: 头文件中定义结构体如下: struct PersonaL{ char name[]; int  age; }; 但是在cpp中使 ...

  5. Qt编写数据可视化大屏界面电子看板13-基础版

    一.前言 之前发布的Qt编写的可视化大屏电子看板系统,很多开发者比较感兴趣,也收到了很多反馈意见,纵观市面上的大屏系统,基本上都是B/S结构的web版本,需要在后台进行自定义配置模块,绑定数据源等,其 ...

  6. git 更新fork的远程仓库

    1.添加远程仓库到本地remote分支 git remote add upstream git@github.com:apache/flink.git # 远程仓库地址 2.查看当前仓库的远程分支 g ...

  7. c# Invoke的新用法

    在C# 3.0及以后的版本中有了Lamda表达式,像上面这种匿名委托有了更简洁的写法..NET Framework 3.5及以后版本更能用Action封装方法.例如以下写法可以看上去非常简洁: voi ...

  8. [ kvm ] 学习笔记 3:KVM 基础功能详解

    1. 构建 KVM 环境 KVM 从诞生开始就需要硬件虚拟化的支持,KVM 必需的硬件虚拟化扩展分别是:Intel 的虚拟化技术(Intel VT)和 AMD 的 AMD-V 技术.首先处理器(CPU ...

  9. 【Leetcode_easy】929. Unique Email Addresses

    problem 929. Unique Email Addresses solution: class Solution { public: int numUniqueEmails(vector< ...

  10. python logging模块日志输出

    import logging logger = logging.getLogger(__name__) logger.setLevel(level = logging.INFO) handler = ...