Skip to content

Commit 1836ba7

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Update Web API projects to use OWIN self-hosting instead of IIS and upgrade to the common project system
1 parent 7bc4cc6 commit 1836ba7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+665
-2741
lines changed

ApiVersioning.sln

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "misc", "misc", "{2957BAF3-9
2828
EndProjectSection
2929
EndProject
3030
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{915BB224-B1D0-4E27-A348-67FCC77AAA44}"
31-
EndProject
32-
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "webapi", "webapi", "{F446ED94-368F-4F67-913B-16E82CA80DFC}"
3331
ProjectSection(SolutionItems) = preProject
34-
samples\webapi\directory.build.targets = samples\webapi\directory.build.targets
32+
samples\directory.build.props = samples\directory.build.props
3533
EndProjectSection
3634
EndProject
35+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "webapi", "webapi", "{F446ED94-368F-4F67-913B-16E82CA80DFC}"
36+
EndProject
3737
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "aspnetcore", "aspnetcore", "{900DD210-8500-4D89-A05D-C9526935A719}"
3838
EndProject
3939
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicSample", "samples\aspnetcore\BasicSample\BasicSample.csproj", "{59389B47-8280-411E-B840-D097AA1DCDEE}"

samples/directory.build.props

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
4+
<PropertyGroup Label="C#">
5+
<LangVersion>latest</LangVersion>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 22 additions & 194 deletions
Original file line numberDiff line numberDiff line change
@@ -1,195 +1,23 @@
1-
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3-
<Import Project="..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props" Condition="Exists('..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" />
4-
<Import Project="..\..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props" Condition="Exists('..\..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" />
5-
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
6-
<PropertyGroup>
7-
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
8-
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
9-
<ProductVersion>
10-
</ProductVersion>
11-
<SchemaVersion>2.0</SchemaVersion>
12-
<ProjectGuid>{E496EED0-F8C9-4FE9-83E6-75E47A3C41A1}</ProjectGuid>
13-
<ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
14-
<OutputType>Library</OutputType>
15-
<AppDesignerFolder>Properties</AppDesignerFolder>
16-
<RootNamespace>Microsoft.Examples</RootNamespace>
17-
<AssemblyName>AdvancedODataWebApiSample</AssemblyName>
18-
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
19-
<UseIISExpress>true</UseIISExpress>
20-
<IISExpressSSLPort />
21-
<IISExpressAnonymousAuthentication />
22-
<IISExpressWindowsAuthentication />
23-
<IISExpressUseClassicPipelineMode />
24-
<UseGlobalApplicationHostFile />
25-
<NuGetPackageImportStamp>
26-
</NuGetPackageImportStamp>
27-
<Use64BitIISExpress />
28-
</PropertyGroup>
29-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
30-
<DebugSymbols>true</DebugSymbols>
31-
<DebugType>full</DebugType>
32-
<Optimize>false</Optimize>
33-
<OutputPath>bin\</OutputPath>
34-
<DefineConstants>DEBUG;TRACE</DefineConstants>
35-
<ErrorReport>prompt</ErrorReport>
36-
<WarningLevel>4</WarningLevel>
37-
</PropertyGroup>
38-
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
39-
<DebugType>pdbonly</DebugType>
40-
<Optimize>true</Optimize>
41-
<OutputPath>bin\</OutputPath>
42-
<DefineConstants>TRACE</DefineConstants>
43-
<ErrorReport>prompt</ErrorReport>
44-
<WarningLevel>4</WarningLevel>
45-
</PropertyGroup>
46-
<ItemGroup>
47-
<Reference Include="Microsoft.AspNet.OData, Version=7.0.1.20718, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
48-
<HintPath>..\..\..\packages\Microsoft.AspNet.OData.7.0.1\lib\net45\Microsoft.AspNet.OData.dll</HintPath>
49-
</Reference>
50-
<Reference Include="Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
51-
<HintPath>..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll</HintPath>
52-
</Reference>
53-
<Reference Include="Microsoft.CSharp" />
54-
<Reference Include="Microsoft.Extensions.DependencyInjection, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
55-
<HintPath>..\..\..\packages\Microsoft.Extensions.DependencyInjection.1.0.0\lib\netstandard1.1\Microsoft.Extensions.DependencyInjection.dll</HintPath>
56-
</Reference>
57-
<Reference Include="Microsoft.Extensions.DependencyInjection.Abstractions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
58-
<HintPath>..\..\..\packages\Microsoft.Extensions.DependencyInjection.Abstractions.1.0.0\lib\netstandard1.0\Microsoft.Extensions.DependencyInjection.Abstractions.dll</HintPath>
59-
</Reference>
60-
<Reference Include="Microsoft.OData.Core, Version=7.5.0.20627, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
61-
<HintPath>..\..\..\packages\Microsoft.OData.Core.7.5.0\lib\portable-net45+win8+wpa81\Microsoft.OData.Core.dll</HintPath>
62-
</Reference>
63-
<Reference Include="Microsoft.OData.Edm, Version=7.5.0.20627, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
64-
<HintPath>..\..\..\packages\Microsoft.OData.Edm.7.5.0\lib\portable-net45+win8+wpa81\Microsoft.OData.Edm.dll</HintPath>
65-
</Reference>
66-
<Reference Include="Microsoft.Owin, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
67-
<HintPath>..\..\..\packages\Microsoft.Owin.3.0.1\lib\net45\Microsoft.Owin.dll</HintPath>
68-
<Private>True</Private>
69-
</Reference>
70-
<Reference Include="Microsoft.Owin.Host.SystemWeb, Version=3.0.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
71-
<HintPath>..\..\..\packages\Microsoft.Owin.Host.SystemWeb.3.0.1\lib\net45\Microsoft.Owin.Host.SystemWeb.dll</HintPath>
72-
<Private>True</Private>
73-
</Reference>
74-
<Reference Include="Microsoft.Spatial, Version=7.5.0.20627, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
75-
<HintPath>..\..\..\packages\Microsoft.Spatial.7.5.0\lib\portable-net45+win8+wpa81\Microsoft.Spatial.dll</HintPath>
76-
</Reference>
77-
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
78-
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
79-
<Private>True</Private>
80-
</Reference>
81-
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
82-
<HintPath>..\..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
83-
<Private>True</Private>
84-
</Reference>
85-
<Reference Include="System.Net.Http" />
86-
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
87-
<HintPath>..\..\..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
88-
<Private>True</Private>
89-
</Reference>
90-
<Reference Include="System.Web.DynamicData" />
91-
<Reference Include="System.Web.Entity" />
92-
<Reference Include="System.Web.ApplicationServices" />
93-
<Reference Include="System.ComponentModel.DataAnnotations" />
94-
<Reference Include="System" />
95-
<Reference Include="System.Data" />
96-
<Reference Include="System.Core" />
97-
<Reference Include="System.Data.DataSetExtensions" />
98-
<Reference Include="System.Web.Extensions" />
99-
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
100-
<HintPath>..\..\..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
101-
<Private>True</Private>
102-
</Reference>
103-
<Reference Include="System.Web.Http.Owin, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
104-
<HintPath>..\..\..\packages\Microsoft.AspNet.WebApi.Owin.5.2.3\lib\net45\System.Web.Http.Owin.dll</HintPath>
105-
<Private>True</Private>
106-
</Reference>
107-
<Reference Include="System.Xml.Linq" />
108-
<Reference Include="System.Drawing" />
109-
<Reference Include="System.Web" />
110-
<Reference Include="System.Xml" />
111-
<Reference Include="System.Configuration" />
112-
<Reference Include="System.Web.Services" />
113-
<Reference Include="System.EnterpriseServices" />
114-
<Reference Include="..\..\..\src\Microsoft.AspNet.WebApi.Versioning\bin\$(Configuration)\net45\Microsoft.AspNet.WebApi.Versioning.dll">
115-
<Name>WebApi</Name>
116-
</Reference>
117-
<Reference Include="..\..\..\src\Microsoft.AspNet.OData.Versioning\bin\$(Configuration)\net45\Microsoft.AspNet.OData.Versioning.dll">
118-
<Name>WebApi.OData</Name>
119-
</Reference>
120-
</ItemGroup>
121-
<ItemGroup>
122-
<Content Include="packages.config" />
123-
<None Include="Web.Debug.config">
124-
<DependentUpon>Web.config</DependentUpon>
125-
</None>
126-
<None Include="Web.Release.config">
127-
<DependentUpon>Web.config</DependentUpon>
128-
</None>
129-
</ItemGroup>
130-
<ItemGroup>
131-
<Content Include="Web.config" />
132-
</ItemGroup>
133-
<ItemGroup>
134-
<Compile Include="Configuration\OrderModelConfiguration.cs" />
135-
<Compile Include="Configuration\PersonModelConfiguration.cs" />
136-
<Compile Include="Controllers\Orders2Controller.cs" />
137-
<Compile Include="Controllers\Orders3Controller.cs" />
138-
<Compile Include="Controllers\OrdersController.cs" />
139-
<Compile Include="Controllers\People2Controller.cs" />
140-
<Compile Include="Controllers\PeopleController.cs" />
141-
<Compile Include="Models\Order.cs" />
142-
<Compile Include="Models\Person.cs" />
143-
<Compile Include="Properties\AssemblyInfo.cs" />
144-
<Compile Include="Startup.cs" />
145-
</ItemGroup>
146-
<ItemGroup>
147-
<ProjectReference Include="..\..\..\src\Microsoft.AspNet.OData.Versioning\Microsoft.AspNet.OData.Versioning.csproj">
148-
<Project>{48a2b488-23ab-4c83-ae30-0b8b735c4562}</Project>
149-
<Name>Microsoft.AspNet.OData.Versioning</Name>
150-
</ProjectReference>
151-
<ProjectReference Include="..\..\..\src\Microsoft.AspNet.WebApi.Versioning\Microsoft.AspNet.WebApi.Versioning.csproj">
152-
<Project>{3bac97ed-1a8e-4f5a-a716-db5255f51c81}</Project>
153-
<Name>Microsoft.AspNet.WebApi.Versioning</Name>
154-
</ProjectReference>
155-
</ItemGroup>
156-
<PropertyGroup>
157-
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
158-
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
159-
</PropertyGroup>
160-
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
161-
<Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
162-
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
163-
<ProjectExtensions>
164-
<VisualStudio>
165-
<FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
166-
<WebProjectProperties>
167-
<UseIIS>True</UseIIS>
168-
<AutoAssignPort>True</AutoAssignPort>
169-
<DevelopmentServerPort>1044</DevelopmentServerPort>
170-
<DevelopmentServerVPath>/</DevelopmentServerVPath>
171-
<IISUrl>http://localhost:1044/</IISUrl>
172-
<NTLMAuthentication>False</NTLMAuthentication>
173-
<UseCustomServer>False</UseCustomServer>
174-
<CustomServerUrl>
175-
</CustomServerUrl>
176-
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
177-
</WebProjectProperties>
178-
</FlavorProperties>
179-
</VisualStudio>
180-
</ProjectExtensions>
181-
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
182-
<PropertyGroup>
183-
<ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
184-
</PropertyGroup>
185-
<Error Condition="!Exists('..\..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.Net.Compilers.1.3.2\build\Microsoft.Net.Compilers.props'))" />
186-
<Error Condition="!Exists('..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props')" Text="$([System.String]::Format('$(ErrorText)', '..\..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\build\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.props'))" />
187-
</Target>
188-
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
189-
Other similar extension points exist, see Microsoft.Common.targets.
190-
<Target Name="BeforeBuild">
191-
</Target>
192-
<Target Name="AfterBuild">
193-
</Target>
194-
-->
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net461</TargetFramework>
6+
<RootNamespace>Microsoft.Examples</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Reference Include="Microsoft.CSharp" />
11+
<Reference Include="System" />
12+
<Reference Include="System.ComponentModel.DataAnnotations" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<PackageReference Include="Microsoft.AspNet.WebApi.OwinSelfHost" Version="5.2.7" />
17+
</ItemGroup>
18+
19+
<ItemGroup>
20+
<ProjectReference Include="..\..\..\src\Microsoft.AspNet.OData.Versioning\Microsoft.AspNet.OData.Versioning.csproj" />
21+
</ItemGroup>
22+
19523
</Project>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace Microsoft.Examples
2+
{
3+
using Microsoft.Owin.Hosting;
4+
using System;
5+
using System.Threading;
6+
7+
public class Program
8+
{
9+
const string Url = "http://localhost:9006/";
10+
const string LaunchUrl = Url + "api";
11+
static readonly ManualResetEvent resetEvent = new ManualResetEvent( false );
12+
13+
public static void Main( string[] args )
14+
{
15+
Console.CancelKeyPress += OnCancel;
16+
17+
using ( WebApp.Start<Startup>( Url ) )
18+
{
19+
Console.WriteLine( "Content root path: " + Startup.ContentRootPath );
20+
Console.WriteLine( "Now listening on: " + Url );
21+
Console.WriteLine( "Application started. Press Ctrl+C to shut down." );
22+
resetEvent.WaitOne();
23+
}
24+
25+
Console.CancelKeyPress -= OnCancel;
26+
}
27+
28+
static void OnCancel( object sender, ConsoleCancelEventArgs e )
29+
{
30+
Console.Write( "Application is shutting down..." );
31+
e.Cancel = true;
32+
resetEvent.Set();
33+
}
34+
}
35+
}

samples/webapi/AdvancedODataWebApiSample/Properties/AssemblyInfo.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.

samples/webapi/AdvancedODataWebApiSample/Startup.cs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace Microsoft.Examples
1111
using Microsoft.OData;
1212
using Microsoft.OData.UriParser;
1313
using Microsoft.Web.Http.Versioning;
14+
using System;
1415
using System.Web.Http;
1516
using static Microsoft.OData.ODataUrlKeyDelimiter;
1617
using static Microsoft.OData.ServiceLifetime;
@@ -24,11 +25,17 @@ public void Configuration( IAppBuilder appBuilder )
2425
var httpServer = new HttpServer( configuration );
2526

2627
configuration.AddApiVersioning(
27-
o =>
28+
options =>
2829
{
29-
o.ReportApiVersions = true;
30-
o.AssumeDefaultVersionWhenUnspecified = true;
31-
o.ApiVersionReader = ApiVersionReader.Combine(
30+
// reporting api versions will return the headers "api-supported-versions" and "api-deprecated-versions"
31+
options.ReportApiVersions = true;
32+
33+
// allows a client to make a request without specifying an api version. the value of
34+
// options.DefaultApiVersion will be 'assumed'; this is meant to grandfather in legacy apis
35+
options.AssumeDefaultVersionWhenUnspecified = true;
36+
37+
// allow multiple locations to request an api version
38+
options.ApiVersionReader = ApiVersionReader.Combine(
3239
new QueryStringApiVersionReader(),
3340
new HeaderApiVersionReader( "api-version", "x-ms-version" ) );
3441
} );
@@ -44,8 +51,14 @@ public void Configuration( IAppBuilder appBuilder )
4451
var models = modelBuilder.GetEdmModels();
4552
var batchHandler = new DefaultODataBatchHandler( httpServer );
4653

54+
// NOTE: when you mix OData and non-Data controllers in Web API, it's RECOMMENDED to only use
55+
// convention-based routing. using attribute routing may not work as expected due to limitations
56+
// in the underlying routing system. the order of route registration is important as well.
57+
//
58+
// DO NOT use configuration.MapHttpAttributeRoutes();
4759
configuration.MapVersionedODataRoutes( "odata", "api", models, ConfigureContainer, batchHandler );
4860
configuration.Routes.MapHttpRoute( "orders", "api/{controller}/{id}", new { id = Optional } );
61+
4962
appBuilder.UseWebApi( httpServer );
5063
}
5164

@@ -54,5 +67,20 @@ static void ConfigureContainer( IContainerBuilder builder )
5467
builder.AddService<IODataPathHandler>( Singleton, sp => new DefaultODataPathHandler() { UrlKeyDelimiter = Parentheses } );
5568
builder.AddService<ODataUriResolver>( Singleton, sp => new UnqualifiedCallAndEnumPrefixFreeResolver() { EnableCaseInsensitive = true } );
5669
}
70+
71+
public static string ContentRootPath
72+
{
73+
get
74+
{
75+
var app = AppDomain.CurrentDomain;
76+
77+
if ( string.IsNullOrEmpty( app.RelativeSearchPath ) )
78+
{
79+
return app.BaseDirectory;
80+
}
81+
82+
return app.RelativeSearchPath;
83+
}
84+
}
5785
}
5886
}

0 commit comments

Comments
 (0)