如何让Swagger插件在自托管服务堆栈中工作

我已经通过github提供的示例重新提出了这个问题,并为任何想要自己运行代码的人提供了一个下拉框下载链接: Swagger没有在自托管的ServiceStack服务上工作


我在我的网站解决方案中运行了我的servicestack JSON服务,在’/ api / path下,但现在我想拆分该服务堆栈部分并将其作为自托管Windows服务运行。 我的问题是,我自己和其他开发人员发现Swagger插件对于测试目的非常有用,但现在它是自托管的,似乎HTTPHandler只设置为仅处理服务路由,而纯HTML不起作用。

我该如何解决?

URL: http://localhost:8081/Swagger-UI/Index.html

回应:

 Handler for Request not found: Request.HttpMethod: GET Request.HttpMethod: GET Request.PathInfo: /Swagger-UI/Index.html Request.QueryString: Request.RawUrl: /Swagger-UI/Index.html 

安装Nuget包:

 ServiceStack ServiceStack.Razor 

@marfarma的更新

我的app.config文件里面没有任何相关的ServiceStack ……

                     

Program.cs:

  AppHostHttpListenerBase appHost = new my.HttpApi.myApiServiceHostBase("My Service", "my.ApiService"); string listeningURL = "http://localhost:8081/"; var appSettings = new ServiceStack.Configuration.AppSettings(); my.HttpApi.FakeProvider.ProductProvider.Init(appSettings); my.HttpApi.FakeProvider.UserProvider.Init(appSettings); #if DEBUG try { appHost.Init(); appHost.Start(listeningURL); Console.WriteLine("Press +C to stop."); Thread.Sleep(Timeout.Infinite); } catch (Exception ex) { Console.WriteLine("ERROR: {0}: {1}", ex.GetType().Name, ex.Message); throw; } finally { appHost.Stop(); } Console.WriteLine("WinServiceAppHost has finished"); 

配置方法:

 public override void Configure(Funq.Container container) { Plugins.RemoveAll(x => x is CsvFormat); Plugins.RemoveAll(x => x is HtmlFormat); ServiceStack.Text.JsConfig.EmitCamelCaseNames = true; //register any dependencies your services use, eg: //container.Register(new MemoryCacheClient()); var config = new EndpointHostConfig { DefaultContentType = ContentType.Json, ServiceStackHandlerFactoryPath = "api" }; SetConfig(config); Plugins.Add(new ServiceStack.Api.Swagger.SwaggerFeature()); Dictionary serviceRoutes = new Dictionary(); serviceRoutes.Add(typeof(AuthService), new[] { "/auth/user" }); AuthFeature authFeature = new AuthFeature(() => new AuthUserSession(), new IAuthProvider[] { new FakeCredentialsAuthProvider() }); authFeature.IncludeAssignRoleServices = false; authFeature.ServiceRoutes = serviceRoutes; //specify manual auth routes Plugins.Add(authFeature); container.Register(new MemoryCacheClient()); var userRep = new InMemoryAuthRepository(); container.Register(userRep); Plugins.Add(new CorsFeature(allowedOrigins: "*", allowedMethods: "GET, POST, OPTIONS", //allowedHeaders: "Content-Type", allowedHeaders : "Origin, X-Atmosphere-tracking-id, X-Atmosphere-Framework, X-Cache-Date, Content-Type, X-Atmosphere-Transport, *", allowCredentials: false)); } 

更新2:

1.)删除了删除html和csv的上面的插件部分

2.)在包管理器(nuget)中运行以下内容:

 Install-Package ServiceStack.Api.Swagger 

仍然没有找到处理程序

marfarma指出我在这个问题( ServiceStack:No /swagger-ui/index.html )中的建议表明我可能没有’swagger-ui’html和javascript存在。 我知道

看来自托管服务堆栈只“处理”指定的路由,当我要求自托管服务提供swagger html和javascript时,我得到上面的“找不到请求处理程序”错误。

当我访问自我托管服务中的/ resources地址时,它会显示预期的页面和数据(建议swagger插件正在正常运行),但是当我从文件系统加载swagger html和javascript时(不是’服务) ‘通过我的服务),并提供它工作的资源url,我得到“无法从服务器读取。它可能没有适当的访问控制源设置。”,这似乎是一个CORS问题,但我我已经在我的服务上启用了cors(这似乎是Swagger UI代码的问题),swagger-ui向我的自托管服务发送一个请求作为’OPTIONS’(而不是GET或POST)发送,并且选项请求失败:(

正如Swagger所回答的那样,我没有在自托管的ServiceStack服务上工作 :

自托管服务从bin / debug提供文件 – >总是复制或者如果需要更新则复制。