autofac + owin + webform + mvc + webapi集成demo
http://git.oschina.net/shiningrise/AutofacOwinDemo
using Microsoft.Owin;
using Owin;
using System.Web.Mvc; using Autofac;
using Autofac.Integration.Owin;
using Autofac.Integration.Mvc;
using Autofac.Integration.WebApi;
using System.Web.Http;
using System.Reflection;
using System.Web.Routing; [assembly: OwinStartupAttribute(typeof(AutofacOwinDemo.Startup))]
namespace AutofacOwinDemo
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
//app.Run(context =>
//{
// context.Response.ContentType = "text/plain";
// return context.Response.WriteAsync("Hello World!");
//}); ConfigureAuth(app); var builder = new ContainerBuilder();
builder.RegisterType<Test1>().As<ITest>();
builder.RegisterType<Test2>();
builder.RegisterControllers(typeof(MvcApplication).Assembly); var config = new HttpConfiguration();
WebApiConfig.Register(config); builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config); var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
config.DependencyResolver = new AutofacWebApiDependencyResolver(container); app.UseAutofacMiddleware(container);
app.UseAutofacMvc(); app.UseAutofacWebApi(config);
app.UseWebApi(config); }
}
}
webform属性注入
pagebase方式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; using Owin;
using Autofac;
using Autofac.Integration.Owin;
using System.Web; namespace AutofacOwinDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
public ITest Test { get; set; } protected void Page_PreInit(object sender, EventArgs e)
{
var owin = this.Context.Request.GetOwinContext() ;
var scop = owin.GetAutofacLifetimeScope();
scop.InjectProperties(this);
} protected void Page_Load(object sender, EventArgs e)
{ }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState; namespace AutofacOwinDemo
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas(); //GlobalConfiguration.Configure(WebApiConfig.Register); //这里要删除,让owin完全管理webapi FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
autofac + owin + webform + mvc + webapi集成demo的更多相关文章
- .net MVC, webAPI,webForm集成steeltoe+springcloud实现调用服务中心服务的总结
开始之前,如果没接触过Autofac的,可以移步到Autofac官方示例学习一下怎么使用:https://github.com/autofac/Examples .net 下集成steeltoe进行微 ...
- Visual Studio 2015 Owin+MVC+WebAPI+ODataV4+EntityFrawork+Identity+Oauth2.0+AngularJS 1.x 学习笔记
2016年,.net 会有很多大更新 ASP.NET 5 在此之前我都是用着古老的.net做开发的 (WebForm + IIS) 为了接下来应对 .net 的新功能,我特地去学习了一下基本的 MVC ...
- 基于OWIN ASP.NET WebAPI 使用OAUTH2授权服务的几点优化
前面在ASP.NET WEBAPI中集成了Client Credentials Grant与Resource Owner Password Credentials Grant两种OAUTH2模式,今天 ...
- ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 用javascript在客户端删除某一个cookie键值对 input点击链接另一个页面,各种操作。 C# 往线程里传参数的方法总结 TCP/IP 协议 用C#+Selenium+ChromeDriver 生成我的咕咚跑步路线地图 (转)值得学习百度开源70+项目
ASP.NET MVC WebApi 返回数据类型序列化控制(json,xml) 我们都知道在使用WebApi的时候Controller会自动将Action的返回值自动进行各种序列化处理(序列化为 ...
- Taurus.MVC WebAPI 入门开发教程1:框架下载环境配置与运行(含系列目录)。
前言: Taurus.MVC 微服务版本已经发布了:Taurus.MVC V3.0.3 微服务开源框架发布:让.NET 架构在大并发的演进过程更简单. 以前都是框架发布时写点相关功能点的文章,没有形成 ...
- 如何在 ASP.NET MVC 中集成 AngularJS(3)
今天来为大家介绍如何在 ASP.NET MVC 中集成 AngularJS 的最后一部分内容. 调试路由表 - HTML 缓存清除 就在我以为示例应用程序完成之后,我意识到,我必须提供两个版本的路由表 ...
- Spring4 MVC Hibernate4集成 Annotation
Spring4 MVC Hibernate4集成 Annotation 一.本文所用环境 二.工程目录 三.Maven添加依赖 四.新建数据库表 五.配置文件 六.Model层 七.DAO层 八.Se ...
- Spring4 MVC Hibernate4集成
Spring4 MVC Hibernate4集成 一. 本文所用环境 Spring4.0.3.RELEASE Hibernate4.3.5.Final Mysql 二. 工程目录 三. ...
- .net和java和谐相处之安卓客户端+.net asp.net mvc webapi 2
作为没有花很多时间转java,把java当C#用的我,在做服务器端程序的时候,自然不想考虑java web,java需要学的框架太多了,看了一下Java Servlet,始终没有编码的冲动.经过几天的 ...
随机推荐
- 关于php插件pdo_mysql的安装
今天在做一个商城的连接的时候,需要MpDO验证.需要安装pdo_mysql模块,刚开始按照php扩展模块的安装按照这个安装ZIP,curl都成功了但是安装pdo_mysql却不行,在./configu ...
- C#的值参数与引用参数
值参数:在使用值参数时,是把变量的值传给函数,函数中对此变量的任何修改都不影响该变量本身的值. 引用参数:使用引用参数时,在函数中对此变量的修改会影响变量的值. 说简单点,值参数,就是我把身份证复印件 ...
- MySQL忘记密码的解决方法
1.关闭正在运行的MySQL. 2.打开DOS窗口,转到mysql\bin目录. 3.输入mysqld --skip-grant-tables回车. 4.原窗口已经不动了,再开一个DOS窗口,转到my ...
- php热身2:CRUD with Ajax
这次热身是一个会员管理系统,包括会员注册.登录.资料修改功能,使用ajax技术 1.建表 use common_module; create table if not exists member( u ...
- 【poj3709】 K-Anonymous Sequence
http://poj.org/problem?id=3709 (题目链接) 题意 给出一个n个数的序列,要求将其中一些数改为另一个比它小的数,改动的花费为两数的绝对值,完成改动后使得整个序列中出现过的 ...
- web api Post 接收不到参数的问题
前端: 注意两个点: 1. contentType: "application/json" 请求的格式是Json 2. 要用JSON.stringify(customer)序列化对 ...
- 通过VMwarek可以安装Android_x86
Android也能安装到VMware上,不过内核是x86的. Android_x86下载资源:http://www.x86android.com/portal.php VMware版本>=12 ...
- [NOIP2012] 提高组 洛谷P1081 开车旅行
题目描述 小 A 和小 B 决定利用假期外出旅行,他们将想去的城市从 1 到 N 编号,且编号较小的 城市在编号较大的城市的西边,已知各个城市的海拔高度互不相同,记城市 i 的海拔高度为 Hi,城市 ...
- Always review
Data structures A data structure is a way to store and organize data in order to facilitate access a ...
- 利用php制作简单的文本式留言板
del.php 代码如下: <html><head ><meta charset="utf-8"><title>我的留言板</ ...