I am hosting my own WebApi, as can be seen below: This is run as a Windows service, so can be started and stopped.
I am using Swagger/Swashbuckle to test my api.
I have the following code.
public class ApiShell : IApiShell
{
IDisposable _webApp;
public void Start()
{
_webApp = WebApp.Start<Startup>("http://localhost:9090");
Console.WriteLine($"Web server running at 'http://localhost:9090'");
}
public void Stop()
{
_webApp.Dispose();
}
internal class Startup
{
//Configure Web API for Self-Host
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
config.EnableSwagger(c =>
{
c.SingleApiVersion("1", "Api");
c.PrettyPrint();
}).EnableSwaggerUi(c => c.EnableDiscoveryUrlSelector());
var resolver = new AutofacWebApiDependencyResolver(IoC.Container);
config.DependencyResolver = resolver;
config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional });
config.MapHttpAttributeRoutes(); //Swagger does not load when I have the following uncommented
config.Formatters.Remove(config.Formatters.XmlFormatter);
app.UseWebApi(config);
GlobalConfiguration.Configuration.EnsureInitialized();
}
}
}
The problem is, when I don't add config.MapHttpAttributeroutes
, I get the following error:
Multiple operations with path 'Entry' and method 'GET'
When I do add config.MapHttpAttributeRoutes
to try and solve this, I get the following error:
The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called in the application's startup code after all other initialization code
I have added GlobalConfiguration.Configuration.EnsureInitialized();
, but this does not help.
Here is my routes:
[RoutePrefix("api")]
public class EntryController : ApiController
{
private IActorSystemShell _actorSystem;
public EntryController(IActorSystemShell actorSystem)
{
_actorSystem = actorSystem;
}
[HttpPost]
public async Task<dynamic> AddPhoneBook([FromBody] Entry entry)
{
...
}
[HttpGet]
public async Task<dynamic> GetEntries()
{
...
}
[HttpGet]
[Route("id/{id}")]
public async Task<dynamic> GetEntryById(int id)
{
...
}
[HttpGet]
[Route("text/{searchText}/SearchAll")]
public async Task<dynamic> SearchEntries(string searchText)
{
...
}
[HttpGet]
[Route("book/{bookId}/{searchText}")]
public async Task<dynamic> SearchInPhoneBook(int bookId, string searchText)
{
...
}
}