Skip to content

Commit 6c99907

Browse files
committed
Fix .NET Core 404 handling + convert SelfHost to netcoreapp2.0
1 parent 9b48b57 commit 6c99907

8 files changed

Lines changed: 51 additions & 111 deletions

File tree

src/ServiceStack.Core.SelfHost/Program.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@
33
using System.IO;
44
using System.Linq;
55
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore;
67
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Configuration;
9+
using Microsoft.Extensions.Logging;
710

811
namespace ServiceStack.Core.SelfHost
912
{
1013
public class Program
1114
{
1215
public static void Main(string[] args)
1316
{
14-
var host = new WebHostBuilder()
15-
.UseKestrel()
16-
.UseContentRoot(Directory.GetCurrentDirectory())
17-
.UseStartup<Startup>()
17+
BuildWebHost(args).Run();
18+
}
19+
20+
public static IWebHost BuildWebHost(string[] args) =>
21+
WebHost.CreateDefaultBuilder(args)
22+
.UseStartup<Startup>()
1823
.UseUrls("http://localhost:55000/")
1924
.Build();
20-
21-
host.Run();
22-
}
2325
}
2426
}

src/ServiceStack.Core.SelfHost/Properties/AssemblyInfo.cs

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

src/ServiceStack.Core.SelfHost/Properties/launchSettings.json

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
4+
<TargetFramework>netcoreapp2.0</TargetFramework>
55
<AssemblyName>ServiceStack.Core.SelfHost</AssemblyName>
6-
<OutputType>Exe</OutputType>
76
<PackageId>ServiceStack.Core.SelfHost</PackageId>
8-
<RuntimeFrameworkVersion>1.1.1</RuntimeFrameworkVersion>
9-
<PackageTargetFallback>$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
10-
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute>
11-
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
12-
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
137
</PropertyGroup>
148

159
<ItemGroup>
16-
<!--<Reference Include="..\..\lib\netstandard1.1\ServiceStack.Interfaces.dll" />
10+
<Folder Include="wwwroot\" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.AspNetCore.All" Version="2.0.0-preview2-final" />
15+
<Reference Include="..\..\lib\netstandard1.1\ServiceStack.Interfaces.dll" />
1716
<Reference Include="..\..\lib\netstandard1.3\ServiceStack.Text.dll" />
1817
<ProjectReference Include="..\ServiceStack\ServiceStack.csproj" />
1918
<ProjectReference Include="..\ServiceStack.Client\ServiceStack.Client.csproj" />
2019
<ProjectReference Include="..\ServiceStack.Common\ServiceStack.Common.csproj" />
21-
<ProjectReference Include="..\ServiceStack.Api.Swagger\ServiceStack.Api.Swagger.csproj" />-->
22-
23-
<PackageReference Include="ServiceStack.Text.Core" Version="1.*" />
24-
<PackageReference Include="ServiceStack.Core" Version="1.*" />
25-
<PackageReference Include="ServiceStack.Interfaces.Core" Version="1.*" />
26-
<PackageReference Include="ServiceStack.Client.Core" Version="1.*" />
27-
<PackageReference Include="ServiceStack.Common.Core" Version="1.*" />
28-
<PackageReference Include="ServiceStack.Api.Swagger.Core" Version="1.*" />
29-
</ItemGroup>
30-
31-
<ItemGroup>
32-
<PackageReference Include="Microsoft.AspNetCore.Diagnostics" Version="1.1.1" />
33-
<PackageReference Include="Microsoft.AspNetCore.Server.IISIntegration" Version="1.1.1" />
34-
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel" Version="1.1.1" />
35-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="1.1.1" />
20+
<ProjectReference Include="..\ServiceStack.Api.Swagger\ServiceStack.Api.Swagger.csproj" />
3621
</ItemGroup>
3722

3823
</Project>

src/ServiceStack.Core.SelfHost/Startup.cs

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Collections.Specialized;
42
using System.IO;
3+
using System.Collections.Generic;
54
using System.Linq;
6-
using System.Net;
7-
using System.Reflection;
85
using System.Threading.Tasks;
96
using Microsoft.AspNetCore.Builder;
107
using Microsoft.AspNetCore.Hosting;
118
using Microsoft.AspNetCore.Http;
129
using Microsoft.Extensions.DependencyInjection;
13-
using Microsoft.Extensions.Logging;
10+
11+
using Funq;
1412
using ServiceStack;
1513
using ServiceStack.Logging;
16-
using Funq;
1714
using ServiceStack.Host;
1815
using ServiceStack.Text;
19-
20-
using Microsoft.AspNetCore.Http.Extensions;
21-
using Microsoft.AspNetCore.Http.Internal;
22-
using Microsoft.Extensions.Primitives;
2316
using ServiceStack.Api.Swagger;
2417
using ServiceStack.Metadata;
2518
using ServiceStack.Web;
@@ -29,32 +22,25 @@ namespace ServiceStack.Core.SelfHost
2922
public class Startup
3023
{
3124
// This method gets called by the runtime. Use this method to add services to the container.
32-
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
25+
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
3326
public void ConfigureServices(IServiceCollection services)
3427
{
3528
}
3629

3730
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
38-
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
31+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
3932
{
40-
loggerFactory.AddConsole();
41-
42-
if (env.IsDevelopment())
43-
{
44-
app.UseDeveloperExceptionPage();
45-
}
33+
app.UseDeveloperExceptionPage();
4634

4735
app.UseServiceStack(new AppHost());
4836

49-
app.Run(async context =>
37+
app.Run(async (context) =>
5038
{
51-
context.Request.EnableRewind();
52-
await context.Response.WriteAsync("Hello World!!!");
39+
await context.Response.WriteAsync("Hello World!");
5340
});
5441
}
5542
}
5643

57-
5844
[Route("/hello")]
5945
[Route("/hello/{Name}")]
6046
public class Hello : IReturn<HelloResponse>
@@ -126,7 +112,7 @@ public class UploadStreamResponse { }
126112
public class AppHost : AppHostBase
127113
{
128114
public AppHost()
129-
: base(".NET Core Test", typeof(MyServices).GetTypeInfo().Assembly) { }
115+
: base(".NET Core Test", typeof(MyServices).GetAssembly()) { }
130116

131117
public override void Configure(Container container)
132118
{

src/ServiceStack.Core.sln

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26430.6
4+
VisualStudioVersion = 15.0.26430.16
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Hosts", "Hosts", "{43C8BF0C-1B57-4B1F-A5D2-6010B93C791A}"
77
EndProject
@@ -24,8 +24,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Build", "Build", "{FA46AB18
2424
EndProject
2525
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{312EC6C7-3026-472C-85A1-8189207DA807}"
2626
EndProject
27-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceStack.Core.SelfHost", "ServiceStack.Core.SelfHost\ServiceStack.Core.SelfHost.csproj", "{C59FEB65-D1FB-477F-B9F9-7CC2C7397D1A}"
28-
EndProject
2927
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ServiceStack.Core.SelfHostTests", "..\tests\ServiceStack.Core.SelfHostTests\ServiceStack.Core.SelfHostTests.csproj", "{97002C10-48C4-4711-961B-F3E3D4FC3E4E}"
3028
EndProject
3129
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mvc.Core.Tests", "..\tests\Mvc.Core.Tests\Mvc.Core.Tests.csproj", "{F62296EA-27ED-46F7-8782-E84555AB4438}"
@@ -64,10 +62,6 @@ Global
6462
Release|Any CPU = Release|Any CPU
6563
EndGlobalSection
6664
GlobalSection(ProjectConfigurationPlatforms) = postSolution
67-
{C59FEB65-D1FB-477F-B9F9-7CC2C7397D1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
68-
{C59FEB65-D1FB-477F-B9F9-7CC2C7397D1A}.Debug|Any CPU.Build.0 = Debug|Any CPU
69-
{C59FEB65-D1FB-477F-B9F9-7CC2C7397D1A}.Release|Any CPU.ActiveCfg = Release|Any CPU
70-
{C59FEB65-D1FB-477F-B9F9-7CC2C7397D1A}.Release|Any CPU.Build.0 = Release|Any CPU
7165
{97002C10-48C4-4711-961B-F3E3D4FC3E4E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
7266
{97002C10-48C4-4711-961B-F3E3D4FC3E4E}.Debug|Any CPU.Build.0 = Debug|Any CPU
7367
{97002C10-48C4-4711-961B-F3E3D4FC3E4E}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -137,7 +131,6 @@ Global
137131
HideSolutionNode = FALSE
138132
EndGlobalSection
139133
GlobalSection(NestedProjects) = preSolution
140-
{C59FEB65-D1FB-477F-B9F9-7CC2C7397D1A} = {43C8BF0C-1B57-4B1F-A5D2-6010B93C791A}
141134
{97002C10-48C4-4711-961B-F3E3D4FC3E4E} = {312EC6C7-3026-472C-85A1-8189207DA807}
142135
{F62296EA-27ED-46F7-8782-E84555AB4438} = {43C8BF0C-1B57-4B1F-A5D2-6010B93C791A}
143136
{0848319C-7A4C-4135-B14E-8376AD7AD7CE} = {43C8BF0C-1B57-4B1F-A5D2-6010B93C791A}

src/ServiceStack/AppHostBase.NetCore.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,10 @@ public virtual async Task ProcessRequest(HttpContext context, Func<Task> next)
114114
if (serviceStackHandler != null)
115115
{
116116
if (serviceStackHandler is NotFoundHttpHandler)
117+
{
117118
await next();
119+
return;
120+
}
118121

119122
if (!string.IsNullOrEmpty(serviceStackHandler.RequestName))
120123
operationName = serviceStackHandler.RequestName;
@@ -125,8 +128,20 @@ public virtual async Task ProcessRequest(HttpContext context, Func<Task> next)
125128
httpReq.OperationName = operationName = restHandler.RestPath.RequestType.GetOperationName();
126129
}
127130

128-
var task = serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
129-
await HostContext.Async.ContinueWith(httpReq, task, x => httpRes.Close(), TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
131+
try
132+
{
133+
await serviceStackHandler.ProcessRequestAsync(httpReq, httpRes, operationName);
134+
}
135+
catch (Exception ex)
136+
{
137+
var logFactory = context.Features.Get<ILoggerFactory>();
138+
var log = logFactory.CreateLogger(GetType());
139+
log.LogError(default(EventId), ex, ex.Message);
140+
}
141+
finally
142+
{
143+
httpRes.Close();
144+
}
130145
//Matches Exceptions handled in HttpListenerBase.InitTask()
131146

132147
return;

src/ServiceStack/Host/NetCore/NetCoreResponse.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,20 @@ public void SetContentLength(long contentLength)
130130

131131
public int StatusCode
132132
{
133-
get { return response.StatusCode; }
134-
set { response.StatusCode = value; }
133+
get => response.StatusCode;
134+
set => response.StatusCode = value;
135135
}
136136

137137
public string StatusDescription
138138
{
139-
get { return response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase; }
140-
set { response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = value; }
139+
get => response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase;
140+
set => response.HttpContext.Features.Get<IHttpResponseFeature>().ReasonPhrase = value;
141141
}
142142

143143
public string ContentType
144144
{
145-
get { return response.ContentType; }
146-
set { response.ContentType = value; }
145+
get => response.ContentType;
146+
set => response.ContentType = value;
147147
}
148148

149149
public Stream OutputStream => response.Body;

0 commit comments

Comments
 (0)