Unable to resolve service for type Microsoft.AspNetCore.Identity.RoleManager - asp.net-core

I am working on project using microservice architecture, I use ASP.NET Core Identity as a separate microservice to create users and roles. I extend users and roles with custom fields and configure Identity in my API project's startup.cs. But while I run my application I got an error as following,
Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microservice.IdentityMS.Application.Interfaces.IIdentityMSService Lifetime: Transient ImplementationType: Microservice.IdentityMS.Application.Services.IdentityMSService': Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager`1[Microservice.IdentityMS.Domain.Models.MembershipRole]' while attempting to activate 'Alexa.IdentityMS.Data.Repository.IdentityMSRepository'.)
Here's my Identity Microservice startup
Startup.cs:
using Microservice.IdentityMS.Data.Context;
using Microservice.IdentityMS.Domain.Models;
using Microservice.Infra.IoC;
using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Microservice.Identity.Api
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
var userConnectionString = Configuration["DbContextSettings:UserConnectionString"];
var dbPassword = Configuration["DbContextSettings:DbPassword"];
var userBuilder = new NpgsqlConnectionStringBuilder(userConnectionString)
{
Password = dbPassword
};
services.AddDbContext<MembershipDBContext>(opts => opts.UseNpgsql(builder.ConnectionString));
services.AddDbContext<UserDBContext>(opts => opts.UseNpgsql(userBuilder.ConnectionString));
services.AddIdentity<MembershipUser, MembershipRole>(options =>
{
options.Password.RequiredLength = 8;
options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._#+ ";
options.SignIn.RequireConfirmedEmail = false;
}).AddRoles<MembershipRole>().AddEntityFrameworkStores<MembershipDBContext>()
.AddDefaultTokenProviders();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Identity Microservice", Version = "v1" });
});
services.AddMediatR(typeof(Startup));
RegisterServices(services);
}
private void RegisterServices(IServiceCollection services)
{
DependencyContainer.RegisterServices(services);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Identity Microservice V1");
});
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
DependencyContainer.cs:
using MediatR;
using Microservices.Domain.Core.Bus;
using Microservices.Infra.Bus;
using Microsoft.Extensions.DependencyInjection;
using Microservices.ProductMS.Domain.Interfaces;
using Microservices.ProductMS.Data.Repository;
using Microservices.ProductMS.Data.Context;
using Microservices.ProductMS.Application.Interfaces;
using Microservices.ProductMS.Application.Services;
using Microservices.PartyMS.Application.Interfaces;
using Microservices.PartyMS.Application.Services;
using Microservices.PartyMS.Domain.Interfaces;
using Microservices.PartyMS.Data.Repository;
using Microservices.PartyMS.Data.Context;
using Microservices.MasterMS.Application.Interfaces;
using Microservices.MasterMS.Application.Services;
using Microservices.MasterMS.Domain.Interfaces;
using Microservices.MasterMS.Data.Repository;
using Microservices.MasterMS.Data.Context;
using Microservices.MasterMS.Domain.Commands;
using Microservices.MasterMS.Domain.CommandHandler;
using Microservices.ProductMS.Domain.Commands;
using Microservices.ProductMS.Domain.CommandHandler;
using Microservices.PartyMS.Domain.CommandHandler;
using Microservices.AccountMS.Application.Interfaces;
using Microservices.AccountMS.Application.Services;
using Microservices.AccountMS.Domain.Interfaces;
using Microservices.AccountMS.Data.Repository;
using Microservices.AccountMS.Data.Context;
using Microservices.SalesPurchaseMS.Domain.Interfaces;
using Microservices.SalesPurchaseMS.Data.Repository;
using Microservices.SalesPurchaseMS.Data.Context;
using Microservices.SalesPurchaseMS.Application.Interfaces;
using Microservices.SalesPurchaseMS.Application.Services;
using Microservices.IdentityMS.Application.Interfaces;
using Microservices.IdentityMS.Application.Services;
using Microservices.IdentityMS.Domain.Interfaces;
using Microservices.IdentityMS.Data.Repository;
using Microservices.IdentityMS.Data.Context;
using Microsoft.AspNetCore.Identity;
using Microservices.IdentityMS.Domain.Models;
namespace Microservices.Infra.IoC
{
public class DependencyContainer
{
public static void RegisterServices(IServiceCollection services)
{
//Domain Bus
services.AddSingleton<IEventBus, RabbitMQBus>(sp =>
{
var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
return new RabbitMQBus(sp.GetService<IMediator>(), scopeFactory);
});
//Subscriptions
services.AddTransient<ProductMS.Domain.EventHandler.CompanyEventHandler>();
services.AddTransient<PartyMS.Domain.EventHandler.CompanyEventHandler>();
//Domain Events
services.AddTransient<IEventHandler<ProductMS.Domain.Events.CompanyEvent>, ProductMS.Domain.EventHandler.CompanyEventHandler>();
services.AddTransient<IEventHandler<PartyMS.Domain.Events.CompanyEvent>, PartyMS.Domain.EventHandler.CompanyEventHandler>();
//services.AddTransient<IEventHandler<SalesMS.Domain.Events.CompanyEvent>, SalesMS.Domain.EventHandler.CompanyEventHandler>();
//services.AddTransient<IEventHandler<SalesMS.Domain.Events.PartyEvent>, SalesMS.Domain.EventHandler.PartyEventHandler>();
//Domain Commands
services.AddTransient<IRequestHandler<CompanySyncCommand, bool>, CompanySyncCommandHandler>();
services.AddTransient<IRequestHandler<ProductSyncCommand, bool>, ProductSyncCommandHandler>();
services.AddTransient<IRequestHandler<ProductCategorySyncCommand, bool>, ProductCategorySyncCommandHandler>();
services.AddTransient<IRequestHandler<PartySyncCommand, bool>, PartySyncCommandHandler>();
//Application Services
services.AddTransient<IMasterService, MasterService>();
services.AddTransient<IProductService, ProductService>();
services.AddTransient<IPartyMSService, PartyMSService>();
//services.AddTransient<IPurchaseService, PurchaseService>();
services.AddTransient<IPurchaseService, PurchaseService>();
services.AddTransient<ISaleService, SaleService>();//SaleMS
services.AddTransient<IAccountMSService, AccountMSService>();
services.AddTransient<IIdentityMSService, IdentityMSService>();
services.AddTransient<IAdministrationService, AdministrationService>();
//Data
services.AddTransient<IMasterRepository, MasterRepository>();
services.AddTransient<MasterDbContext>();
services.AddTransient<IProductRepository, ProductRepository>();
services.AddTransient<ProductsDBContext>();
services.AddTransient<IPartyMSRepository, PartyMSRepository>();
services.AddTransient<PartyMSDBContext>();
services.AddTransient<IPurchaseRepository, PurchaseRepository>();
services.AddTransient<ISaleRepository, SaleRepository>();
services.AddTransient<SPDBContext>();
services.AddTransient<IAccountMSRepository, AccountMSRepository>();
services.AddTransient<AccountDbContext>();
services.AddTransient<IIdentityMSRepository, IdentityMSRepository>();
services.AddTransient<IAdministrationRepository, AdministrationRepository>();//IdentityMS
services.AddTransient<UserDBContext>();
}
}
}
IdentityMSRepository.cs:
public class IdentityMSRepository : IIdentityMSRepository
{
private readonly UserDBContext _userContext;
private readonly RoleManager<MembershipRole> _roleManager;
private readonly UserManager<MembershipUser> _userManager;
public IdentityMSRepository(UserDBContext userContext, RoleManager<MembershipRole> roleManager, UserManager<MembershipUser> userManager)
{
_userContext = userContext;
_roleManager = roleManager;
_userManager = userManager;
}
}
What am I missing ?

Finally I made change as the comment suggested I moved it from DI container to startup file and it's working. #Rena Thank you so much.

Related

How do I enable compression for my API on the linux service plan?

I'm deploying a .net-core web API to the Linux app service plan. I haven't created a DOCKERFILE. Just building production release and deploying. Based on the response headers in production, it appears to be using the Kestrel server. I found in the MS documentation that one can enable compression in the code. This approach seems to work perfectly locally, however I do not see the Content-Encoding header on my web API call while in production.
You can add below code in your startup.cs file. It works for me.
// Configure Compression level
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
// Add Response compression services
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
And you also need add app.UseResponseCompression(); in configure function.
Test Result:
My Startup.cs file.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.ResponseCompression;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
namespace corewebapp_linux
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Configure Compression level
services.Configure<GzipCompressionProviderOptions>(options => options.Level = CompressionLevel.Fastest);
// Add Response compression services
services.AddResponseCompression(options =>
{
options.Providers.Add<GzipCompressionProvider>();
options.EnableForHttps = true;
});
services.AddControllers();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "corewebapp_linux", Version = "v1" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "corewebapp_linux v1"));
}
app.UseResponseCompression();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}

Getting 401 error without any message while authorizing dotnet core web api with keycloack

I was setting authentication and authorization for my dotnet core application (3.1 version). I set up a keycloak server as my identity provider. I got a JWT token from keycloak and i used it while calling my api method. I am always getting 401 error. I have checked almost all the Keycloak configurations and i am not able to find the issue.
My JWT token is
eyJhbGciOiJSUzI1NiIsInR5cCIgOiAiSldUIiwia2lkIiA6ICJza0dldkJuZnhieWlCSF9sOC1ZSEpxVnk2bm8yV3c0eG14a0xjeVlvMXRzIn0.eyJqdGkiOiJlNjI3NTA3MC05MTA0LTRhMGQtOGYxNC1mNzdkY2FjMjYzMDUiLCJleHAiOjE1ODg1MjMxMTcsIm5iZiI6MCwiaWF0IjoxNTg4NTE5NTE3LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjgwODAvYXV0aC9yZWFsbXMvZ2FkZ2VvbiIsImF1ZCI6WyJmdHMtcG9ydGFsIiwiYWNjb3VudCJdLCJzdWIiOiJiMzIxYTA0Ny0zNzBkLTRlOTMtOWQ0MS1jZmRjYWMyN2I1ZjYiLCJ0eXAiOiJCZWFyZXIiLCJhenAiOiJmdHMtcG9ydGFsIiwiYXV0aF90aW1lIjowLCJzZXNzaW9uX3N0YXRlIjoiYmEzNmRiYjctMGYyOC00MDY4LWIwYmQtMDUxMmQxN2Q1MzEyIiwiYWNyIjoiMSIsInJlYWxtX2FjY2VzcyI6eyJyb2xlcyI6WyJvZmZsaW5lX2FjY2VzcyIsInVtYV9hdXRob3JpemF0aW9uIl19LCJyZXNvdXJjZV9hY2Nlc3MiOnsiZnRzLXBvcnRhbCI6eyJyb2xlcyI6WyJmdHNfYWRtaW4iXX0sImFjY291bnQiOnsicm9sZXMiOlsibWFuYWdlLWFjY291bnQiLCJBZG1pbmlzdHJhdG9ycyIsIm1hbmFnZS1hY2NvdW50LWxpbmtzIiwidmlldy1wcm9maWxlIl19fSwic2NvcGUiOiJmdHNfcG9ydGFsIGVtYWlsIHByb2ZpbGUiLCJlbWFpbF92ZXJpZmllZCI6ZmFsc2UsIm5hbWUiOiJQcml5ZXNoIEsiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJwa2FyYXRoYSIsImdpdmVuX25hbWUiOiJQcml5ZXNoIiwiZmFtaWx5X25hbWUiOiJLIiwiZW1haWwiOiJwcml5ZXNoa2FyYXRoYUBnbWFpbC5jb20iLCJmdHMtcnVsZXMiOlsiZnRzX2FkbWluIl19.gFjqPSmyXvk78OhbPJ853upCHIHZdsAsjT1Psc7pzBqv30unH0EyCk3chTK0_87J-5bH2QAEyShKc0QVENMEL2PEhIKvgI7hdqT7oBKiTu3Ux2U5c2KbL9Dbism7nhr9FidrrkxtOsJUyR9hbZCGLnrmoHJEkNMvp3usS4HO1AxwaCSKUVXyNy3FmQJnY_R33IdSaiKsaLCJg57SIak1SexPnlUyFT6_yvbyKLjgpuL3Uk5TasQ3A8GVM2RRGVZa-s9L75rsFflkmRtAvCQca3QCRq8vHl929q6yy2TSrNlVLCeQxdK3Yapk-k_KSFS0dkoCgSQ3F5oGOECxPw-pBg
My Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Fts.App.Definitions;
using Fts.App.Services;
using Fts.Data.Defnitions;
using Fts.Data.Repository;
using Lbs.FtsClient;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
namespace Fts.Api
{
public class Startup
{
public Startup()
{
Configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
}).AddJwtBearer("Bearer",o =>
{
o.Authority = Configuration["Jwt:Authority"];
o.Audience = Configuration["Jwt:Audience"];
o.RequireHttpsMetadata = false;
o.Events = new JwtBearerEvents()
{
OnAuthenticationFailed = c =>
{
c.NoResult();
c.Response.StatusCode = 500;
c.Response.ContentType = "text/plain";
return c.Response.WriteAsync(c.Exception.ToString());
}
};
});
services.AddAuthorization(options =>
{
options.AddPolicy("apiadmin", policy => policy.RequireClaim("fts-rules", "[fts_admin]"));
});
services.AddTransient<IUserRepository, UserRepositiry>();
services.AddTransient<IDbAccess, DbAccess>();
services.AddTransient<ISiteRepository, SiteRepository>();
services.AddTransient<IAssetRepository, AssetRepository>();
services.AddTransient<IAssetService, AssetService>();
services.AddTransient<IFtsWebService, FtsWebService>();
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseCors(builder =>
builder.WithOrigins("http://localhost:4200").AllowAnyMethod().AllowAnyHeader());
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
}
I added following in my controller.
[Authorize(Roles = "apiadmin")]
[ApiController]
[Route("[controller]")]
Please help on this issue.
Thanks
You should add authentication middleware to your web api application , otherwise JWT bearer authentication won't work :
app.UseRouting();
app.UseAuthentication(); <-- add this line
app.UseAuthorization();

Missing Mutual TLS Reference in Identity Server

Good Afternoon,
I've been following the documentation for adding mutual TLS to Identity Server.
However, when I add the following code:
var builder = services.AddIdentityServer(options =>
{
options.MutualTls.Enabled = true;
options.MutualTls.ClientCertificateAuthenticationScheme = "x509";
});
I get this import reference error:
'Identity Server Options' does not contain a definition for 'MutualTls'...
It's the same for AddMutualTlsSecretValidators.
Are those references in a separate library? I scoured the documentation and have been digging around for a while but can't seem to find anything.
Any help you can give will be greatly appreciated.
I tried various imports in my Startup class such as IdentityModel, idunno.Authentication.Certificate and IdentityServer4 but those didn't help.
Here's my Startup class:
using IdentityModel;
using idunno.Authentication.Certificate;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
namespace IdentityServer
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddAuthentication()
.AddCertificate("x509", options =>
{
options.RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck;
options.Events = new CertificateAuthenticationEvents
{
OnValidateCertificate = context =>
{
context.Principal = Principal.CreateFromCertificate(context.ClientCertificate, includeAllClaims: true);
context.Success();
return Task.CompletedTask;
}
};
});
var builder = services.AddIdentityServer(options =>
{
// Complains about missing reference
options.MutualTls.Enabled = true;
// Complains about missing reference
options.MutualTls.ClientCertificateAuthenticationScheme = "x509";
})
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApis())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers())
// Complains about missing reference
.AddMutualTlsSecretValidators();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseIdentityServer();
app.UseMvcWithDefaultRoute();
}
}
}
As Ruard van Elburg said, looks like my version of Identity Server 4 was not the most current. When I updated to the latest (2.5.0) it worked.

Setting RedirectStatusCode for AddHttpsRedirection is not having any effect

Why is the following line not having any effect? It still gives me a 307!
services.AddHttpsRedirection(options => options.RedirectStatusCode = StatusCodes.Status301MovedPermanently);
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Rewrite;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace blog
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddHttpsRedirection(options => options.RedirectStatusCode = StatusCodes.Status301MovedPermanently);
services.Configure<RouteOptions>(options =>
{
options.LowercaseUrls = true;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.Use((context, next) =>
{
context.Request.PathBase = new PathString("/blog");
return next();
});
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseMvc();
}
}
}
As I read your startup config, I see that while not in development mode, you are also using HSTS. If you are testing with Chrome, analyze the network traffic response headers and see if it contains Non-Authoritative-Reason: HSTS. If this is the case, then HSTS is managing the redirect which is expected. The UseHttpsRedirection middleware is more of a fallback in this case for when the client does not support HSTS.
To test using a client that does not support HSTS and ensure the 301 is actually being returned during HTTPS redirection, use an API client such as Postman or Insomnia and view the timeline of the request.

Error when calling UseSwagger in Azure Web API

I have VS2015 and .Net Core Web API project created.
I'm following example in http://www.technicalblogs.sentientmindz.com/2017/04/09/enabling-swagger-support-to-the-web-api/
I have installed Swashbuckle.AspNetCore and next trying to code, but getting errors when using UseSwagger. Please advise me.
/* Startup.cs */
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Owin;
using Owin;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.Extensions.DependencyInjection;
[assembly: OwinStartup(typeof(TestApi.Startup))]
namespace TestApi
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
/*use swagger added by me*/
app.UseSwagger(); /*ERROR:iAppBuilder does not contain definition for UseSwagger…*/
app.UseSwaggerUI(c =>. /*ERROR :iAppBuilder does not contain definition for UseSwaggerUI…*/
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Accounts API V1");
});
}
//Add framework services by me
public void ConfigureServices(IServiceCollection services) {
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "AcccountsAPI", Version = "v1" });
});
}
}
}
I'm assuming from your code that you're using .Net Core v1.1, this is how I've done it:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
config.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "WebAPI");
c.IncludeXmlComments(GetXmlCommentsPath());
c.ResolveConflictingActions(x => x.First());
}).EnableSwaggerUi();
}
protected static string GetXmlCommentsPath()
{
return System.String.Format($#"{0}\bin\MyApi.XML",
System.AppDomain.CurrentDomain.BaseDirectory);
}