首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Nancy.Owin vs Nancy.Hosting.Self与顶层机架

Nancy.Owin vs Nancy.Hosting.Self与顶层机架
EN

Stack Overflow用户
提问于 2018-01-21 01:50:07
回答 1查看 781关注 0票数 0

1)使用两个不同的包设置有什么区别?其中一个比另一个更受支持吗?

我有一个使用Nancy.Hosting.Self的项目,没有找到任何关于如何设置windows身份验证的文章,而我找到了Nancy.Owin作为中间件的文章。我现在已经切换到Nancy.owin。

这里有几个不同的问题。

2) owin格式化程序可以在两个nancy引导程序中配置,就像在owin端点topshelf中一样。我应该在哪里配置格式化程序?如果在topshelf端点中进行配置,这是否也会应用于nancy引导程序?

3)在topshelf中使用nancy端点时,可以选择设置防火墙规则和url预留。我在owin终结点中找不到它。

代码语言:javascript
复制
private static void CreateHost(HostConfigurator host)
    {
        var logger = SetupLogging();
        host.UseSerilog(logger);
        host.UseLinuxIfAvailable();
        //sc => serviceconfigurator, ls => licenseService
        host.Service<WebService>(sc =>
        {
            sc.ConstructUsing(name => new WebService(_config));
            sc.WhenStarted(ls => ls.Start());
            sc.WhenStopped(ls => ls.Stop());
            sc.OwinEndpoint(app =>
            {
                app.ConfigureHttp(configuration =>
                {
                    //use json.net serializer
                    configuration.Formatters.Clear();
                    configuration.Formatters.Add(new JsonMediaTypeFormatter());

                    //configure json settings
                    var jsonSettings = configuration.Formatters.JsonFormatter.SerializerSettings;
                    jsonSettings.Formatting = Formatting.Indented;
                    jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = false, AllowIntegerValues = true });
                    jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                });
                app.ConfigureAppBuilder(builder =>
                {
                    builder.UseNancy(options =>
                    {
                        options.Bootstrapper = new Bootstrapper(_config);
                        //options.PerformPassThrough
                    });
                });
                app.ConfigureStartOptions(options =>
                {
                    options.Port = _config.LicenseServicePort;
                    //options.Urls = new List<string>(){};
                });
            });

            //add host reservation during service install, this is the only time, nancy will have admin rights, will be deleted when service is uninstalled as well.
            //nc => nancyConfig
            //sc.WithNancyEndpoint(host, nc =>
            //{
            //    nc.AddHost(port: _config.LicenseServicePort);
            //    nc.CreateUrlReservationsOnInstall();
            //    nc.DeleteReservationsOnUnInstall();
            //    nc.OpenFirewallPortsOnInstall(firewallRuleName: "mycustomservice");
            //    nc.Bootstrapper = new Bootstrapper(_config);
            //});
        });

        host.SetDescription("Licensing service for my api.");
        host.SetDisplayName("myservice");
        host.SetServiceName("myservice);
        host.RunAsNetworkService();
        host.StartAutomatically();
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 23:31:39

发布了codetoshare的答案

代码语言:javascript
复制
private static void CreateHost(HostConfigurator host)
    {
        Log.Logger = SetupLogging();
        host.SetStartTimeout(TimeSpan.FromSeconds(60));
        //Plug serilog into host: log startup urls and start / stop
        host.UseSerilog(Log.Logger);

        //Allow to be run on linux as well
        host.UseLinuxIfAvailable();

        //sc => serviceconfigurator, ls => licenseService
        host.Service<WebService>(sc =>
        {
            //basic topshelf configuration
            sc.ConstructUsing(name => new WebService());
            sc.WhenStarted(ls => ls.Start());
            sc.WhenStopped(ls =>
            {
                ls.Stop();
                DisposeLogging();
            });
            //I am using an extension here because I had converted the application from Nancy.Host.Self to Nancy.Owin
            //if the extension will not get updated and this breaks the application, convert it to a normal app: see nearly every topshelf + owin example
            //owin configuration
            sc.OwinEndpoint(app =>
            {
                app.ConfigureHttp(configuration =>
                {
                    //use json.net serializer
                    configuration.Formatters.Clear();
                    configuration.Formatters.Add(new JsonMediaTypeFormatter());

                    //configure json settings
                    var jsonSettings = configuration.Formatters.JsonFormatter.SerializerSettings;
                    jsonSettings.Formatting = Formatting.Indented;
                    jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = false, AllowIntegerValues = true });
                    jsonSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
                });

                app.ConfigureAppBuilder(builder =>
                {
                    //setup windows authentication
                    HttpListener listener = (HttpListener)builder.Properties["System.Net.HttpListener"];
                    listener.AuthenticationSchemes = AuthenticationSchemes.IntegratedWindowsAuthentication;

                    //setup nancy
                    builder.UseNancy(options =>
                    {
                        options.Bootstrapper = new Bootstrapper(_config);
                    });
                });
                //setup urls: always add localhost and 127.0.0.1 together with the host specified in the config file
                app.ConfigureStartOptions(options =>
                {
                    options.Port = _config.LicenseServicePort;
                    var localhost = $"http://localhost:{_config.LicenseServicePort}";
                    var localhost2 = $"http://127.0.0.1:{_config.LicenseServicePort}";
                    //todo: this should support https as well
                    //todo: allow multiple hosts to be specified on config
                    options.Urls.Add(localhost);
                    options.Urls.Add(localhost2);
                    var configuredHost = $"{_config.LicenseServiceUrl}:{_config.LicenseServicePort}";
                    if (!configuredHost.Equals(localhost) && !configuredHost.Equals(localhost2))
                    {
                        options.Urls.Add(configuredHost);
                    }
                });
            });

            //old nancyhost config, keep this untill documented on confluence
            //add host reservation during service install, this is the only time, nancy will have admin rights, will be deleted when service is uninstalled as well.
            //nc => nancyConfig
            //sc.WithNancyEndpoint(host, nc =>
            //{
            //    nc.AddHost(port: _config.LicenseServicePort);
            //    nc.CreateUrlReservationsOnInstall();
            //    nc.DeleteReservationsOnUnInstall();
            //    nc.OpenFirewallPortsOnInstall(firewallRuleName: "SegreyLicensingService");
            //    nc.Bootstrapper = new Bootstrapper(_config);
            //});
        });
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48359289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档