我已经在GitHub上提供了例子,并提供了一个Dropbox下载链接,供任何想运行代码的人使用来重新提出这个问题:Swagger not working on a self hosted ServiceStack Service
我曾将我的ServiceStack JSON服务放在我的网站解决方案中,在“/api/”路径下运行,但现在我想将该部分ServiceStack拆分出来并作为自托管Windows服务运行。我的问题是,我和其他开发人员都认为Swagger插件非常有用,但现在它是自托管的,似乎HTTP处理程序仅设置为处理服务路由,而纯HTML无法正常工作。
我该如何解决这个问题?
URL:
我曾将我的ServiceStack JSON服务放在我的网站解决方案中,在“/api/”路径下运行,但现在我想将该部分ServiceStack拆分出来并作为自托管Windows服务运行。我的问题是,我和其他开发人员都认为Swagger插件非常有用,但现在它是自托管的,似乎HTTP处理程序仅设置为处理服务路由,而纯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相关的内容...
<?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
仍然找不到处理程序。
这个问题中的建议(ServiceStack: No /swagger-ui/index.html)是marfarma指出的,它认为我可能没有“swagger-ui”HTML和JavaScript。但是我有。
似乎自托管的servicestack只“处理”指定的路由,当我要求自托管服务提供Swagger HTML和JavaScript时,我会收到上面的“找不到请求的处理程序”的错误。
当我访问我的自托管服务中的/resource地址时,它显示了预期的页面和数据(表明Swagger插件正在正常工作),但当我从文件系统加载Swagger HTML和JavaScript(不是由我的服务“提供”),并提供它可以正常工作的资源URL时,我得到“无法从服务器读取。它可能没有适当的访问控制起源设置。”,这似乎是CORS问题,但我已经在我的服务上启用了cors(似乎是Swagger UI代码的一个问题),swagger-ui向我的自托管服务发送一个名为“OPTIONS”的请求(而不是GET或POST),options请求失败 :(
Plugins.RemoveAll(x => x is HtmlFormat);
,会发生什么? - Pauli Price