{"id":9186,"date":"2018-05-31T09:00:33","date_gmt":"2018-05-31T00:00:33","guid":{"rendered":"http:\/\/www.moonmile.net\/blog\/?p=9186"},"modified":"2018-06-01T15:14:24","modified_gmt":"2018-06-01T06:14:24","slug":"net-core%e3%81%a7%e4%bd%9c%e6%88%90%e3%81%97%e3%81%9f-web-api%e3%82%a2%e3%83%97%e3%83%aa%e3%82%92-windows-%e3%82%b5%e3%83%bc%e3%83%93%e3%82%b9%e3%81%a7%e5%8b%95%e3%81%8b%e3%81%99","status":"publish","type":"post","link":"http:\/\/www.moonmile.net\/blog\/archives\/9186","title":{"rendered":".NET Core\u3067\u4f5c\u6210\u3057\u305f Web API\u30a2\u30d7\u30ea\u3092 Windows \u30b5\u30fc\u30d3\u30b9\u3067\u52d5\u304b\u3059"},"content":{"rendered":"<p>\n\u57fa\u672c\u306f\u2193\u306b\u66f8\u3044\u3066\u3042\u308b\u306e\u3060\u3051\u3069\u3001\u6240\u3005\u9593\u9055\u3063\u3066\u3044\u308b\uff1f\u3068\u3044\u3046\u304b\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3042\u305f\u308a\u3067\u52d5\u304b\u306a\u304f\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u30dd\u30a4\u30f3\u30c8\u3060\u3051\u30e1\u30e2\u66f8\u304d\u3002\n<\/p>\n<p>\nWindows \u30b5\u30fc\u30d3\u30b9\u3067 ASP.NET Core \u3092\u30db\u30b9\u30c8\u3057\u307e\u3059\u3002<br \/>\n<a href=\"https:\/\/docs.microsoft.com\/ja-jp\/aspnet\/core\/host-and-deploy\/windows-service?view=aspnetcore-2.0&#038;tabs=aspnetcore2x\">https:\/\/docs.microsoft.com\/ja-jp\/aspnet\/core\/host-and-deploy\/windows-service?view=aspnetcore-2.0&#038;tabs=aspnetcore2x<\/a>\n<\/p>\n<p><a href=\"https:\/\/github.com\/aspnet\/Docs\/tree\/master\/aspnetcore\/host-and-deploy\/windows-service\/sample\">https:\/\/github.com\/aspnet\/Docs\/tree\/master\/aspnetcore\/host-and-deploy\/windows-service\/sample<\/a><\/p>\n<p><h2>*.csproj\u3067\u30bf\u30fc\u30b2\u30c3\u30c8\u3092.NET Framework\u306b\u3059\u308b<\/h2>\n<\/p>\n<p>\nASP.NET Core + .NET Core \u3067\u4f5c\u6210\u3057\u305f\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\uff08*.csproj\uff09\u3092\u958b\u3044\u3066\u3001TargetFramework \u3092\u300cnetcoreapp2.0\u300d\u304b\u3089\u300cnet461\u300d\u306b\u5909\u3048\u308b\u3002\n<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n  &lt;PropertyGroup&gt;\r\n    &lt;TargetFramework&gt;netcoreapp2.0&lt;\/TargetFramework&gt;\r\n  &lt;\/PropertyGroup&gt;\r\n<\/pre>\n<p>\n\u304b\u3089\n<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n  &lt;PropertyGroup&gt;\r\n    &lt;TargetFramework&gt;net461&lt;\/TargetFramework&gt;\r\n    &lt;RuntimeIdentifier&gt;win7-x64&lt;\/RuntimeIdentifier&gt;\r\n  &lt;\/PropertyGroup&gt;\r\n<\/pre>\n<p>\n\u306b\u5909\u66f4\u3059\u308b\u3002\n<\/p>\n<p>\n\u3053\u308c\u306f Windows \u30b5\u30fc\u30d3\u30b9\u3092\u4f7f\u3046\u305f\u3081\u306b .NET Framework \u306b\u5207\u308a\u66ff\u3048\u3066\u3044\u308b\u306e\u3067\u3001Linux\u4e0a\u3067\u52d5\u304b\u3059\u5834\u5408\u306f\u3001netcoreapp2.0 \u3067\u4f7f\u3046\u3053\u3068\u306b\u306a\u308b\u3002\n<\/p>\n<p><h2>NuGet\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u5909\u66f4\u3059\u308b<\/h2>\n<\/p>\n<p>\nASP.NET Core \u3092\u4f7f\u3063\u3066\u3044\u308b\u3068\u3053\u308d\u3092\u3001.NET Core \u304b\u3089 .NET Framework \u306e\u3082\u306e\u306b\u5207\u308a\u66ff\u3048\u308b\u3002\u3042\u3068\u300cMicrosoft.AspNetCore.Hosting.WindowsServices\u300d\u3092\u8ffd\u52a0\u3057\u3066\u3001Windows \u30b5\u30fc\u30d3\u30b9\u3067\u8d77\u52d5\u3059\u308b\u305f\u3081\u306e RunAsService() \u3092\u4f7f\u3048\u308b\u3088\u3046\u306b\u3057\u3066\u304a\u304f\u3002\n<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n  &lt;ItemGroup&gt;\r\n    &lt;PackageReference Include=&amp;quot;Microsoft.AspNetCore.All&amp;quot; Version=&amp;quot;2.0.8&amp;quot; \/&gt;\r\n  &lt;\/ItemGroup&gt;\r\n<\/pre>\n<p>\n\u304b\u3089\n<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n  &lt;ItemGroup&gt;\r\n    &lt;PackageReference Include=&amp;quot;Microsoft.AspNetCore&amp;quot; Version=&amp;quot;2.0.3&amp;quot; \/&gt;\r\n    &lt;PackageReference Include=&amp;quot;Microsoft.AspNetCore.Mvc&amp;quot; Version=&amp;quot;2.0.4&amp;quot; \/&gt;\r\n    &lt;PackageReference Include=&amp;quot;Microsoft.AspNetCore.Hosting.WindowsServices&amp;quot; Version=&amp;quot;2.0.3&amp;quot; \/&gt;\r\n  &lt;\/ItemGroup&gt;\r\n<\/pre>\n<p><h2>Program.cs\u3092\u66f8\u304d\u66ff\u3048\u308b<\/h2>\n<\/p>\n<p>\nASP.NET Core + .NET Core \u306e\u5834\u5408\u306b\u306f\u3001IWebHost \u30a4\u30f3\u30bf\u30fc\u30d5\u30a7\u30fc\u30b9\u3092\u4f7f\u3063\u3066\u3044\u308b\u306e\u3060\u304c\u3001\u9762\u5012\u306a\u306e\u3067\u3001Main \u306b\u66f8\u304d\u66ff\u3048\u3066\u3057\u307e\u3046\u3002\n<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class Program\r\n{\r\n    public static void Main(string&#x5B;] args)\r\n    {\r\n        BuildWebHost(args).Run();\r\n    }\r\n\r\n    public static IWebHost BuildWebHost(string&#x5B;] args) =&gt;\r\n        WebHost.CreateDefaultBuilder(args)\r\n            .UseStartup&lt;Startup&gt;()\r\n            .Build();\r\n}\r\n<\/pre>\n<p>\n\u304b\u3089\n<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing Microsoft.AspNetCore;\r\nusing Microsoft.AspNetCore.Hosting;\r\nusing Microsoft.AspNetCore.Hosting.WindowsServices;\r\n\r\npublic class Program\r\n{\r\n    public static void Main(string&#x5B;] args)\r\n    {\r\n        bool isService = false;\r\n        if (Debugger.IsAttached || args.Contains(&amp;quot;--console&amp;quot;))\r\n        {\r\n            isService = false;\r\n        }\r\n        if ( args.Contains(&amp;quot;--service&amp;quot;))\r\n        {\r\n            isService = true;\r\n        }\r\n        \/\/ \u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6d88\u3057\u3066\u304a\u304f\r\n        args = new string&#x5B;] { };\r\n\r\n        if (isService)\r\n        {\r\n            \/\/ \u30b5\u30fc\u30d3\u30b9\u3067\u52d5\u4f5c\u3055\u305b\u308b\r\n            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;\r\n            var pathToContentRoot = Path.GetDirectoryName(pathToExe);\r\n            var host =\r\n                WebHost.CreateDefaultBuilder(args)\r\n                .UseContentRoot(pathToContentRoot)\r\n                .UseStartup&lt;Startup&gt;()\r\n                .Build();\r\n            host.RunAsService();\r\n        }\r\n        else\r\n        {\r\n            \/\/ \u30b3\u30f3\u30bd\u30fc\u30eb\u30a2\u30d7\u30ea\u3067\u8d77\u52d5\u3059\u308b\r\n            var host = \r\n                WebHost.CreateDefaultBuilder(args)\r\n                .UseStartup&lt;Startup&gt;()\r\n                .UseUrls($&amp;quot;http:\/\/*:{port}&amp;quot;)\r\n                .Build();\r\n            host.Run();\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>\n\u8d77\u52d5\u30b9\u30a4\u30c3\u30c1\u3092\u4ed8\u3051\u3066\u901a\u5e38\u8d77\u52d5\u3068\u30b5\u30fc\u30d3\u30b9\u306e\u8d77\u52d5\u306b\u5207\u308a\u66ff\u3048\u3066\u3044\u308b\u3002MS\u306e\u30b5\u30f3\u30d7\u30eb\u3067\u306f\u3001\u30c7\u30d5\u30a9\u30eb\u30c8\u3067\u30b5\u30fc\u30d3\u30b9\u8d77\u52d5\u306b\u306a\u3063\u3066\u3044\u308b\u306e\u3060\u304c\u3001\u3053\u3061\u3089\u306f\u300c&#8211;service\u300d\u3092\u4ed8\u3051\u305f\u3068\u304d\u3060\u3051\u3001Windows \u30b5\u30fc\u30d3\u30b9\u3067\u8d77\u52d5\u3059\u308b\u3088\u3046\u306b\u3057\u3066\u3044\u308b\u3002\n<\/p>\n<p>\nWebHost.CreateDefaultBuilder \u306b\u4e0d\u8981\u306a\u30d1\u30e9\u30e1\u30fc\u30bf\u3092\u6e21\u3059\u3068\u843d\u3061\u308b\u306e\u3067\u3001args \u306e\u4e2d\u8eab\u3092\u6d88\u3057\u3066\u304a\u304f\uff08\u306a\u3093\u3068\u306a\u304f\u30d0\u30b0\u81ed\u3044\u306e\u3060\u304c\uff09\u3002\n<\/p>\n<p><h2>\u30b5\u30fc\u30d3\u30b9\u3067\u8d77\u52d5\u3059\u308b<\/h2>\n<\/p>\n<p>\n\u30b5\u30fc\u30d3\u30b9\u3092\u767b\u9332\u3059\u308b\u305f\u3081\u306b sc \u30b3\u30de\u30f3\u30c9\u3092\u7ba1\u7406\u8005\u6a29\u9650\u306e\u30b3\u30de\u30f3\u30c9\u30d7\u30ed\u30f3\u30d7\u30c8\u3067\u5b9f\u884c\u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nsc create UsbService binpath=&quot;D:\\work\\SG\\git\\usbblocker\\UsbBlocker.Web\\bin\\Debug\\net461\\win7-x64\\UsbBlocker.Web.exe --service&quot;\r\n<\/pre>\n<p>\nbinpath \u306b\u306f\u3001\u30d5\u30eb\u30d1\u30b9\u3092\u6307\u5b9a\u3059\u308b\u3002\u30b9\u30a4\u30c3\u30c1\u3092\u4ed8\u3051\u308b\u305f\u3081\u306b\u30c0\u30d6\u30eb\u30af\u30a9\u30fc\u30c8\u3067\u56f2\u3063\u3066\u3042\u308b\u3002\u30b5\u30fc\u30d3\u30b9\u540d\u306f UsbService \u3068\u3057\u3066\u3044\u308b\u304c\u3001\u30c0\u30d6\u3089\u306a\u3044\u3088\u3046\u306b\u81ea\u524d\u3067\u540d\u524d\u3092\u3064\u3051\u308b\u3002\n<\/p>\n<p>\n\u30b5\u30fc\u30d3\u30b9\u306e\u8d77\u52d5\u306f sc start \u3067\u3067\u304d\u308b\u3002\n<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nsb start UsbBlockerService\r\n<\/pre>\n<p>\n\u30b5\u30fc\u30d3\u30b9\u3092\u524a\u9664\u3059\u308b\u3068\u304d\u306f sc delete \u3059\u308b\u3002\n<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nsb delete UsbBlockerService\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u57fa\u672c\u306f\u2193\u306b\u66f8\u3044\u3066\u3042\u308b\u306e\u3060\u3051\u3069\u3001\u6240\u3005\u9593\u9055\u3063\u3066\u3044\u308b\uff1f\u3068\u3044\u3046\u304b\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\u30a2\u30c3\u30d7\u3042\u305f\u308a\u3067\u52d5\u304b\u306a\u304f\u306a\u3063\u3066\u3044\u308b\u306e\u3067\u3001\u30dd\u30a4\u30f3\u30c8\u3060\u3051\u30e1\u30e2\u66f8\u304d\u3002 Windows \u30b5\u30fc\u30d3\u30b9\u3067 ASP.NET Core \u3092\u30db\u30b9\u30c8\u3057\u307e\u3059\u3002 https:\/\/d &hellip; <a href=\"http:\/\/www.moonmile.net\/blog\/archives\/9186\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3,23],"tags":[],"class_list":["post-9186","post","type-post","status-publish","format-standard","hentry","category-dev","category-csharp"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/comments?post=9186"}],"version-history":[{"count":3,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9186\/revisions"}],"predecessor-version":[{"id":9190,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/posts\/9186\/revisions\/9190"}],"wp:attachment":[{"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/media?parent=9186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/categories?post=9186"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.moonmile.net\/blog\/wp-json\/wp\/v2\/tags?post=9186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}