Created
July 10, 2019 17:53
-
-
Save VladislavMS/394649cd4370c08bacba9b2e098d2739 to your computer and use it in GitHub Desktop.
DI 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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