我在使用dotnet核心的Topshelf遇到了麻烦。帮我配置它

时间:2016-12-13 11:46:08

标签: c# owin visual-studio-code topshelf .net-core

在我的program.cs文件中,我正在尝试配置Topshelf,但我无法让它运行。我认为Topshelf无法在dnxcore50中运行?也许我错过了别的什么?

using System;
using System.Web.Http;
using CreateDictionary.Memory;
using Microsoft.Owin;
using Microsoft.Owin.Hosting;
using Owin;
using System.Threading.Tasks;
using Topshelf;

    namespace ConsoleApplication
    {
        public class Program
        {
            public static void Main(string[] args)
            {
            Console.WriteLine("Server starting....");
            ServerRun.Run();
            Console.ReadLine();
            }
        }
        public class Startup
        {
            public void Configuration(IAppBuilder appBuilder)
            {
                appBuilder.Use(typeof (ConfigApiMiddleware));
                appBuilder.UseWebApi(WebApiConfiguration());
            }
            //Config to connect
            private static HttpConfiguration WebApiConfiguration()
            {
                var config = new HttpConfiguration();
                config.Routes.MapHttpRoute(
                    "CustomeApi",
                    "api/{controller}/{id}",
                    new { id = RouteParameter.Optional });
                return config;
            }
        }
        public class ServerRun
        {

            public static void Run()
            {

                HostFactory.Run(x =>
                {
                    x.Service<OwinServer>(s =>
                    {
                        s.ConstructUsing(name => new OwinServer());
                        s.WhenStarted(tc => tc.Start());
                        s.WhenStopped(tc => tc.Stop());
                    });
                    x.RunAsLocalSystem();
                });

                Console.WriteLine("Server running at {0} - press Enter to quit. ", "http://localhost:8383");
                Console.ReadLine();
            }

        }
        public class ConfigApiMiddleware : OwinMiddleware
        {
            public ConfigApiMiddleware(OwinMiddleware next):base(next)
            {}
            public async override Task Invoke(IOwinContext context)
            {
                //Config phần header request tại đây

                var request = context.Request.Headers;

                var origin = request["Origin"];
                var host = request["Host"];
    //            if (origin == null)
    //            {
    //                origin = "API version";
    //            }
                Console.WriteLine("Origin:{0}--Host:{1}", origin,host);

                //Config phần header respond tại đây

                context.Response.Headers["MachineName"] = Environment.MachineName;

                context.Response.Headers["Access-Control-Allow-Origin"] = "*";
                context.Response.Headers["Content-Type"] = "application/json; charset=utf-8";
                context.Response.Headers["Access-Control-Allow-Methods"] = "GET,POST,PUT,DELETE,OPTION";
                context.Response.Headers["Access-Control-Allow-Credentials"] = "true";
                context.Response.Headers["Access-Control-Allow-Headers"] = "Origin, X-Requested-With, Content-Type, Accept,Authorization";

                await Next.Invoke(context);
            }
        } 
        public class OwinServer
        {
            private IDisposable _webapp;

            public void Start()
            {
                _webapp = WebApp.Start<Startup>("http://localhost:8383");//"http://localhost:8383"
                MemoryInfor.InitMemory();
            }

            public void Stop()
            {
                _webapp.Dispose();
            }
        }
    }

我在project.json文件中的配置:

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
  },
  "dependencies": {
    "System.Data.Common": "4.3.0",
    "System.Data.SqlClient": "4.3.0",
    "Microsoft.NETCore.Portable.Compatibility": "1.0.2",
    "Owin": "1.0.0",
    "Microsoft.AspNet.WebApi.Owin": "5.2.3",
    "Microsoft.AspNet.WebApi.OwinSelfHost": "5.2.3",
    "Topshelf": "4.0.3",
    "Topshelf.Owin": "1.3.29"
  },
  "frameworks": {
    "netcoreapp1.1": {
      "dependencies": {
        "Microsoft.NETCore.App": {
          "type": "platform",
          "version": "1.1.0"
        }
      },
      "imports": [
        "dnxcore50",
        "net452",
        "net461"
      ]
    }
  }
}

当我使用命令dotnet run

构建它时,我得到了这个结果
Unhandled Exception: System.TypeInitializationException: The type initializer for 'Topshelf.Logging.TypeExtensions' t
hrew an exception. ---> System.TypeLoadException: Could not load type 'System.Collections.Concurrent.ConcurrentDictio
nary`2' from assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e'.
   at Topshelf.Caching.ConcurrentCache`2..ctor()
   at Topshelf.Logging.TypeNameFormatter..ctor(String genericArgumentSeparator, String genericOpen, String genericClo
se, String namespaceSeparator, String nestedTypeSeparator)
   at Topshelf.Logging.TypeNameFormatter..ctor()
   at Topshelf.Logging.TypeExtensions..cctor()
   --- End of inner exception stack trace ---
   at Topshelf.Logging.HostLogger.Get(Type type)
   at Topshelf.HostFactory.Run(Action`1 configureCallback)
   at ConsoleApplication.ServerRun.Run() in C:\Users\0baut_000\Desktop\OwinServer\Program.cs:line 45
   at ConsoleApplication.Program.Main(String[] args) in C:\Users\0baut_000\Desktop\OwinServer\Program.cs:line 17

1 个答案:

答案 0 :(得分:1)

不幸的是,Topshelf尚未支持dotnet核心;计划是在NETStandard 2.0发布时实现。