我用 github 上提供的示例和想要自己运行代码的任何人的下拉框下载链接重新问了这个问题:Swagger not working on a self hosting 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 时(不是 'served ' 通过我的服务),并为其提供有效的资源 url,我得到“无法从服务器读取。它可能没有适当的访问控制源设置。”,这似乎是一个 CORS 问题,但我'已经在我的服务上启用了 cors(它似乎是 Swagger UI 代码的问题),swagger-ui 向我的自托管服务发送作为“OPTIONS”(而不是 GET 或 POST)发送的请求,并且选项请求失败:(