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

时间:2013-10-14 08:19:31

标签: c# servicestack self-hosting swagger-ui

我已经通过github提供的示例重新提出了这个问题,并为任何想要自行运行代码的人提供了一个下拉框下载链接:Swagger not working on a self hosted ServiceStack Service


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

我该如何解决这个问题?

网址: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 ......

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_service1_V2_SSL" />
        <binding name="BasicHttpBinding_service2_V1_SSL" />
      </basicHttpBinding>
    </bindings>
    <client>
      <!-- bespoke services I'm connecting too -->
    </client>
  </system.serviceModel>
  <appSettings>
     <!-- bespoke app settings -->
  </appSettings>  
</configuration>

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 <CTRL>+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, e.g:
            //container.Register<ICacheClient>(new MemoryCacheClient());

            var config = new EndpointHostConfig { DefaultContentType = ContentType.Json, ServiceStackHandlerFactoryPath = "api" };


            SetConfig(config);
            Plugins.Add(new ServiceStack.Api.Swagger.SwaggerFeature());

            Dictionary<Type, string[]> serviceRoutes = new Dictionary<Type, string[]>();
            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<ICacheClient>(new MemoryCacheClient());
            var userRep = new InMemoryAuthRepository();
            container.Register<IUserAuthRepository>(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时(不是由我的服务“服务”,并提供它工作的资源网址,我得到“无法从服务器读取。它可能没有适当的访问控制源设置。”,这似乎是一个CORS问题,但我已经启用了我的服务上的cors(看起来是Swagger UI代码的问题),swagger-ui发送我的自托管服务作为'OPTIONS'发送的请求(而不是GET或POST),以及选项请求失败:(

1 个答案:

答案 0 :(得分:2)

Swagger not working on a self hosted ServiceStack Service中回答:

自托管服务从bin / debug提供文件 - &gt;如果需要更新,则总是复制或复制。