winform_webApiSelfHost

窗本构造函数中添加以下代码:

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

App.config文件中添加以下内容:

<appSettings>
<add key="baseAddress" value="http://localhost:9002" />
</appSettings>

Create an OWIN configuration handler

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

Add API controllers

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

Add code to start/stop the WebAPI listener

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

原文:http://www.cnblogs.com/xinzheng/p/5586965.html

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

转:winform_webApiSelfHost及 OWIN WebAPI Service的更多相关文章

  1. 基于OWIN WebAPI 使用OAuth授权服务【客户端验证授权(Resource Owner Password Credentials Grant)】

    适用范围 前面介绍了Client Credentials Grant ,只适合客户端的模式来使用,不涉及用户相关.而Resource Owner Password Credentials Grant模 ...

  2. ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus

    ASP.NET Linux部署(2) - MS Owin + WebApi + Mono + Jexus 本文承接我的上一篇博文: ASP.NET 5 Linux部署,那篇文章主要是针对最新的ASP. ...

  3. (转)基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】

    适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...

  4. 基于OWIN WebAPI 使用OAuth授权服务【客户端模式(Client Credentials Grant)】

    适应范围 采用Client Credentials方式,即应用公钥.密钥方式获取Access Token,适用于任何类型应用,但通过它所获取的Access Token只能用于访问与用户无关的Open ...

  5. Window sevice +OWIN+webapi

    webapi正常都托管在iis上,这里引入owin,将其托管在window服务上. 第一步:创建一个服务,这里就不多描述. 第二步:引入OWIN 2.1引入bll 2.2加入api路由配置 publi ...

  6. 【干货】基于Owin WebApi 使用OAuth2进行客户端授权服务

    前言:采用Client Credentials方式,即密钥key/password,场景一般是分为客户端限制必须有权限才能使用的模块,这和微信公众号开放平台很类似. 让用户通过客户端去获取自己的tok ...

  7. Owin WebAPI上传文件

    Owin是微软出了几年的东东了,一直没时间学习.大概了解了下,是一个脱离IIS环境,快速搭建WebAPI服务的东西. 刚好想尝试下尽量脱离IIS创建简单快捷配置的项目,就是用了Nginx+Owin的模 ...

  8. Owin + WebApi + OAuth2 搭建授权模式(授权码模式 Part I)

    绪 最近想要整理自己代码封装成库,也十分想把自己的设计思路贴出来让大家指正,奈何时间真的不随人意. 想要使用 OWIN 做中间件服务,该服务中包含 管线.授权 两部分.于是决定使用 webapi .O ...

  9. 基于OWIN WebAPI 使用OAUTH2授权服务【授权码模式(Authorization Code)】

    之前已经简单实现了OAUTH2的授权码模式(Authorization Code),但是基于JAVA的,今天花了点时间调试了OWIN的实现,基本就把基于OWIN的OAUHT2的四种模式实现完了.官方推 ...

随机推荐

  1. hadoop 编译代码及运行

    搞定了hadoop配置之后,可以写代码运行了,首先要配一下CLASS_PATH,修改/etc/profile export JAVA_HOME=/usr/lib/jvm/java--openjdk-i ...

  2. 【最近公共祖先】【线段树】URAL - 2109 - Tourism on Mars

    Few people know, but a long time ago a developed state existed on Mars. It consisted of n cities, nu ...

  3. 【pb_ds】bzoj1056 [HAOI2008]排名系统/bzoj1862 [Zjoi2006]GameZ游戏排名系统

    STL裸题,线下AC,bzoj无限RE ing…… #include<cstdio> #include<cctype> #include<iostream> #in ...

  4. 【spfa】bzoj1295 [SCOI2009]最长距离

    题意:给你一个n*m的点阵.有些点是障碍,求一个欧几里得距离最大的点对(A,B),使得在移走的障碍≤T的情况下,可以从A走到B. 建图,跑n*m次spfa,求出从 每个点 出发到 其他所有点 的 经过 ...

  5. log4j.properties配置详解与实例-全部测试通过

    最近使用log4j写log时候发现网上的写的都是千篇一律,写的好的嘛不全,写的全一点的嘛没有一点格式,看着累.这里把网上收集到的整理了一下,并且全部都在机器上测试成功了.这么好的文档估计没有了吧? # ...

  6. checkbox与文字对齐方式(同样可用于radio单选框)

    checkbox 在chrome 和 firefox中 间距是由margin撑开, IE中则是由padding,并且无法设置边框:   主要参考大神的博客,http://www.zhangxinxu. ...

  7. 【jQuery】:not选择器的说明和:checked选择器的使用

    1.:not选择器的说明使用 先给出一下例子: $(".form1 :not(input[name='category'])") 这个 能实现 获取到from1表单中除了input ...

  8. OneThink框架的文章详情页分页

    Application/Home/Controller/ArticleController.class.php的detail函数修改结果如下: /* 文档模型详情页 */public function ...

  9. scrapy安装使用教程

    1. 安装Python,我用的是Python2.7.11,你喜欢用什么版本,你开心就好,只是后面的软件有些可能需要配套. 2. 安装pip,下载pip-8.1.2.tar.gz (md5, pgp). ...

  10. iOS:Masonry练习详解

    Masonry练习详解   添加约束的方式: 1.通过使用NSLayoutConstraints添加约束到约束数组中,之前必须设置translatesAutoresizingMaskIntoConst ...