Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DI 1
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace DiExamples
{
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<IPhotoService, PhotoService>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.Run(async (context) =>
{
var photoService = context.RequestServices.GetRequiredService<IPhotoService>();
await context.Response.WriteAsync(photoService.Upload());
});
}
}
public interface IPhotoService
{
string Upload();
}
public class PhotoService : IPhotoService
{
private IHostingEnvironment _env;
public PhotoService(IHostingEnvironment env)
{
_env = env;
}
public string Upload()
{
// Do some work
return "Upload Done";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.