Skip to content

Instantly share code, notes, and snippets.

@VladislavMS
Created July 10, 2019 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VladislavMS/394649cd4370c08bacba9b2e098d2739 to your computer and use it in GitHub Desktop.
Save VladislavMS/394649cd4370c08bacba9b2e098d2739 to your computer and use it in GitHub Desktop.
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