├── .nuget
├── NuGet.exe
├── NuGet.Config
└── NuGet.targets
├── OWINTest.Service
├── install.cmd
├── uninstall.cmd
├── App.config
├── API
│ ├── RoutedController.cs
│ └── PersonController.cs
├── ProjectInstaller.cs
├── Program.cs
├── packages.config
├── APIServiceTest.cs
├── Startup.cs
├── APIServiceTest.Designer.cs
├── Properties
│ └── AssemblyInfo.cs
├── ProjectInstaller.Designer.cs
├── ProjectInstaller.resx
└── OWINTest.Service.csproj
├── OWINTest.API
├── packages.config
├── ValuesController.cs
├── Properties
│ └── AssemblyInfo.cs
└── OWINTest.API.csproj
├── LICENSE
├── OWINTest.sln
├── .gitignore
└── README.md
/.nuget/NuGet.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/danesparza/OWIN-WebAPI-Service/HEAD/.nuget/NuGet.exe
--------------------------------------------------------------------------------
/OWINTest.Service/install.cmd:
--------------------------------------------------------------------------------
1 | path = %path%;C:\Windows\Microsoft.NET\Framework\v4.0.30319;
2 |
3 | installutil OWINTest.Service.exe
4 |
5 | pause
--------------------------------------------------------------------------------
/OWINTest.Service/uninstall.cmd:
--------------------------------------------------------------------------------
1 | path = %path%;C:\Windows\Microsoft.NET\Framework\v4.0.30319;
2 |
3 | installutil /u OWINTest.Service.exe
4 |
5 | pause
--------------------------------------------------------------------------------
/.nuget/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/OWINTest.Service/App.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/OWINTest.API/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/OWINTest.Service/API/RoutedController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Web.Http;
7 |
8 | namespace OWINTest.Service.API
9 | {
10 | [RoutePrefix("api/testing")]
11 | public class RoutedController : ApiController
12 | {
13 | [Route("getall")]
14 | public IEnumerable GetAllItems()
15 | {
16 | return new string[] { "value1", "value2" };
17 | }
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/OWINTest.Service/ProjectInstaller.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections;
3 | using System.Collections.Generic;
4 | using System.ComponentModel;
5 | using System.Configuration.Install;
6 | using System.Linq;
7 | using System.Threading.Tasks;
8 |
9 | namespace OWINTest.Service
10 | {
11 | [RunInstaller(true)]
12 | public partial class ProjectInstaller : System.Configuration.Install.Installer
13 | {
14 | public ProjectInstaller()
15 | {
16 | InitializeComponent();
17 | }
18 |
19 | private void serviceInstaller1_AfterInstall(object sender, InstallEventArgs e)
20 | {
21 |
22 | }
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/OWINTest.Service/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.ServiceProcess;
5 | using System.Text;
6 | using System.Threading.Tasks;
7 |
8 | namespace OWINTest.Service
9 | {
10 | static class Program
11 | {
12 | ///
13 | /// The main entry point for the application.
14 | ///
15 | static void Main()
16 | {
17 | ServiceBase[] ServicesToRun;
18 | ServicesToRun = new ServiceBase[]
19 | {
20 | new APIServiceTest()
21 | };
22 | ServiceBase.Run(ServicesToRun);
23 | }
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/OWINTest.Service/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/OWINTest.API/ValuesController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Web.Http;
7 |
8 | namespace OWINTest.API
9 | {
10 | public class ValuesController : ApiController
11 | {
12 | // GET api/values
13 | public IEnumerable Get()
14 | {
15 | return new string[] { "value1", "value2" };
16 | }
17 |
18 | // GET api/values/5
19 | public string Get(int id)
20 | {
21 | return "value";
22 | }
23 |
24 | // POST api/values
25 | public void Post([FromBody]string value)
26 | {
27 |
28 | }
29 |
30 | // PUT api/values/5
31 | public void Put(int id, [FromBody]string value)
32 | {
33 |
34 | }
35 |
36 | // DELETE api/values/5
37 | public void Delete(int id)
38 | {
39 |
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/OWINTest.Service/API/PersonController.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Web.Http;
7 |
8 | namespace OWINTest.Service.API
9 | {
10 | public class PersonController : ApiController
11 | {
12 | // GET api/values
13 | public IEnumerable Get()
14 | {
15 | return new string[] { "value1", "value2" };
16 | }
17 |
18 | // GET api/values/5
19 | public string Get(int id)
20 | {
21 | return "value";
22 | }
23 |
24 | // POST api/values
25 | public void Post([FromBody]string value)
26 | {
27 |
28 | }
29 |
30 | // PUT api/values/5
31 | public void Put(int id, [FromBody]string value)
32 | {
33 |
34 | }
35 |
36 | // DELETE api/values/5
37 | public void Delete(int id)
38 | {
39 |
40 | }
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/OWINTest.Service/APIServiceTest.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.ComponentModel;
4 | using System.Data;
5 | using System.Diagnostics;
6 | using System.Linq;
7 | using System.ServiceProcess;
8 | using System.Text;
9 | using System.Threading.Tasks;
10 | using Microsoft.Owin.Hosting;
11 |
12 | namespace OWINTest.Service
13 | {
14 | public partial class APIServiceTest : ServiceBase
15 | {
16 | public string baseAddress = "http://localhost:9000/";
17 | private IDisposable _server = null;
18 |
19 | public APIServiceTest()
20 | {
21 | InitializeComponent();
22 | }
23 |
24 | protected override void OnStart(string[] args)
25 | {
26 | _server = WebApp.Start(url: baseAddress);
27 | }
28 |
29 | protected override void OnStop()
30 | {
31 | if(_server != null)
32 | {
33 | _server.Dispose();
34 | }
35 | base.OnStop();
36 | }
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Dan Esparza
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy of
6 | this software and associated documentation files (the "Software"), to deal in
7 | the Software without restriction, including without limitation the rights to
8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 | the Software, and to permit persons to whom the Software is furnished to do so,
10 | 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, FITNESS
17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/OWINTest.Service/Startup.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using System.Text;
5 | using System.Threading.Tasks;
6 | using System.Web.Http;
7 | using Owin;
8 |
9 | namespace OWINTest.Service
10 | {
11 | class Startup
12 | {
13 | // Hack from http://stackoverflow.com/a/17227764/19020 to load controllers in
14 | // another assembly. Another way to do this is to create a custom assembly resolver
15 | Type valuesControllerType = typeof(OWINTest.API.ValuesController);
16 |
17 | // This code configures Web API. The Startup class is specified as a type
18 | // parameter in the WebApp.Start method.
19 | public void Configuration(IAppBuilder appBuilder)
20 | {
21 | // Configure Web API for self-host.
22 | HttpConfiguration config = new HttpConfiguration();
23 |
24 | // Enable attribute based routing
25 | // http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
26 | config.MapHttpAttributeRoutes();
27 |
28 | config.Routes.MapHttpRoute(
29 | name: "DefaultApi",
30 | routeTemplate: "api/{controller}/{id}",
31 | defaults: new { id = RouteParameter.Optional }
32 | );
33 |
34 | appBuilder.UseWebApi(config);
35 | }
36 | }
37 | }
38 |
--------------------------------------------------------------------------------
/OWINTest.Service/APIServiceTest.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace OWINTest.Service
2 | {
3 | partial class APIServiceTest
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if(disposing)
17 | {
18 | if (_server != null)
19 | {
20 | _server.Dispose();
21 | }
22 | if (components != null)
23 | {
24 | components.Dispose();
25 | }
26 | }
27 | base.Dispose(disposing);
28 | }
29 |
30 | #region Component Designer generated code
31 |
32 | ///
33 | /// Required method for Designer support - do not modify
34 | /// the contents of this method with the code editor.
35 | ///
36 | private void InitializeComponent()
37 | {
38 | components = new System.ComponentModel.Container();
39 | this.ServiceName = "Service1";
40 | }
41 |
42 | #endregion
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/OWINTest.API/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
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("OWINTest.API")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("OWINTest.API")]
13 | [assembly: AssemblyCopyright("Copyright © 2014")]
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("d611539e-d658-4ebf-948e-98cc39d62ccf")]
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")]
37 |
--------------------------------------------------------------------------------
/OWINTest.Service/Properties/AssemblyInfo.cs:
--------------------------------------------------------------------------------
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("OWINTest.Service")]
9 | [assembly: AssemblyDescription("")]
10 | [assembly: AssemblyConfiguration("")]
11 | [assembly: AssemblyCompany("")]
12 | [assembly: AssemblyProduct("OWINTest.Service")]
13 | [assembly: AssemblyCopyright("Copyright © 2014")]
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("3c680edc-55b3-4c07-885e-15adc849c05e")]
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")]
37 |
--------------------------------------------------------------------------------
/OWINTest.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.30110.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OWINTest.Service", "OWINTest.Service\OWINTest.Service.csproj", "{040449FC-D996-49AF-BD74-D137D5688992}"
7 | EndProject
8 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OWINTest.API", "OWINTest.API\OWINTest.API.csproj", "{1671F393-642C-46EA-9FA7-58F53818E724}"
9 | EndProject
10 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{A89D3C9D-EDE8-4518-92A7-045C5DB112A5}"
11 | ProjectSection(SolutionItems) = preProject
12 | .nuget\NuGet.Config = .nuget\NuGet.Config
13 | .nuget\NuGet.exe = .nuget\NuGet.exe
14 | .nuget\NuGet.targets = .nuget\NuGet.targets
15 | EndProjectSection
16 | EndProject
17 | Global
18 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
19 | Debug|Any CPU = Debug|Any CPU
20 | Release|Any CPU = Release|Any CPU
21 | EndGlobalSection
22 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
23 | {040449FC-D996-49AF-BD74-D137D5688992}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
24 | {040449FC-D996-49AF-BD74-D137D5688992}.Debug|Any CPU.Build.0 = Debug|Any CPU
25 | {040449FC-D996-49AF-BD74-D137D5688992}.Release|Any CPU.ActiveCfg = Release|Any CPU
26 | {040449FC-D996-49AF-BD74-D137D5688992}.Release|Any CPU.Build.0 = Release|Any CPU
27 | {1671F393-642C-46EA-9FA7-58F53818E724}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
28 | {1671F393-642C-46EA-9FA7-58F53818E724}.Debug|Any CPU.Build.0 = Debug|Any CPU
29 | {1671F393-642C-46EA-9FA7-58F53818E724}.Release|Any CPU.ActiveCfg = Release|Any CPU
30 | {1671F393-642C-46EA-9FA7-58F53818E724}.Release|Any CPU.Build.0 = Release|Any CPU
31 | EndGlobalSection
32 | GlobalSection(SolutionProperties) = preSolution
33 | HideSolutionNode = FALSE
34 | EndGlobalSection
35 | EndGlobal
36 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # Build Folders (you can keep bin if you'd like, to store dlls and pdbs)
2 | [Bb]in/
3 | [Oo]bj/
4 |
5 | # mstest test results
6 | TestResults
7 |
8 | ## Ignore Visual Studio temporary files, build results, and
9 | ## files generated by popular Visual Studio add-ons.
10 |
11 | # User-specific files
12 | *.suo
13 | *.user
14 | *.sln.docstates
15 |
16 | # Build results
17 | [Dd]ebug/
18 | [Rr]elease/
19 | x64/
20 | *_i.c
21 | *_p.c
22 | *.ilk
23 | *.meta
24 | *.obj
25 | *.pch
26 | *.pdb
27 | *.pgc
28 | *.pgd
29 | *.rsp
30 | *.sbr
31 | *.tlb
32 | *.tli
33 | *.tlh
34 | *.tmp
35 | *.log
36 | *.vspscc
37 | *.vssscc
38 | .builds
39 |
40 | # Visual C++ cache files
41 | ipch/
42 | *.aps
43 | *.ncb
44 | *.opensdf
45 | *.sdf
46 |
47 | # Visual Studio profiler
48 | *.psess
49 | *.vsp
50 | *.vspx
51 |
52 | # Guidance Automation Toolkit
53 | *.gpState
54 |
55 | # ReSharper is a .NET coding add-in
56 | _ReSharper*
57 |
58 | # NCrunch
59 | *.ncrunch*
60 | .*crunch*.local.xml
61 |
62 | # Installshield output folder
63 | [Ee]xpress
64 |
65 | # DocProject is a documentation generator add-in
66 | DocProject/buildhelp/
67 | DocProject/Help/*.HxT
68 | DocProject/Help/*.HxC
69 | DocProject/Help/*.hhc
70 | DocProject/Help/*.hhk
71 | DocProject/Help/*.hhp
72 | DocProject/Help/Html2
73 | DocProject/Help/html
74 |
75 | # Click-Once directory
76 | publish
77 |
78 | # Publish Web Output
79 | *.Publish.xml
80 |
81 | # NuGet Packages Directory
82 | packages
83 |
84 | # Windows Azure Build Output
85 | csx
86 | *.build.csdef
87 |
88 | # Windows Store app package directory
89 | AppPackages/
90 |
91 | # Others
92 | [Bb]in
93 | [Oo]bj
94 | sql
95 | TestResults
96 | [Tt]est[Rr]esult*
97 | *.Cache
98 | ClientBin
99 | [Ss]tyle[Cc]op.*
100 | ~$*
101 | *.dbmdl
102 | Generated_Code #added for RIA/Silverlight projects
103 |
104 | # Backup & report files from converting an old project file to a newer
105 | # Visual Studio version. Backup files are not needed, because we have git ;-)
106 | _UpgradeReport_Files/
107 | Backup*/
108 | UpgradeLog*.XML
109 |
--------------------------------------------------------------------------------
/OWINTest.Service/ProjectInstaller.Designer.cs:
--------------------------------------------------------------------------------
1 | namespace OWINTest.Service
2 | {
3 | partial class ProjectInstaller
4 | {
5 | ///
6 | /// Required designer variable.
7 | ///
8 | private System.ComponentModel.IContainer components = null;
9 |
10 | ///
11 | /// Clean up any resources being used.
12 | ///
13 | /// true if managed resources should be disposed; otherwise, false.
14 | protected override void Dispose(bool disposing)
15 | {
16 | if(disposing && (components != null))
17 | {
18 | components.Dispose();
19 | }
20 | base.Dispose(disposing);
21 | }
22 |
23 | #region Component Designer generated code
24 |
25 | ///
26 | /// Required method for Designer support - do not modify
27 | /// the contents of this method with the code editor.
28 | ///
29 | private void InitializeComponent()
30 | {
31 | this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
32 | this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
33 | //
34 | // serviceProcessInstaller1
35 | //
36 | this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
37 | this.serviceProcessInstaller1.Password = null;
38 | this.serviceProcessInstaller1.Username = null;
39 | //
40 | // serviceInstaller1
41 | //
42 | this.serviceInstaller1.DisplayName = "OWIN Service Test";
43 | this.serviceInstaller1.ServiceName = "OWINServiceTest";
44 | this.serviceInstaller1.AfterInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_AfterInstall);
45 | //
46 | // ProjectInstaller
47 | //
48 | this.Installers.AddRange(new System.Configuration.Install.Installer[] {
49 | this.serviceProcessInstaller1,
50 | this.serviceInstaller1});
51 |
52 | }
53 |
54 | #endregion
55 |
56 | private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
57 | private System.ServiceProcess.ServiceInstaller serviceInstaller1;
58 | }
59 | }
--------------------------------------------------------------------------------
/OWINTest.API/OWINTest.API.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {1671F393-642C-46EA-9FA7-58F53818E724}
8 | Library
9 | Properties
10 | OWINTest.API
11 | OWINTest.API
12 | v4.5
13 | 512
14 | ..\
15 | true
16 |
17 |
18 | true
19 | full
20 | false
21 | bin\Debug\
22 | DEBUG;TRACE
23 | prompt
24 | 4
25 |
26 |
27 | pdbonly
28 | true
29 | bin\Release\
30 | TRACE
31 | prompt
32 | 4
33 |
34 |
35 |
36 | ..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll
37 |
38 |
39 |
40 |
41 |
42 | False
43 | ..\packages\Microsoft.AspNet.WebApi.Client.5.1.0\lib\net45\System.Net.Http.Formatting.dll
44 |
45 |
46 | False
47 | ..\packages\Microsoft.AspNet.WebApi.Core.5.1.0\lib\net45\System.Web.Http.dll
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
67 |
68 |
69 |
70 |
77 |
--------------------------------------------------------------------------------
/OWINTest.Service/ProjectInstaller.resx:
--------------------------------------------------------------------------------
1 |
2 |
3 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 |
105 |
106 |
107 |
108 |
109 | text/microsoft-resx
110 |
111 |
112 | 2.0
113 |
114 |
115 | System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
116 |
117 |
118 | System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
119 |
120 |
121 | 17, 56
122 |
123 |
124 | 196, 17
125 |
126 |
127 | False
128 |
129 |
--------------------------------------------------------------------------------
/OWINTest.Service/OWINTest.Service.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | Debug
6 | AnyCPU
7 | {040449FC-D996-49AF-BD74-D137D5688992}
8 | WinExe
9 | Properties
10 | OWINTest.Service
11 | OWINTest.Service
12 | v4.5
13 | 512
14 | ..\
15 | true
16 |
17 |
18 | AnyCPU
19 | true
20 | full
21 | false
22 | bin\Debug\
23 | DEBUG;TRACE
24 | prompt
25 | 4
26 |
27 |
28 | AnyCPU
29 | pdbonly
30 | true
31 | bin\Release\
32 | TRACE
33 | prompt
34 | 4
35 |
36 |
37 |
38 | ..\packages\Microsoft.Owin.2.0.2\lib\net45\Microsoft.Owin.dll
39 |
40 |
41 | ..\packages\Microsoft.Owin.Host.HttpListener.2.0.2\lib\net45\Microsoft.Owin.Host.HttpListener.dll
42 |
43 |
44 | ..\packages\Microsoft.Owin.Hosting.2.0.2\lib\net45\Microsoft.Owin.Hosting.dll
45 |
46 |
47 | False
48 | ..\packages\Newtonsoft.Json.4.5.11\lib\net40\Newtonsoft.Json.dll
49 |
50 |
51 | ..\packages\Owin.1.0\lib\net40\Owin.dll
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 | False
60 | ..\packages\Microsoft.AspNet.WebApi.Client.5.1.0\lib\net45\System.Net.Http.Formatting.dll
61 |
62 |
63 | False
64 | ..\packages\Microsoft.AspNet.WebApi.Core.5.1.0\lib\net45\System.Web.Http.dll
65 |
66 |
67 | ..\packages\Microsoft.AspNet.WebApi.Owin.5.1.0\lib\net45\System.Web.Http.Owin.dll
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | Component
79 |
80 |
81 | APIServiceTest.cs
82 |
83 |
84 |
85 |
86 |
87 | Component
88 |
89 |
90 | ProjectInstaller.cs
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 | PreserveNewest
99 |
100 |
101 |
102 | PreserveNewest
103 |
104 |
105 |
106 |
107 | {1671f393-642c-46ea-9fa7-58f53818e724}
108 | OWINTest.API
109 |
110 |
111 |
112 |
113 | ProjectInstaller.cs
114 |
115 |
116 |
117 |
118 |
119 |
120 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
121 |
122 |
123 |
124 |
131 |
--------------------------------------------------------------------------------
/.nuget/NuGet.targets:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | $(MSBuildProjectDirectory)\..\
5 |
6 |
7 | false
8 |
9 |
10 | false
11 |
12 |
13 | true
14 |
15 |
16 | false
17 |
18 |
19 |
20 |
21 |
22 |
26 |
27 |
28 |
29 |
30 | $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))
31 |
32 |
33 |
34 |
35 | $(SolutionDir).nuget
36 |
37 |
38 |
39 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName.Replace(' ', '_')).config
40 | $(MSBuildProjectDirectory)\packages.$(MSBuildProjectName).config
41 |
42 |
43 |
44 | $(MSBuildProjectDirectory)\packages.config
45 | $(PackagesProjectConfig)
46 |
47 |
48 |
49 |
50 | $(NuGetToolsPath)\NuGet.exe
51 | @(PackageSource)
52 |
53 | "$(NuGetExePath)"
54 | mono --runtime=v4.0.30319 "$(NuGetExePath)"
55 |
56 | $(TargetDir.Trim('\\'))
57 |
58 | -RequireConsent
59 | -NonInteractive
60 |
61 | "$(SolutionDir) "
62 | "$(SolutionDir)"
63 |
64 |
65 | $(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)" $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)
66 | $(NuGetCommand) pack "$(ProjectPath)" -Properties "Configuration=$(Configuration);Platform=$(Platform)" $(NonInteractiveSwitch) -OutputDirectory "$(PackageOutputDir)" -symbols
67 |
68 |
69 |
70 | RestorePackages;
71 | $(BuildDependsOn);
72 |
73 |
74 |
75 |
76 | $(BuildDependsOn);
77 | BuildPackage;
78 |
79 |
80 |
81 |
82 |
83 |
84 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
99 |
100 |
103 |
104 |
105 |
106 |
108 |
109 |
112 |
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 |
125 |
126 |
141 |
142 |
143 |
144 |
145 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | OWIN WebAPI Service example [](https://opensource.org/licenses/MIT)
2 | ===========================
3 |
4 | Sometimes, you just need a good example to get started.
5 |
6 | The [OWIN-WebAPI-Service project](https://github.com/danesparza/OWIN-WebAPI-Service) came out of a need to create a self-hosted WebAPI 2 service in a Windows service. Microsoft says that going forward, [OWIN is the way to go](http://www.asp.net/web-api/overview/hosting-aspnet-web-api/self-host-a-web-api). I wanted to use [attribute routing](http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2) in WebAPI 2. I couldn't find a decent example anywhere, so I created my own.
7 |
8 | *Please be aware that OWIN (and this project template) are only compatible with .NET 4.5 and newer projects.*
9 |
10 | ## If starting from scratch:
11 |
12 | ### Create the service project ###
13 | If you're starting from scratch, add a new service project to your solution by selecting **'Windows Service'** in the new project template.
14 |
15 | ### Add the OWIN Nuget packages ###
16 |
17 | From the package manager console:
18 |
19 | ```powershell
20 | Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
21 | ```
22 |
23 | This will install the following dependent packages automatically:
24 | * Microsoft.AspNet.WebApi.Client
25 | * Microsoft.AspNet.WebApi.Core
26 | * Microsoft.AspNet.WebApi.Owin
27 | * Microsoft.AspNet.WebApi.OwinSelfHost
28 | * Microsoft.Owin
29 | * Microsoft.Owin.Host.HttpListener
30 | * Microsoft.Owin.Hosting
31 | * Newtonsoft.Json
32 | * Owin
33 |
34 | ### Create an OWIN configuration handler
35 | Create the file `Startup.cs` and put a configuration handler in it:
36 |
37 | ```CSharp
38 | class Startup
39 | {
40 | // Hack from http://stackoverflow.com/a/17227764/19020 to load controllers in
41 | // another assembly. Another way to do this is to create a custom assembly resolver
42 | Type valuesControllerType = typeof(OWINTest.API.ValuesController);
43 |
44 | // This code configures Web API. The Startup class is specified as a type
45 | // parameter in the WebApp.Start method.
46 | public void Configuration(IAppBuilder appBuilder)
47 | {
48 | // Configure Web API for self-host.
49 | HttpConfiguration config = new HttpConfiguration();
50 |
51 | // Enable attribute based routing
52 | // http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2
53 | config.MapHttpAttributeRoutes();
54 |
55 | config.Routes.MapHttpRoute(
56 | name: "DefaultApi",
57 | routeTemplate: "api/{controller}/{id}",
58 | defaults: new { id = RouteParameter.Optional }
59 | );
60 |
61 | appBuilder.UseWebApi(config);
62 | }
63 | }
64 | ```
65 |
66 | Note that:
67 | * You can load API controllers from another assembly by using the hack `Type valuesControllerType = typeof(OWINTest.API.ValuesController);` or by creating a [custom assembly resolver](http://www.strathweb.com/2013/08/customizing-controller-discovery-in-asp-net-web-api/)
68 | * You can use Attribute based routing by including the line `config.MapHttpAttributeRoutes()` before the default `config.Routes.MapHttpRoute`
69 |
70 | ### Add API controllers
71 | Add API controllers to the service project by creating classes inherited from `ApiController`. Here is a simple example that uses attribute based routing:
72 |
73 | ```CSharp
74 | using System;
75 | using System.Collections.Generic;
76 | using System.Linq;
77 | using System.Text;
78 | using System.Threading.Tasks;
79 | using System.Web.Http;
80 |
81 | namespace OWINTest.Service.API
82 | {
83 | [RoutePrefix("api/testing")]
84 | public class RoutedController : ApiController
85 | {
86 | [Route("getall")]
87 | public IEnumerable GetAllItems()
88 | {
89 | return new string[] { "value1", "value2" };
90 | }
91 | }
92 | }
93 | ```
94 |
95 | Note that:
96 | * Controllers in the service assembly will be loaded automatically.
97 | * If you want to load a controller in another assembly, you'll need to update your `Startup.cs` file (and read the note about loading controllers from other assemblies, above)
98 |
99 | ### Add code to start/stop the WebAPI listener
100 |
101 | Add code to the default service (inherited from `ServiceBase`) that the Visual Studio template created for you. The finished service class should look something like this:
102 |
103 | ```CSharp
104 | public partial class APIServiceTest : ServiceBase
105 | {
106 | public string baseAddress = "http://localhost:9000/";
107 | private IDisposable _server = null;
108 |
109 | public APIServiceTest()
110 | {
111 | InitializeComponent();
112 | }
113 |
114 | protected override void OnStart(string[] args)
115 | {
116 | _server = WebApp.Start(url: baseAddress);
117 | }
118 |
119 | protected override void OnStop()
120 | {
121 | if(_server != null)
122 | {
123 | _server.Dispose();
124 | }
125 | base.OnStop();
126 | }
127 | }
128 | ```
129 |
130 | See how simple that is?
131 | * In the `OnStart` handler, we start the listener and pass our `Startup` class we created. That calls our configuration handler.
132 | * In the `OnStop` handler, we just stop the listener
133 | * The service will be listening with a base location of `http://localhost:9000`.
134 |
135 | ### Install the service
136 | Create a service installer by right-clicking on the service design surface and selecting 'Add installer' from the context menu. You can update the service name, description, [startup mode](http://superuser.com/a/285655/4508) and [default credentials](http://stackoverflow.com/a/510225/19020) by updating the properties on the 2 new controls that are added.
137 |
138 | After you've [added the service installer](http://msdn.microsoft.com/en-us/library/ddhy0byf(v=vs.110).aspx) by updating the service code, install the service using the [.NET installutil.exe](http://msdn.microsoft.com/en-us/library/50614e95(v=vs.110).aspx). See the sample batch files `install.cmd` and `uninstall.cmd` for an example of making this a little easier on yourself.
139 |
140 | ### Stuff to try
141 | Now that you've compiled and installed your service, start it up in the 'Services' app in the control panel.
142 | * If you've added the `RoutedController` example above, try navigating to the following url in [Postman](http://www.getpostman.com/) or your favorite REST service tester: `http://localhost:9000/api/testing/getall` -- you should get a JSON string array back.
143 | * Try hitting breakpoints in your running service in Visual Studio by selecting 'Debug/Attach to Process'. Select your running service exe, then press 'Attach'.
144 | * Try calling the service directly from a browser-based single page application. (Hint: You won't be able to until you [enable CORS](http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api))
145 |
146 | ## Tips
147 |
148 | ### Building the sample service
149 |
150 | So if you just want to take a look at the sample project, you'll need to either grab the zip or [clone the project](https://help.github.com/articles/which-remote-url-should-i-use/) in git.
151 |
152 | Before you build and install the service you'll need to do a 'Nuget package restore'. The easiest way to do this is probably to right-click on the solution in Visual Studio and select 'Manage Nuget packages for solution...'
153 |
154 | You should see the 'Manage NuGet Packages' screen pop up. At the very top of the screen, you'll probably see a yellow message indicating that 'Some NuGet packages are missing from this solution. Click to restore from your online package sources.' with a Restore button. Go ahead and click Restore and then close the window once the missing packages have been downloaded.
155 |
156 | Try your build again after that, and you should be good.
157 |
158 | ### Installing the service
159 |
160 | You'll need to run the `installutil` command as an Administrator. To do that, you'll need to [run the command prompt itself as Administrator](https://technet.microsoft.com/en-us/library/cc947813%28v=ws.10%29.aspx?f=255&MSPPError=-2147217396), or use [other interesting tricks](http://stackoverflow.com/a/12401075/19020)
161 |
162 | ### Serving more than just localhost
163 |
164 | If you want to listen to all requests coming in a certain port -- not just localhost requests, you'll need to know a few things.
165 |
166 | **First**, understand [there are permission differences between Local System, Local service, Network service](http://stackoverflow.com/a/510225/19020), and a user account. I recommend you run under 'Local service' because it's a minimal set of permissions.
167 |
168 | **Second**, you'll need to change the code that starts the service. Instead of listening for requests to `http://localhost:9000`, you'll need to listen for requests to `http://+:9000`.
169 |
170 | **Third**, you'll need to use the command-line tool `netsh` to authorize 'Local Service' to listen for requests. I usually put this command in the **install.bat** file that installs the service:
171 |
172 | ```bash
173 | netsh http add urlacl url=http://+:9000/ user="Local Service"
174 | ```
175 |
176 | Without this, you'll have problems starting the service and listening to all requests for that port.
177 |
178 | ### Help -- I'm getting Error 1053 when trying to start the service
179 |
180 | If you're getting `Error 1053: The service did not respond to the start or control request in a timely fashion.` there is a good chance you don't have the right version of the .NET framework installed. Remember: OWIN and WebAPI 2 require .NET 4.5 or a more recent version of the framework to be installed.
181 |
182 |
183 |
--------------------------------------------------------------------------------