Skip to content

Commit f9e1490

Browse files
authored
Owin request scope (#43)
* Store OwinRequestScope in CallContext * Rename Ninject.Web.Common.OwinHost to Ninject.Web.Common.Owin * Remove System.Web from Ninject.Web.Common.Owin project
1 parent 0bfedc4 commit f9e1490

7 files changed

Lines changed: 89 additions & 8 deletions

File tree

Ninject.Web.Common.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ VisualStudioVersion = 15.0.27004.2002
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.Web.Common", "src\Ninject.Web.Common\Ninject.Web.Common.csproj", "{1FD016A0-A938-4F81-88BE-0DED65D9A022}"
77
EndProject
8-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.Web.Common.OwinHost", "src\Ninject.Web.Common.OwinHost\Ninject.Web.Common.OwinHost.csproj", "{F57C3DD1-15C0-4F76-9E92-81C4124CA47F}"
8+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.Web.Common.Owin", "src\Ninject.Web.Common.Owin\Ninject.Web.Common.Owin.csproj", "{F57C3DD1-15C0-4F76-9E92-81C4124CA47F}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ninject.Web.Common.WebHost", "src\Ninject.Web.Common.WebHost\Ninject.Web.Common.WebHost.csproj", "{FDBC2893-A26E-44F1-AC57-0D901780AE08}"
1111
EndProject

src/Ninject.Web.Common.OwinHost/Ninject.Web.Common.OwinHost.csproj renamed to src/Ninject.Web.Common.Owin/Ninject.Web.Common.Owin.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
<Version>0.0.0</Version>
66
<Authors>Ninject Project Contributors</Authors>
77
<Company>Ninject Project Contributors</Company>
8-
<Product>Ninject.Web.Common.OwinHost</Product>
8+
<Product>Ninject.Web.Common.Owin</Product>
99
<Description>OWIN extension for Ninject.Web.Common</Description>
1010
<Copyright>2010-2011 bbv Software Services AG. 2011-2017 Ninject Project Contributors.</Copyright>
1111
<GenerateDocumentationFile>true</GenerateDocumentationFile>
1212
<AssemblyOriginatorKeyFile>..\Ninject.snk</AssemblyOriginatorKeyFile>
1313
<SignAssembly>true</SignAssembly>
1414
<PublicSign Condition=" '$(OS)' != 'Windows_NT' ">true</PublicSign>
15-
<PackageId>Ninject.Web.Common.OwinHost</PackageId>
15+
<PackageId>Ninject.Web.Common.Owin</PackageId>
1616
<PackageVersion>0.0.0</PackageVersion>
1717
<PackageTags>IoC;DI;Ninject;Web;OWIN</PackageTags>
1818
<PackageProjectUrl>http://www.ninject.org/</PackageProjectUrl>

src/Ninject.Web.Common.OwinHost/OwinAppBuilderExtensions.cs renamed to src/Ninject.Web.Common.Owin/OwinAppBuilderExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
// </copyright>
2020
// -------------------------------------------------------------------------------------------------
2121

22-
namespace Ninject.Web.Common.OwinHost
22+
namespace Ninject.Web.Common.Owin
2323
{
2424
using System;
2525
using System.Collections.Generic;
2626
using System.Threading.Tasks;
2727

28-
using Owin;
28+
using global::Owin;
2929

3030
/// <summary>
3131
/// The OWIN app builder extensions.

src/Ninject.Web.Common.OwinHost/OwinBootstrapper.cs renamed to src/Ninject.Web.Common.Owin/OwinBootstrapper.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
// </copyright>
2020
// -------------------------------------------------------------------------------------------------
2121

22-
namespace Ninject.Web.Common.OwinHost
22+
namespace Ninject.Web.Common.Owin
2323
{
2424
using System;
2525
using System.Collections.Generic;
26+
using System.Runtime.Remoting.Messaging;
2627
using System.Threading.Tasks;
2728

2829
using Ninject.Modules;
@@ -78,15 +79,26 @@ public Func<IDictionary<string, object>, Task> Execute(Func<IDictionary<string,
7879
{
7980
kernel.Load(this.modules);
8081
}
82+
83+
kernel.Components.Add<INinjectHttpApplicationPlugin, OwinHttpApplicationPlugin>();
84+
8185
return kernel;
8286
});
8387

8488
return async context =>
8589
{
8690
using (var scope = new OwinRequestScope())
8791
{
92+
CallContext.SetData(NinjectOwinRequestScope, scope);
8893
context[NinjectOwinRequestScope] = scope;
89-
await next(context);
94+
try
95+
{
96+
await next(context);
97+
}
98+
finally
99+
{
100+
CallContext.FreeNamedDataSlot(NinjectOwinRequestScope);
101+
}
90102
}
91103
};
92104
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// -------------------------------------------------------------------------------------------------
2+
// <copyright file="OwinHttpApplicationPlugin.cs" company="Ninject Project Contributors">
3+
// Copyright (c) 2010-2011 bbv Software Services AG.
4+
// Copyright (c) 2011-2017 Ninject Project Contributors. All rights reserved.
5+
//
6+
// Dual-licensed under the Apache License, Version 2.0, and the Microsoft Public License (Ms-PL).
7+
// You may not use this file except in compliance with one of the Licenses.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
// or
12+
// http://www.microsoft.com/opensource/licenses.mspx
13+
//
14+
// Unless required by applicable law or agreed to in writing, software
15+
// distributed under the License is distributed on an "AS IS" BASIS,
16+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
// See the License for the specific language governing permissions and
18+
// limitations under the License.
19+
// </copyright>
20+
// -------------------------------------------------------------------------------------------------
21+
22+
namespace Ninject.Web.Common.Owin
23+
{
24+
using System.Runtime.Remoting.Messaging;
25+
26+
using Ninject.Activation;
27+
28+
/// <summary>
29+
/// The Owin Http Application plugin.
30+
/// </summary>
31+
public class OwinHttpApplicationPlugin : INinjectHttpApplicationPlugin
32+
{
33+
/// <summary>
34+
/// Gets or sets the <see cref="INinjectSettings"/>.
35+
/// </summary>
36+
public INinjectSettings Settings { get; set; }
37+
38+
/// <summary>
39+
/// Disposes the instances.
40+
/// </summary>
41+
public void Dispose()
42+
{
43+
}
44+
45+
/// <summary>
46+
/// Gets the owin request scope.
47+
/// </summary>
48+
/// <param name="context">The context.</param>
49+
/// <returns>The request scope.</returns>
50+
public object GetRequestScope(IContext context)
51+
{
52+
return CallContext.GetData(OwinBootstrapper.NinjectOwinRequestScope);
53+
}
54+
55+
/// <summary>
56+
/// Starts the instance.
57+
/// </summary>
58+
public void Start()
59+
{
60+
}
61+
62+
/// <summary>
63+
/// Stops the instance.
64+
/// </summary>
65+
public void Stop()
66+
{
67+
}
68+
}
69+
}

src/Ninject.Web.Common.OwinHost/OwinRequestScope.cs renamed to src/Ninject.Web.Common.Owin/OwinRequestScope.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// </copyright>
2020
// -------------------------------------------------------------------------------------------------
2121

22-
namespace Ninject.Web.Common.OwinHost
22+
namespace Ninject.Web.Common.Owin
2323
{
2424
using Ninject.Infrastructure.Disposal;
2525

src/Ninject.Web.Common.OwinHost/Properties/AssemblyInfo.cs renamed to src/Ninject.Web.Common.Owin/Properties/AssemblyInfo.cs

File renamed without changes.

0 commit comments

Comments
 (0)