Skip to content

Instantly share code, notes, and snippets.

@rkttu
Created July 7, 2017 17:19
Show Gist options
  • Save rkttu/beee8a517d6e2d530aa5f1700618a72e to your computer and use it in GitHub Desktop.
Save rkttu/beee8a517d6e2d530aa5f1700618a72e to your computer and use it in GitHub Desktop.
Web API Scratchpad with LINQPad
<Query Kind="Program">
<Connection>
<ID>bea78312-0e65-4c9e-8db4-e64dea24514e</ID>
<Persist>true</Persist>
<Server>(localdb)\hubk</Server>
<Database>PointSaving</Database>
<ShowServer>true</ShowServer>
</Connection>
<NuGetReference>Autofac.WebApi2</NuGetReference>
<NuGetReference>Microsoft.AspNet.WebApi.OwinSelfHost</NuGetReference>
<NuGetReference>Microsoft.EntityFrameworkCore</NuGetReference>
<NuGetReference>Microsoft.EntityFrameworkCore.DynamicLinq</NuGetReference>
<NuGetReference>Microsoft.EntityFrameworkCore.InMemory</NuGetReference>
<NuGetReference>Microsoft.EntityFrameworkCore.Relational</NuGetReference>
<NuGetReference>Microsoft.EntityFrameworkCore.SqlServer</NuGetReference>
<NuGetReference>Swashbuckle</NuGetReference>
<Namespace>Autofac</Namespace>
<Namespace>Autofac.Integration.WebApi</Namespace>
<Namespace>Microsoft.EntityFrameworkCore</Namespace>
<Namespace>Microsoft.Owin.Hosting</Namespace>
<Namespace>Owin</Namespace>
<Namespace>Swashbuckle.Application</Namespace>
<Namespace>System.ComponentModel.DataAnnotations</Namespace>
<Namespace>System.ComponentModel.DataAnnotations.Schema</Namespace>
<Namespace>System.Net.Http</Namespace>
<Namespace>System.Threading.Tasks</Namespace>
<Namespace>System.Web.Http</Namespace>
<Namespace>System.Web.Http.Controllers</Namespace>
<Namespace>System.Web.Http.Dispatcher</Namespace>
<IncludePredicateBuilder>true</IncludePredicateBuilder>
</Query>
[RoutePrefix("v1/values")]
public class ValuesController : ApiController
{
public ValuesService ValuesService { get; set; }
[HttpGet, Route]
public IEnumerable<string> Get()
{
return this.ValuesService.GetValues();
}
}
public class ValuesService
{
public ValuesRepository Repository { get; set; }
public IEnumerable<string> GetValues()
{
return this.Repository.GetValues();
}
}
public class ValuesRepository
{
public EFCoreDataContext Context { get; set; }
public IEnumerable<string> GetValues()
{
return new string[] { "value_a", "value_b", this.Context.Database.GetDbConnection().ConnectionString };
}
}
// Test Logic
void Main()
{
Test(
setup => setup.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()).PropertiesAutowired().InstancePerRequest(),
client =>
{
client.GetStringAsync("v1/values").Dump();
Console.Read();
});
}
#region Entity Framework Scheme
public class EFCoreDataContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder options)
{
using (var context = new TypedDataContext()) options.UseSqlServer(context.Connection.ConnectionString);
}
}
#endregion // Entity Framework Scheme
#region Configurations
static readonly Uri baseAddress = new Uri("http://localhost:9000/", UriKind.Absolute);
readonly Action<Action<ContainerBuilder>, Action<HttpClient>> Test = (setup, action) =>
{
Console.WriteLine($"Listening Address: {baseAddress}");
using (WebApp.Start(baseAddress.AbsoluteUri, (appBuilder) =>
{
HttpConfiguration config = new HttpConfiguration();
config.Services.Replace(typeof(IHttpControllerTypeResolver), new ControllerResolver());
config.MapHttpAttributeRoutes();
var builder = new ContainerBuilder();
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
builder.RegisterWebApiFilterProvider(config);
setup?.Invoke(builder);
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
config.EnableSwagger(c => c.SingleApiVersion("v1", "A title for your API")).EnableSwaggerUi();
config.EnsureInitialized();
appBuilder.UseWebApi(config);
}))
{
Process.Start(new ProcessStartInfo(new Uri(baseAddress, "/swagger").AbsoluteUri) { UseShellExecute = true });
action?.Invoke(new HttpClient() { BaseAddress = baseAddress });
}
};
public class ControllerResolver : DefaultHttpControllerTypeResolver
{
public override ICollection<Type> GetControllerTypes(IAssembliesResolver assembliesResolver)
=> Assembly.GetExecutingAssembly().GetExportedTypes().Where(i => typeof(IHttpController).IsAssignableFrom(i)).ToList();
}
#endregion // Configurations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment