【发布时间】:2013-10-14 08:19:31
【问题描述】:
我已经通过 github 上提供的示例和想要自己运行代码的任何人的下拉框下载链接重新提出了这个问题:Swagger not working on a self hosted ServiceStack Service
我的 servicestack JSON 服务在我的网站解决方案中运行,位于“/api/”路径下,但现在我想拆分该服务堆栈部分并将其作为自托管 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>
程序.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 时(我的服务没有“服务”),并为其提供有效的资源 url,我得到“无法从服务器读取。它可能没有适当的访问控制源设置。”,这似乎是一个 CORS 问题,但我已经在我的服务上启用了 cors(它似乎是 Swagger UI 代码的问题),swagger-ui 向我的自托管服务发送作为“OPTIONS”(而不是 GET 或 POST)发送的请求,以及选项请求失败:(
【问题讨论】:
-
你能创建一个显示问题的最小测试用例吗?使用 Hello World 示例作为您的服务,并使用您现有的 AppHost 配置。我认为这可能是 AppHost 配置的问题,但没有更多细节,我只能推测。
-
嗨@marfarma,我添加了我认为可能适用的代码。我的 app.config 非常简单,无论如何,我的代码主要来自 servicestack 站点示例。最后的 CORS 东西是我试图让它工作的尝试,可能有点矫枉过正,但它无论如何都不起作用
-
在我的脑海中,如果你注释掉
Plugins.RemoveAll(x => x is HtmlFormat);这一行会发生什么? -
另外,你看到这个问题了吗:stackoverflow.com/q/16649066/149060
-
@marfarma 我已经更新了我的问题,感谢您迄今为止的帮助
标签: c# servicestack self-hosting swagger-ui