Skip to content

Commit

Permalink
[aspnet5] Update to ASP.NET Core 1.0.0
Browse files Browse the repository at this point in the history
This migrates the server generator for aspnet5 from 1.0.0-rc1-final to
1.0.0.

Changes are fairly significant in how Kestrel hosts the application, as
well as how Swagger finds XML comments for documentation. Changes are
only related to hosting, docker, and configuration.
  • Loading branch information
jimschubert committed Jul 6, 2016
1 parent 82ee6bb commit ae73bb7
Show file tree
Hide file tree
Showing 21 changed files with 358 additions and 158 deletions.
1 change: 1 addition & 0 deletions bin/all-petstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fi
cd $APP_DIR
./bin/akka-scala-petstore.sh
./bin/android-petstore.sh
./bin/aspnet5-petstore-server.sh
./bin/clojure-petstore.sh
./bin/csharp-petstore.sh
./bin/dynamic-html.sh
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,22 @@ public void processOpts() {
apiPackage = packageName + ".Controllers";
modelPackage = packageName + ".Models";

supportingFiles.add(new SupportingFile("NuGet.Config", "", "NuGet.Config"));
supportingFiles.add(new SupportingFile("global.json", "", "global.json"));
supportingFiles.add(new SupportingFile("build.mustache", "", "build.sh"));
supportingFiles.add(new SupportingFile("build.sh.mustache", "", "build.sh"));
supportingFiles.add(new SupportingFile("build.bat.mustache", "", "build.bat"));
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile("Solution.mustache", "", this.packageName + ".sln"));
supportingFiles.add(new SupportingFile("Dockerfile.mustache", this.sourceFolder, "Dockerfile"));
supportingFiles.add(new SupportingFile("gitignore", this.sourceFolder, ".gitignore"));
supportingFiles.add(new SupportingFile("appsettings.json", this.sourceFolder, "appsettings.json"));

supportingFiles.add(new SupportingFile("project.mustache", this.sourceFolder, "project.json"));
supportingFiles.add(new SupportingFile("project.json.mustache", this.sourceFolder, "project.json"));
supportingFiles.add(new SupportingFile("Startup.mustache", this.sourceFolder, "Startup.cs"));
supportingFiles.add(new SupportingFile("Program.mustache", this.sourceFolder, "Program.cs"));
supportingFiles.add(new SupportingFile("web.config", this.sourceFolder, "web.config"));

supportingFiles.add(new SupportingFile("Project.xproj.mustache", this.sourceFolder, this.packageName + ".xproj"));

supportingFiles.add(new SupportingFile("Properties" + File.separator + "launchSettings.json", this.sourceFolder + File.separator + "Properties", "launchSettings.json"));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
FROM microsoft/aspnet:1.0.0-rc1-final
FROM microsoft/dotnet:latest

ENV DOTNET_CLI_TELEMETRY_OPTOUT 1

RUN mkdir -p /app/{{packageName}}
COPY . /app/{{packageName}}
WORKDIR /app/{{packageName}}
RUN ["dnu", "restore"]
RUN ["dnu", "pack", "--out", "artifacts"]

EXPOSE 5000/tcp
ENTRYPOINT ["dnx", "-p", "project.json", "web"]

RUN ["dotnet", "restore"]
ENTRYPOINT ["dotnet", "run", "-p", "project.json", "web"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="dotnet-core" value="https://www.myget.org/F/dotnet-core/api/v3/index.json" />
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Builder;

namespace {{packageName}}
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel(options =>
{
// options.ThreadCount = 4;
// options.UseHttps("cert.pfx", "certpassword");
options.NoDelay = true;
options.UseConnectionLogging();
})
.UseUrls("http://+:5000" /*, "https://+:5001" */)
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();

host.Run();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>{{projectGuid}}</ProjectGuid>
<RootNamespace>Sample</RootNamespace>
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath>
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
</PropertyGroup>
<Import Project="$(VSToolsPath)\DotNet.Web\Microsoft.DotNet.Web.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:50352/",
"sslPort": 0
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger/ui",
"launchUrl": "swagger/ui/index.html",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"web": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "http://localhost:5000/swagger/ui/index.html",
"environmentVariables": {
"ASPNET_ENV": "Development"
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
}
27 changes: 27 additions & 0 deletions modules/swagger-codegen/src/main/resources/aspnet5/README.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# {{packageName}} - ASP.NET Core 1.0 Server

{{#appDescription}}
{{{appDescription}}}
{{/appDescription}}

## Run

Linux/OS X:

```
sh build.sh
```

Windows:

```
build.bat
```

## Run in Docker

```
cd src/{{packageName}}
docker build -t {{packageName}} .
docker run -p 5000:5000 {{packageName}}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 14
VisualStudioVersion = 14.0.25420.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{815BE834-0656-4C12-84A4-43F2BA4B8BDE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{AFF6BF88-8A7D-4736-AF81-31BCE86B19BD}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
EndProjectSection
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "{{packageName}}", "src\{{packageName}}\{{packageName}}.xproj", "{{packageGuid}}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{3B5990B3-40F1-4148-89B5-84AC71432557}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B5990B3-40F1-4148-89B5-84AC71432557}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B5990B3-40F1-4148-89B5-84AC71432557}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B5990B3-40F1-4148-89B5-84AC71432557}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{{packageGuid}} = {815BE834-0656-4C12-84A4-43F2BA4B8BDE}
EndGlobalSection
EndGlobal
115 changes: 30 additions & 85 deletions modules/swagger-codegen/src/main/resources/aspnet5/Startup.mustache
Original file line number Diff line number Diff line change
@@ -1,136 +1,81 @@
{{>partial_header}}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using System.Threading.Tasks;
using System.Xml.XPath;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using Newtonsoft.Json.Serialization;
using Swashbuckle.SwaggerGen;
using Swashbuckle.SwaggerGen.XmlComments;
using Swashbuckle.Swagger.Model;
using Swashbuckle.SwaggerGen.Annotations;

namespace {{packageName}}
{
public class Startup
{
private readonly IHostingEnvironment _hostingEnv;
private readonly IApplicationEnvironment _appEnv;
public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
public IConfigurationRoot Configuration { get; }

public Startup(IHostingEnvironment env)
{
_hostingEnv = env;
_appEnv = appEnv;
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; set; }



// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
string xmlComments = string.Format(@"{0}{4}artifacts{4}{1}{4}{2}{3}{4}{{packageName}}.xml",
GetSolutionBasePath(),
_appEnv.Configuration,
_appEnv.RuntimeFramework.Identifier.ToLower(),
_appEnv.RuntimeFramework.Version.ToString().Replace(".", string.Empty),
Path.DirectorySeparatorChar);
// Add framework services.
services.AddMvc()
.AddJsonOptions(
opts => { opts.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); });

// Uncomment the following line to add Web API services which makes it easier to port Web API 2 controllers.
// You will also need to add the Microsoft.AspNet.Mvc.WebApiCompatShim package to the 'dependencies' section of project.json.
// services.AddWebApiConventions();


services.AddSwaggerGen();
services.ConfigureSwaggerDocument(options =>

services.ConfigureSwaggerGen(options =>
{
options.SingleApiVersion(new Info
{
Version = "v1",
Title = "{{packageName}}",
Description = "{{packageName}} (ASP.NET 5 Web API 2.x)"
Description = "{{packageName}} (ASP.NET Core 1.0)"
});

options.OperationFilter(new ApplyXmlActionCommentsFixed(xmlComments));
});

services.ConfigureSwaggerSchema(options => {
options.DescribeAllEnumsAsStrings = true;
options.ModelFilter(new ApplyXmlTypeCommentsFixed(xmlComments));
options.DescribeAllEnumsAsStrings();

var comments = new XPathDocument($"{AppContext.BaseDirectory}{Path.DirectorySeparatorChar}{_hostingEnv.ApplicationName}.xml");
options.OperationFilter<XmlCommentsOperationFilter>(comments);
options.ModelFilter<XmlCommentsModelFilter>(comments);
});

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.MinimumLevel = LogLevel.Information;
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
app.UseIISPlatformHandler();
app.UseMvc();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc();
app.UseSwaggerGen();
app.UseSwagger();
app.UseSwaggerUi();
}

// Taken from https://github.com/domaindrivendev/Ahoy/blob/master/test/WebSites/Basic/Startup.cs
private string GetSolutionBasePath()
{
var dir = Directory.CreateDirectory(_appEnv.ApplicationBasePath);
while (dir.Parent != null)
{
if (dir.GetDirectories("artifacts").Any())
return dir.FullName;
dir = dir.Parent;
}
throw new InvalidOperationException("Failed to detect solution base path - artifacts not found. Did you run dnu pack --out artifacts?");
}

// Entry point for the application.
public static void Main(string[] args) => WebApplication.Run<Startup>(args);
}


// using Swashbuckle.SwaggerGen.XmlComments;
public class ApplyXmlTypeCommentsFixed : ApplyXmlTypeComments
{
public ApplyXmlTypeCommentsFixed() : base("")
{
throw new NotImplementedException();
}

public ApplyXmlTypeCommentsFixed(string filePath): base(filePath)
{
}
}

public class ApplyXmlActionCommentsFixed : ApplyXmlActionComments
{
public ApplyXmlActionCommentsFixed() : base("")
{
throw new NotImplementedException();
}

public ApplyXmlActionCommentsFixed(string filePath): base(filePath)
{
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Verbose",
"Default": "Information",
"System": "Information",
"Microsoft": "Information"
}
Expand Down
Loading

0 comments on commit ae73bb7

Please sign in to comment.