public StatusMasterController(IStatusMasterRepository repos, IMapper mapper, ILogger<StatusMasterController> logger)
var result = await Task.Run(() => _repos.FetchStatusMasterList());
2. We make IRepository
6. StartUp.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using QOCR.API.Helpers;
using QOCR.IRepository.Common;
using QOCR.IRepository.Organization;
using QOCR.IRepository.Settings;
using QOCR.IRepository.General;
using QOCR.IRepository.Favorites;
using QOCR.Repository.Common;
using QOCR.Repository.Organization;
using QOCR.Repository.Settings;
//using QOCR.Repository.VesselOperations;
using QOCR.Repository.General;
using QOCR.Repository.Favorites;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Diagnostics;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.IdentityModel.Tokens;
using AutoMapper;
using QOCR.API.Controllers.Settings;
using QOCR.API.Controllers.Organization;
using QOCR.API.Controllers.General;
using QOCR.API.Controllers.Favorites;
using Microsoft.OpenApi.Models;
using System.Reflection;
using System.IO;
using QOCR.API.Controllers.Common;
using QOCR.IRepository.Auth;
using QOCR.Repository.Auth;
using QOCR.IRepository.Login;
using QOCR.Repository.Login;
using QOCR.IRepository.Menu;
using QOCR.Repository.Menu;
using QOCR.Models.Common;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Mvc;
using QOCR.Dtos.Common;
using QOCR.IRepository.Announcement;
using QOCR.Repository.Announcement;
using QOCR.API.Controllers.Announcement;
using QOCR.API.Common;
using QOCR.DBEntity;
using QOCR.IRepository.OcrMaster;
using QOCR.Repository.OcrMaster;
using QOCR.Repository.OcrMaster;
using QOCR.IRepository.OcrMaster;
namespace QOCR.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();
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddMvc(options => options.EnableEndpointRouting = false);
//services.AddCors();
services.AddCors(c =>
{
c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
});
//Gautam : For Images and Photo
services.Configure<CloudinarySettings>(Configuration.GetSection("CloudinarySettings"));
services.Configure<SqlDbSettings>(Configuration.GetSection("SqlDbSettings"));
services.Configure<MSCTauliaSettings>(Configuration.GetSection("MSCTauliaSettings"));
services.Configure<AccessLogSettings>(Configuration.GetSection("AccessLogSettings"));
//services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
// .AddJwtBearer(options =>
// {
// options.TokenValidationParameters = new TokenValidationParameters
// {
// ValidateIssuerSigningKey = true,
// IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII
// .GetBytes(Configuration.GetSection("AppSettings:Token").Value)),
// ValidateIssuer = false,
// ValidateAudience = false
// };
// });
// Adding Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
// Adding Jwt Bearer
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = Configuration.GetSection("AppSettings:ValidAudience").Value,
ValidIssuer = Configuration.GetSection("AppSettings:ValidIssuer").Value,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("AppSettings:Token").Value))
};
});
//services.AddSwaggerGen(options =>
//{
// options.SwaggerDoc("v1", new OpenApiInfo { Title = "QOCRApi", Version = "v1" });
// //Attaching Applicaton context data into XML file
// var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
// var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
// options.IncludeXmlComments(xmlPath);
//});
services.AddSwaggerGen(x =>
{
x.SwaggerDoc("v1", new OpenApiInfo { Title = "QOCRApi", Version = "v1" });
x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Description = @"JWT Authorization header using the Bearer scheme. \r\n\r\n
Enter 'Bearer' [space] and then your token in the text input below.
\r\n\r\nExample: 'Bearer 12345abcdef'",
Name = "Authorization",
In = ParameterLocation.Header,
Type = SecuritySchemeType.ApiKey,
Scheme = "Bearer"
});
x.AddSecurityRequirement(new OpenApiSecurityRequirement()
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
},
Scheme = "oauth2",
Name = "Bearer",
In = ParameterLocation.Header,
},
new List<string>()
} });
// XML Documentation
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
x.IncludeXmlComments(xmlPath);
});
//Mapper Section
services.AddAutoMapper(typeof(CountryController).Assembly);
services.AddAutoMapper(typeof(UserProfileController).Assembly);
services.AddAutoMapper(typeof(SequenceController).Assembly);
services.AddAutoMapper(typeof(UserRuleController).Assembly);
services.AddAutoMapper(typeof(AttachmentController).Assembly);
services.AddAutoMapper(typeof(EmailUtil).Assembly);
services.AddAutoMapper(typeof(MessageCenterController).Assembly);
services.AddAutoMapper(typeof(EmailManagementController).Assembly);
services.AddAutoMapper(typeof(AnnouncementController).Assembly);
services.AddAutoMapper(typeof(FavoriteSettingController).Assembly);
//Auth
services.AddSingleton<IAuthRepository, AuthRepository>();
services.AddSingleton<ILoginRepository, LoginRepository>();
//Menu
services.AddSingleton<IMenuRepository, MenuRepository>();
//General
services.AddSingleton<IUserRuleRepository, UserRuleRepository>();
services.AddSingleton<IMessageCenterRepository, MessageCenterRepository>();
services.AddSingleton<IEmailManagementRepository, EmailManagementRepository>();
services.AddSingleton<IExceptionRepository, ExceptionRepository>();
services.AddSingleton<IFeedbackRepository, FeedbackRepository>();
services.AddSingleton<IRegionRepository, RegionRepository>();
services.AddSingleton<ICountryRepository, CountryRepository>();
services.AddSingleton<ITerminalRepository, TerminalRepository>();
services.AddSingleton<ISequenceRepository, SequenceRepository>();
services.AddSingleton<IDepartmentRepository, DepartmentRepository>();
services.AddSingleton<IDesignationRepository, DesignationRepository>();
//services.AddSingleton<IVesselRepository, VesselRepository>();
services.AddSingleton<IDropDownRepository, DropDownRepository>();
services.AddSingleton<ILocationRepository, LocationRepository>();
//Organization
services.AddSingleton<ICompanyRepository, CompanyRepository>();
services.AddSingleton<IUserProfileRepository, UserProfileRepository>();
services.AddSingleton<IRoleRepository, RoleRepository>();
//OcrMaster
services.AddSingleton<IStatusMasterRepository, StatusMasterRepository>();
services.AddSingleton<IProcessMasterRepository, ProcessMasterRepository>();
services.AddSingleton<IDocumentMasterRepository, DocumentMasterRepository>();
//attachment
services.AddSingleton<IAttachmentRepository, AttachmentRepository>();
services.AddSingleton<IAlertsRepository, AlertsRepository>();
//end
//Email
services.AddSingleton<IEmailUtil, EmailUtil>();
services.AddSingleton<IEmailData, EmailData>();
//end
services.AddSingleton<IWorkFlowRepository, WorkFlowRepository>();
//end
services.AddSingleton<IEmailUtil, EmailUtil>();
services.AddSingleton<ITemplateResolver, TemplateResolver>();
services.Configure<EmailConfig>(Configuration.GetSection(EmailConfig.Option));
//vessel operations
//services.AddSingleton<IFixedCostRepository, FixedCostRepository>();
//services.AddSingleton<ITugInterchangeRepository, TugInterchangeRepository>();
//services.AddSingleton<IVesselTrackerRepository, VesselTrackerRepository>();
//end
//Share Feature
services.AddSingleton<IShareFeatureRepository, ShareFeatureRepository>();
//end
// Announcement
services.AddAutoMapper(typeof(AnnouncementController).Assembly);
services.AddSingleton<IAnnouncementRepository, AnnouncementRepository>();
//End
// Audit Trail
services.AddSingleton<IAuditTrailRepository, AuditTrailRepository>();
services.AddSingleton<IActivityLogRepository, ActivityLogRepository>();
//End
services.AddSingleton<IImagesRepository, ImagesRepository>();
services.AddSingleton<IEscalationRepository, EscalationRepository>();
services.AddSingleton<IDataArchivalRepository, DataArchivalRepository>();
//favorites
services.AddSingleton<IFavoriteSettingRepository, FavoriteSettingRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
[Obsolete]
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler(builder =>
{
builder.Run(async context =>
{
context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
var error = context.Features.Get<IExceptionHandlerFeature>();
if (error != null)
{
var InnerExp = error.Error.InnerException;
string DetailMsg = string.Empty;
if (InnerExp == null)
{
DetailMsg = error.Error.Message;
}
else
{
DetailMsg = ((Npgsql.PostgresException)InnerExp).Code;
if (DetailMsg.Contains("2350"))
{
DetailMsg = "GFE" + DetailMsg;
}
else
{
DetailMsg = ((Npgsql.PostgresException)InnerExp).Detail;
}
}
context.Response.AddApplicationError(DetailMsg);
await context.Response.WriteAsync(DetailMsg);
}
});
});
app.UseHsts();
app.UseHttpsRedirection();
}
//app.UseHsts();
var swaggerOptions = new Helpers.SwaggerOptions();
Configuration.GetSection(nameof(Helpers.SwaggerOptions)).Bind(swaggerOptions);
app.UseSwagger(options => { options.RouteTemplate = swaggerOptions.JsonRoute; });
app.UseSwaggerUI(options => { options.SwaggerEndpoint(swaggerOptions.UIEndpoint, swaggerOptions.Description); });
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
//app.UseHttpsRedirection();
app.UseCors(x => x.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod());
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Fallback", action = "Index" }
);
});
}
}
}
Great Article and very helpful.
ReplyDelete