我正在尝试创建ServiceStack REST API项目。到目前为止,我有一切工作,除了Swagger(OpenAPI)。当我加载Swagger UI时,它没有显示任何API,我可以从/ openapi端点看到没有发现API。
/ metadata端点工作正常,除了" Swagger UI"链接。
我已将代码减少到最低限度:
using System;
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using ServiceStack;
using ServiceStack.Api.OpenApi;
using ServiceStack.Host.Handlers;
namespace ApiTest
{
class Program {
[Route("/hello/{Name}", "GET", Summary = @"Hello somebody", Notes = @"Welcomes the caller")]
public class Hello : IReturn<HelloResponse> {
[ApiMember(Name="Name", Description = "Name of the caller", IsRequired = true)]
public string Name { get; set; }
}
public class HelloResponse {
public string Result { get; set; }
}
public class HelloService : Service
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
//Define the Web Services AppHost
public class AppHost : AppHostBase {
public AppHost()
: base("HttpListener Self-Host", typeof(HelloService).Assembly) { }
public override void Configure(Funq.Container container) {
Plugins.Add(new OpenApiFeature());
}
}
static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build();
host.Run();
}
public class Startup
{
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseServiceStack(new AppHost());
app.Use(new RequestInfoHandler());
}
}
}
}
有什么建议吗?
BTW这是我的项目文件;还保持最低限度:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<RestoreSources>https://www.myget.org/F/servicestack;$(RestoreSources)</RestoreSources>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="ServiceStack.Core">
<Version>1.0.*</Version>
</PackageReference>
<PackageReference Include="ServiceStack.Common.Core">
<Version>1.0.*</Version>
</PackageReference>
<PackageReference Include="ServiceStack.Client.Core">
<Version>1.0.*</Version>
</PackageReference>
<PackageReference Include="ServiceStack.Interfaces.Core">
<Version>1.0.*</Version>
</PackageReference>
<PackageReference Include="ServiceStack.Api.OpenApi.Core">
<Version>1.0.*</Version>
</PackageReference>
<PackageReference Include="ServiceStack.Text.Core">
<Version>1.0.*</Version>
</PackageReference>
<PackageReference Include="ServiceStack.Kestrel">
<Version>1.0.*</Version>
</PackageReference>
</ItemGroup>
</Project>
答案 0 :(得分:1)
如果您正在使用ServiceStack和.NET Core 2.0,我们建议使用明确支持.NET Core 2.0的pre-release MyGet packages。
同样SwaggerFeature
实现了旧的1.2规范,新项目应该使用新的OpenApiFeature which implements the newer Swagger/OpenApi 2.0 spec。
答案 1 :(得分:0)
感谢@mythz并与他提供的示例项目进行比较,我发现了问题。在我的代码中,我将我的DTO类放在Program类中;即我的DTO课程是ApiTest.Program.Hello。当我改变我的代码以便DTO是顶级的(仅在命名空间内而不是在另一个类中)时,Swagger开始工作。
非常感谢@mythz在这件事上的快速回复和帮助!