Skip to content

Commit 294be6a

Browse files
authored
HttpListener exception handling #314 (#317)
1 parent 2c68ecf commit 294be6a

File tree

7 files changed

+177
-22
lines changed

7 files changed

+177
-22
lines changed

Katana.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FunctionalTests", "Function
138138
EndProject
139139
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunctionalTests", "tests\FunctionalTests\FunctionalTests.csproj", "{4EF3F748-16D0-4112-AE43-AACADB3EF8E9}"
140140
EndProject
141+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Katana.Sandbox.Selfhost", "tests\Katana.Sandbox.Selfhost\Katana.Sandbox.Selfhost.csproj", "{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5}"
142+
EndProject
141143
Global
142144
GlobalSection(SolutionConfigurationPlatforms) = preSolution
143145
Debug|Any CPU = Debug|Any CPU
@@ -308,6 +310,10 @@ Global
308310
{4EF3F748-16D0-4112-AE43-AACADB3EF8E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
309311
{4EF3F748-16D0-4112-AE43-AACADB3EF8E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
310312
{4EF3F748-16D0-4112-AE43-AACADB3EF8E9}.Release|Any CPU.Build.0 = Release|Any CPU
313+
{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
314+
{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5}.Debug|Any CPU.Build.0 = Debug|Any CPU
315+
{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5}.Release|Any CPU.ActiveCfg = Release|Any CPU
316+
{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5}.Release|Any CPU.Build.0 = Release|Any CPU
311317
EndGlobalSection
312318
GlobalSection(SolutionProperties) = preSolution
313319
HideSolutionNode = FALSE
@@ -357,5 +363,6 @@ Global
357363
{23D90C94-647A-416C-9227-9A0779401EBC} = {10A4935F-4C17-44ED-BB00-D044FC7C77B8}
358364
{AA80B4A3-C885-4A7E-AF1A-FC3E89611988} = {10A4935F-4C17-44ED-BB00-D044FC7C77B8}
359365
{4EF3F748-16D0-4112-AE43-AACADB3EF8E9} = {DD2E82F1-F3EA-4D1F-B623-9C34440D79D6}
366+
{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5} = {D067FB54-B69D-4502-8E2F-676271AC4B86}
360367
EndGlobalSection
361368
EndGlobal

src/Microsoft.Owin.Host.HttpListener/OwinHttpListener.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,30 +214,12 @@ private async void ProcessRequestsAsync()
214214
{
215215
context = await _listener.GetContextAsync();
216216
}
217-
catch (ApplicationException ae)
218-
{
219-
// These come from the thread pool if HttpListener tries to call BindHandle after the listener has been disposed.
220-
Interlocked.Decrement(ref _currentOutstandingAccepts);
221-
LogHelper.LogException(_logger, "Accept", ae);
222-
return;
223-
}
224-
catch (HttpListenerException hle)
225-
{
226-
// These happen if HttpListener has been disposed
227-
Interlocked.Decrement(ref _currentOutstandingAccepts);
228-
LogHelper.LogException(_logger, "Accept", hle);
229-
return;
230-
}
231-
catch (ObjectDisposedException ode)
232-
{
233-
// These happen if HttpListener has been disposed
234-
Interlocked.Decrement(ref _currentOutstandingAccepts);
235-
LogHelper.LogException(_logger, "Accept", ode);
236-
return;
237-
}
238217
catch (Exception ex)
239218
{
240-
// Some other unknown error. Log it and try to keep going.
219+
// HttpListenerException happen if HttpListener has been disposed, or if the client disconnects mid request.
220+
// ObjectDisposedException happen if HttpListener has been disposed.
221+
// ApplicationException come from the thread pool if HttpListener tries to call BindHandle after the listener has been disposed.
222+
// Log it and try to keep going. Let IsListening break the loop.
241223
Interlocked.Decrement(ref _currentOutstandingAccepts);
242224
LogHelper.LogException(_logger, "Accept", ex);
243225
continue;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
5+
</startup>
6+
</configuration>
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{9F2F31EF-6017-48CC-88D6-EC4FF4FEAAA5}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>Katana.Sandbox.Selfhost</RootNamespace>
11+
<AssemblyName>Katana.Sandbox.Selfhost</AssemblyName>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<PlatformTarget>AnyCPU</PlatformTarget>
17+
<DebugSymbols>true</DebugSymbols>
18+
<DebugType>full</DebugType>
19+
<Optimize>false</Optimize>
20+
<OutputPath>bin\Debug\</OutputPath>
21+
<DefineConstants>DEBUG;TRACE</DefineConstants>
22+
<ErrorReport>prompt</ErrorReport>
23+
<WarningLevel>4</WarningLevel>
24+
</PropertyGroup>
25+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
26+
<PlatformTarget>AnyCPU</PlatformTarget>
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>bin\Release\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
</PropertyGroup>
34+
<ItemGroup>
35+
<Reference Include="Owin, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f0ebd12fd5e55cc5, processorArchitecture=MSIL">
36+
<HintPath>..\..\packages\Owin.1.0\lib\net40\Owin.dll</HintPath>
37+
<Private>True</Private>
38+
</Reference>
39+
<Reference Include="System" />
40+
<Reference Include="System.Core" />
41+
<Reference Include="System.Xml.Linq" />
42+
<Reference Include="System.Data.DataSetExtensions" />
43+
<Reference Include="Microsoft.CSharp" />
44+
<Reference Include="System.Data" />
45+
<Reference Include="System.Net.Http" />
46+
<Reference Include="System.Xml" />
47+
</ItemGroup>
48+
<ItemGroup>
49+
<Compile Include="Program.cs" />
50+
<Compile Include="Properties\AssemblyInfo.cs" />
51+
</ItemGroup>
52+
<ItemGroup>
53+
<None Include="App.config" />
54+
<None Include="packages.config" />
55+
</ItemGroup>
56+
<ItemGroup>
57+
<ProjectReference Include="..\..\src\Microsoft.Owin.Diagnostics\Microsoft.Owin.Diagnostics.csproj">
58+
<Project>{670915f7-f111-42ff-b004-39379a9d5951}</Project>
59+
<Name>Microsoft.Owin.Diagnostics</Name>
60+
</ProjectReference>
61+
<ProjectReference Include="..\..\src\Microsoft.Owin.FileSystems\Microsoft.Owin.FileSystems.csproj">
62+
<Project>{63988a9b-fa70-4bba-8c7e-784145384f7c}</Project>
63+
<Name>Microsoft.Owin.FileSystems</Name>
64+
</ProjectReference>
65+
<ProjectReference Include="..\..\src\Microsoft.Owin.Host.HttpListener\Microsoft.Owin.Host.HttpListener.csproj">
66+
<Project>{9f0c72d8-e43f-4f01-9deb-919191911919}</Project>
67+
<Name>Microsoft.Owin.Host.HttpListener</Name>
68+
</ProjectReference>
69+
<ProjectReference Include="..\..\src\Microsoft.Owin.Hosting\Microsoft.Owin.Hosting.csproj">
70+
<Project>{c225eb2f-e7a7-463f-b058-1705f204978e}</Project>
71+
<Name>Microsoft.Owin.Hosting</Name>
72+
</ProjectReference>
73+
<ProjectReference Include="..\..\src\Microsoft.Owin.StaticFiles\Microsoft.Owin.StaticFiles.csproj">
74+
<Project>{f31a42db-2f57-4dac-b2bc-106f2d6f3c82}</Project>
75+
<Name>Microsoft.Owin.StaticFiles</Name>
76+
</ProjectReference>
77+
<ProjectReference Include="..\..\src\Microsoft.Owin\Microsoft.Owin.csproj">
78+
<Project>{0db69cae-b0bc-4688-9467-66b4c1023d3f}</Project>
79+
<Name>Microsoft.Owin</Name>
80+
</ProjectReference>
81+
</ItemGroup>
82+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
83+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
84+
Other similar extension points exist, see Microsoft.Common.targets.
85+
<Target Name="BeforeBuild">
86+
</Target>
87+
<Target Name="AfterBuild">
88+
</Target>
89+
-->
90+
</Project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+

2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Owin;
8+
using Microsoft.Owin.Hosting;
9+
using Microsoft.Owin.Host.HttpListener;
10+
using Owin;
11+
12+
namespace Katana.Sandbox.Selfhost
13+
{
14+
class Program
15+
{
16+
static void Main()
17+
{
18+
var address = "http://localhost:8000/";
19+
using (var server = WebApp.Start(address, appBuilder =>
20+
{
21+
var owinHttpListener = appBuilder.Properties[typeof(OwinHttpListener).FullName] as OwinHttpListener;
22+
appBuilder.Use(async (context, next) => await context.Response.WriteAsync("Hello world!"));
23+
}))
24+
{
25+
Console.WriteLine("Listening on " + address);
26+
Console.ReadKey();
27+
}
28+
}
29+
}
30+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("Katana.Sandbox.Selfhost")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("Katana.Sandbox.Selfhost")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("9f2f31ef-6017-48cc-88d6-ec4ff4feaaa5")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="Owin" version="1.0" targetFramework="net45" />
4+
</packages>

0 commit comments

Comments
 (0)