├── src └── AspNetWindowsService │ ├── index.html │ ├── global.json │ ├── Properties │ └── launchSettings.json │ ├── TimeController.cs │ ├── project.json │ ├── AspNetWindowsService.xproj │ └── Program.cs ├── uninstall.cmd ├── install.cmd ├── LICENSE ├── README.md ├── AspNetWindowsService.sln ├── .gitattributes └── .gitignore /src/AspNetWindowsService/index.html: -------------------------------------------------------------------------------- 1 | hello -------------------------------------------------------------------------------- /src/AspNetWindowsService/global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "1.0.0-rc1-final" 4 | } 5 | } 6 | -------------------------------------------------------------------------------- /uninstall.cmd: -------------------------------------------------------------------------------- 1 | set service_name=%1 2 | if "%service_name%"=="" (set service_name=AspNetWindowsService) 3 | 4 | sc stop %service_name% 5 | sc delete %service_name% 6 | -------------------------------------------------------------------------------- /src/AspNetWindowsService/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "run": { 4 | "commandName": "run", 5 | "sdkVersion": "dnx-clr-win-x86.1.0.0-rc1-final" 6 | } 7 | } 8 | } -------------------------------------------------------------------------------- /src/AspNetWindowsService/TimeController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNet.Mvc; 2 | using System; 3 | 4 | namespace MyDnxService 5 | { 6 | public class TimeController 7 | { 8 | [Route("time")] 9 | public DateTime GetTime() 10 | { 11 | return DateTime.Now; 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /install.cmd: -------------------------------------------------------------------------------- 1 | set service_name=%1 2 | if "%service_name%"=="" (set service_name=AspNetWindowsService) 3 | 4 | call dnu restore 5 | call dnu publish src\AspNetWindowsService --out publish-output --runtime active --no-source 6 | sc create %service_name% binPath= "\"%~dp0publish-output\approot\run.cmd\" --windows-service" 7 | sc start %service_name% 8 | -------------------------------------------------------------------------------- /src/AspNetWindowsService/project.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "1.0.0-*", 3 | "description": "Shell project for hosting ASP.NET in a Windows service", 4 | "commands": { 5 | "run": "AspNetWindowsService" 6 | }, 7 | "frameworks": { 8 | "dnx451": { 9 | "frameworkAssemblies": { 10 | "System.ServiceProcess": "4.0.0.0" 11 | } 12 | } 13 | }, 14 | "dependencies": { 15 | "Microsoft.AspNet.Hosting": "1.0.0-rc1-final", 16 | "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", 17 | "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final", 18 | "Microsoft.AspNet.Mvc": "6.0.0-rc1-final" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Taskmatics 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /src/AspNetWindowsService/AspNetWindowsService.xproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 14.0 5 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 6 | 7 | 8 | 9 | 26950a60-193a-46b6-8430-21e2b59c5214 10 | MyDnxService 11 | ..\..\artifacts\obj\$(MSBuildProjectName) 12 | ..\..\artifacts\bin\$(MSBuildProjectName)\ 13 | 14 | 15 | 2.0 16 | True 17 | Disabled 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | aspnet-windows-service 2 | ======= 3 | 4 | This repository provides a shell project that you can use to get a Windows service hosting ASP.NET 5 with static files and MVC 6. 5 | 6 | For detailed information, please read the following post: [How to Host ASP.NET in a Windows Service](http://taskmatics.com/blog/host-asp-net-in-a-windows-service/) 7 | 8 | ### Installation 9 | 10 | #### 0. Run a Command Prompt as Administrator 11 | This is needed in order to run the `install` command. 12 | 13 | #### 1. Clone the repository 14 | Run `git clone https://github.com/taskmatics/aspnet-windows-service` 15 | 16 | Run `cd aspnet-windows-service` 17 | 18 | #### 2. Install the service 19 | Run `install ` (`AspNetWindowsService` is used by default for the name) 20 | 21 | This command does a few things: 22 | * Publishes the project to an output folder (`.\publish-output`) 23 | * Installs the Windows service (using the service name provided) and points to the `run.cmd` in the published output folder 24 | * Starts the service 25 | 26 | #### Browsing the Website 27 | To view static content (from `index.html`), launch a browser and navigate to `http://localhost:5000`. The port is configurable in code in the `src\Program.cs`. 28 | 29 | To view MVC output (from `TimeController`), launch a browser and navigate to `http://localhost:5000/time`. The port is configurable in code in the `src\Program.cs`. 30 | 31 | #### Uninstalling 32 | Run `uninstall ` (The name must match the one used during install.) 33 | 34 | This command will stop and uninstall the Windows service (using the service name provided). -------------------------------------------------------------------------------- /AspNetWindowsService.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 14 4 | VisualStudioVersion = 14.0.23107.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{58DCF7A9-2DB5-4D3C-840D-D0637DF256AF}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{4AEF0F43-655C-4AE2-ADB1-3993A0895B6F}" 9 | ProjectSection(SolutionItems) = preProject 10 | src\AspNetWindowsService\global.json = src\AspNetWindowsService\global.json 11 | EndProjectSection 12 | EndProject 13 | Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "AspNetWindowsService", "src\AspNetWindowsService\AspNetWindowsService.xproj", "{26950A60-193A-46B6-8430-21E2B59C5214}" 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|Any CPU = Debug|Any CPU 18 | Release|Any CPU = Release|Any CPU 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {26950A60-193A-46B6-8430-21E2B59C5214}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {26950A60-193A-46B6-8430-21E2B59C5214}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {26950A60-193A-46B6-8430-21E2B59C5214}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {26950A60-193A-46B6-8430-21E2B59C5214}.Release|Any CPU.Build.0 = Release|Any CPU 25 | EndGlobalSection 26 | GlobalSection(SolutionProperties) = preSolution 27 | HideSolutionNode = FALSE 28 | EndGlobalSection 29 | GlobalSection(NestedProjects) = preSolution 30 | {26950A60-193A-46B6-8430-21E2B59C5214} = {58DCF7A9-2DB5-4D3C-840D-D0637DF256AF} 31 | EndGlobalSection 32 | EndGlobal 33 | -------------------------------------------------------------------------------- /src/AspNetWindowsService/Program.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNet.Builder; 2 | using Microsoft.AspNet.Hosting; 3 | using Microsoft.AspNet.Hosting.Internal; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.Configuration.Memory; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using System; 8 | using System.Diagnostics; 9 | using System.Linq; 10 | using System.ServiceProcess; 11 | 12 | namespace MyDnxService 13 | { 14 | public class Program : ServiceBase 15 | { 16 | private IApplication _application; 17 | 18 | public static void Main(string[] args) 19 | { 20 | try 21 | { 22 | if (args.Contains("--windows-service")) 23 | { 24 | Run(new Program()); 25 | Debug.WriteLine("Exiting"); 26 | return; 27 | } 28 | 29 | var program = new Program(); 30 | program.OnStart(null); 31 | Console.ReadLine(); 32 | program.OnStop(); 33 | } 34 | catch (Exception ex) 35 | { 36 | Debug.WriteLine(ex); 37 | throw; 38 | } 39 | } 40 | 41 | protected override void OnStart(string[] args) 42 | { 43 | try 44 | { 45 | var configProvider = new MemoryConfigurationProvider(); 46 | configProvider.Add("server.urls", "http://localhost:5000"); 47 | 48 | var config = new ConfigurationBuilder() 49 | .Add(configProvider) 50 | .Build(); 51 | 52 | var builder = new WebHostBuilder(config); 53 | builder.UseServer("Microsoft.AspNet.Server.Kestrel"); 54 | builder.UseServices(services => services.AddMvc()); 55 | builder.UseStartup(appBuilder => 56 | { 57 | appBuilder.UseDefaultFiles(); 58 | appBuilder.UseStaticFiles(); 59 | appBuilder.UseMvc(); 60 | }); 61 | 62 | var hostingEngine = builder.Build(); 63 | _application = hostingEngine.Start(); 64 | } 65 | catch (Exception ex) 66 | { 67 | Debug.WriteLine("error in OnStart: " + ex); 68 | throw; 69 | } 70 | } 71 | 72 | protected override void OnStop() 73 | { 74 | _application?.Dispose(); 75 | } 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | build/ 21 | bld/ 22 | [Bb]in/ 23 | [Oo]bj/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | 28 | # MSTest test Results 29 | [Tt]est[Rr]esult*/ 30 | [Bb]uild[Ll]og.* 31 | 32 | # NUNIT 33 | *.VisualState.xml 34 | TestResult.xml 35 | 36 | # Build Results of an ATL Project 37 | [Dd]ebugPS/ 38 | [Rr]eleasePS/ 39 | dlldata.c 40 | 41 | # DNX 42 | project.lock.json 43 | artifacts/ 44 | 45 | *_i.c 46 | *_p.c 47 | *_i.h 48 | *.ilk 49 | *.meta 50 | *.obj 51 | *.pch 52 | *.pdb 53 | *.pgc 54 | *.pgd 55 | *.rsp 56 | *.sbr 57 | *.tlb 58 | *.tli 59 | *.tlh 60 | *.tmp 61 | *.tmp_proj 62 | *.log 63 | *.vspscc 64 | *.vssscc 65 | .builds 66 | *.pidb 67 | *.svclog 68 | *.scc 69 | 70 | # Chutzpah Test files 71 | _Chutzpah* 72 | 73 | # Visual C++ cache files 74 | ipch/ 75 | *.aps 76 | *.ncb 77 | *.opensdf 78 | *.sdf 79 | *.cachefile 80 | 81 | # Visual Studio profiler 82 | *.psess 83 | *.vsp 84 | *.vspx 85 | 86 | # TFS 2012 Local Workspace 87 | $tf/ 88 | 89 | # Guidance Automation Toolkit 90 | *.gpState 91 | 92 | # ReSharper is a .NET coding add-in 93 | _ReSharper*/ 94 | *.[Rr]e[Ss]harper 95 | *.DotSettings.user 96 | 97 | # JustCode is a .NET coding add-in 98 | .JustCode 99 | 100 | # TeamCity is a build add-in 101 | _TeamCity* 102 | 103 | # DotCover is a Code Coverage Tool 104 | *.dotCover 105 | 106 | # NCrunch 107 | _NCrunch_* 108 | .*crunch*.local.xml 109 | 110 | # MightyMoose 111 | *.mm.* 112 | AutoTest.Net/ 113 | 114 | # Web workbench (sass) 115 | .sass-cache/ 116 | 117 | # Installshield output folder 118 | [Ee]xpress/ 119 | 120 | # DocProject is a documentation generator add-in 121 | DocProject/buildhelp/ 122 | DocProject/Help/*.HxT 123 | DocProject/Help/*.HxC 124 | DocProject/Help/*.hhc 125 | DocProject/Help/*.hhk 126 | DocProject/Help/*.hhp 127 | DocProject/Help/Html2 128 | DocProject/Help/html 129 | 130 | # Click-Once directory 131 | publish/ 132 | 133 | # Publish Web Output 134 | *.[Pp]ublish.xml 135 | *.azurePubxml 136 | ## TODO: Comment the next line if you want to checkin your 137 | ## web deploy settings but do note that will include unencrypted 138 | ## passwords 139 | #*.pubxml 140 | 141 | *.publishproj 142 | 143 | # NuGet Packages 144 | *.nupkg 145 | # The packages folder can be ignored because of Package Restore 146 | **/packages/* 147 | # except build/, which is used as an MSBuild target. 148 | !**/packages/build/ 149 | # Uncomment if necessary however generally it will be regenerated when needed 150 | #!**/packages/repositories.config 151 | 152 | # Windows Azure Build Output 153 | csx/ 154 | *.build.csdef 155 | 156 | # Windows Store app package directory 157 | AppPackages/ 158 | 159 | # Visual Studio cache files 160 | # files ending in .cache can be ignored 161 | *.[Cc]ache 162 | # but keep track of directories ending in .cache 163 | !*.[Cc]ache/ 164 | 165 | # Others 166 | ClientBin/ 167 | [Ss]tyle[Cc]op.* 168 | ~$* 169 | *~ 170 | *.dbmdl 171 | *.dbproj.schemaview 172 | *.pfx 173 | *.publishsettings 174 | node_modules/ 175 | orleans.codegen.cs 176 | 177 | # RIA/Silverlight projects 178 | Generated_Code/ 179 | 180 | # Backup & report files from converting an old project file 181 | # to a newer Visual Studio version. Backup files are not needed, 182 | # because we have git ;-) 183 | _UpgradeReport_Files/ 184 | Backup*/ 185 | UpgradeLog*.XML 186 | UpgradeLog*.htm 187 | 188 | # SQL Server files 189 | *.mdf 190 | *.ldf 191 | 192 | # Business Intelligence projects 193 | *.rdl.data 194 | *.bim.layout 195 | *.bim_*.settings 196 | 197 | # Microsoft Fakes 198 | FakesAssemblies/ 199 | 200 | # Node.js Tools for Visual Studio 201 | .ntvs_analysis.dat 202 | 203 | # Visual Studio 6 build log 204 | *.plg 205 | 206 | # Visual Studio 6 workspace options file 207 | *.opt 208 | 209 | # LightSwitch generated files 210 | GeneratedArtifacts/ 211 | _Pvt_Extensions/ 212 | ModelManifest.xml 213 | /publish-output 214 | --------------------------------------------------------------------------------