简体   繁体   English

如何创建可以导入Azure API管理门户的Web API

[英]How to create Web API that can be imported into Azure API Management Portal

so I have fiddled around a bit with Azure API Management Portal. 所以我在Azure API管理门户中稍微摆弄了一下。 I have followed the tutorial on how the import the conference api and managed to get it to work. 我已经按照教程了解了如何导入会议api并设法使其工作。

Then I created a WebApi app that uses swagger. 然后我创建了一个使用swagger的WebApi应用程序。 My configuration is as follows: 我的配置如下:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
    });
    ...
}
public void Configure(IApplicationBuilder app,
    IServiceProvider services, 
    IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseSwagger();

    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "Address Service API");
    });

    app.UseHttpsRedirection();
    app.UseMvc();
}

If I run this and navigate to https://my-api/swagger , I can see the swagger UI and I can also see the specification when I click on the link on the swagger UI or visit the url https://my-api.azurewebsites.net/swagger/v1/swagger.json 如果我运行这个并导航到https://my-api/swagger ,我可以看到swagger UI,当我点击swagger UI上的链接或访问url https://my-api.azurewebsites.net/swagger/v1/swagger.json时,我也可以看到规范https://my-api.azurewebsites.net/swagger/v1/swagger.json

So my problem is, I have no idea on how to actually import this into AAMP. 所以我的问题是,我不知道如何将其实际导入AAMP。 I can publish it to a app service and it works from there, but if I try to import the url https://my-api.azurewebsites.net/swagger/v1/swagger.json into the AAMP, I get an error: 我可以将它发布到应用服务,它可以在那里工作,但如果我尝试将网址https://my-api.azurewebsites.net/swagger/v1/swagger.json导入AAMP,我会收到错误:

在此输入图像描述

So I wait an hour and try again, only the be greeted by the same error and I think I am missing something because when I imported the conference api specification, it had a different url than mine, yet I cannot find anything or I am searching for the wrong things. 所以我等了一个小时再试一次,只是遇到了同样的错误,我想我错过了什么,因为当我导入会议api规范时,它有一个不同于我的网址,但我找不到任何东西或者我正在搜索对于错误的事情。 Can anybody please give me a heads up here? 请问有人在这里问我一个问题吗?

I have also tried searching for the sources of the conference API so I can deduct what I am doing wrong but I didn't have any luck on finding those. 我也尝试过搜索会议API的来源,这样我就可以推断出我做错了什么,但我没有找到那些好运。

Importing Swagger document into APIM is pretty straight forward by following this Azure document. 通过遵循此Azure文档,将Swagger文档导入APIM非常简单。 There's no issue when you import Swagger 1.2 documents. 导入Swagger 1.2文档时没有问题。 However, if you're intending to import Swagger 2.0 ones, you might be facing these kind of issue 但是,如果您打算导入Swagger 2.0,则可能会遇到这类问题

If you're building an API app with .NET Framework 4.5+, using Swashbuckle library, it would be fine. 如果您使用.NET Framework 4.5+构建API应用程序,使用Swashbuckle库,那就没问题了。 However, if you're building the app with ASP.NET Core, it does bring you a headache. 但是,如果您使用ASP.NET Core构建应用程序,它确实会让您头疼。 Firstly, look at your Startup.cs file. 首先,查看您的Startup.cs文件。 The ConfigureService method looks like: ConfigureService方法如下所示:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    ...

    services.AddSwaggerGen();

    services.ConfigureSwaggerDocument(
        options =>
        {
            options.SingleApiVersion(new Info() { Version = "v1", Title = "Swagger UI" });
            options.IgnoreObsoleteActions = true;
            options.OperationFilter(new ApplyXmlActionComments(GetXmlPath(appEnv)));
        });

    services.ConfigureSwaggerSchema(
        options =>
        {
            options.DescribeAllEnumsAsStrings = true;
            options.IgnoreObsoleteProperties = true;
            options.CustomSchemaIds(type => type.FriendlyId(true));
            options.ModelFilter(new ApplyXmlTypeComments(GetXmlPath(appEnv)));
        });

    ...
}

private static string GetXmlPath(IApplicationEnvironment appEnv)
{
    var assembly = typeof(Startup).GetTypeInfo().Assembly;
    var assemblyName = assembly.GetName().Name;

    var path = $@"{appEnv.ApplicationBasePath}\{assemblyName}.xml";
    if (File.Exists(path))
    {
        return path;
    }

    var config = appEnv.Configuration;
    var runtime = $"{appEnv.RuntimeFramework.Identifier.ToLower()}{appEnv.RuntimeFramework.Version.ToString().Replace(".", string.Empty)}";
    path = $@"{appEnv.ApplicationBasePath}\..\..\artifacts\bin\{assemblyName}\{config}\{runtime}\{assemblyName}.xml";
    return path;
}

In addition to this, the Configure method might look like: 除此之外,Configure方法可能如下所示:

public void Configure(IApplicationBuilder app)
{
    ...

    app.UseSwaggerGen();
    app.UseSwaggerUi();

    ...
}

Wen need to include two additional properties – host and schemes. 温需要包括两个额外的属性 - 主机和方案。 Swagger specification clearly declares that both are NOT required. Swagger规范明确声明两者都不是必需的。 However, APIM DOES require both properties to be included in the swagger.json document. 但是,APIM DOES要求将这两个属性都包含在swagger.json文档中。

So, how can we sort this out? 那么,我们怎么能解决这个问题呢?

For your app in .NET 4.5+, just make sure that your SwaggerConfig.cs has activated those options with proper settings: 对于.NET 4.5+中的应用程序,只需确保您的SwaggerConfig.cs已使用适当的设置激活了这些选项:

SwaggerDocsConfig.Schemes(new[] { “http”, “https” });
SwaggerDocsConfig.RootUrl(req => GetRootUrlFromAppConfig());

In your ASP.NET Core app, it might be tricky as you should implement the IDocumentFilter interface. 在您的ASP.NET Core应用程序中,它可能会很棘手,因为您应该实现IDocumentFilter接口。 Here's a sample code: 这是一个示例代码:

  public class SchemaDocumentFilter : IDocumentFilter
    {
      public void Apply(SwaggerDocument swaggerDoc, DocumentFilterContext context)
      {
        swaggerDoc.Host = "localhost:44321";
        swaggerDoc.BasePath = "/";
        swaggerDoc.Schemes = new List<string>() { "https" };
      }
    }

And this SchemaDocumentFilter should be added into your ConfigureService method in Startup.cs:

public static void ConfigureServices(this IServiceCollection services)
{
  ...

  services.AddSwaggerGen();

  services.ConfigureSwaggerDocument(
    options =>
      {
        options.SingleApiVersion(new Info() { Version = "v1", Title = "Swagger UI" });
        options.IgnoreObsoleteActions = true;
        options.OperationFilter(new ApplyXmlActionComments(GetXmlPath(appEnv)));

        options.DocumentFilter<SchemaDocumentFilter>();
      });

  services.ConfigureSwaggerSchema(
    options =>
      {
        options.DescribeAllEnumsAsStrings = true;
        options.IgnoreObsoleteProperties = true;
        options.CustomSchemaIds(type => type.FriendlyId(true));
        options.ModelFilter(new ApplyXmlTypeComments(GetXmlPath(appEnv)));
      });

  ...
}

Once you complete this, then import your swagger.json to APIM then it should work. 完成此操作后,将swagger.json导入APIM然后它应该可以正常工作。

Reference : 参考

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM