Please help, I'm trying to self-host a web api.
When the same controller is hosted on a web project, and run on dev, via F5, everything works well.
However now I'm trying to self-host the same, I'm getting 411 and 404. 411, when I use Fiddler to connect, 404 when I'm attempting to connect via another library.
This is the console app that's supposed to host the service:
class Program
{
static int portNumber;
static void Main(string[] args)
{
portNumber = 8089;
var config = new HttpSelfHostConfiguration(
string.Format("http://localhost:{0}", portNumber));
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (var server = new HttpSelfHostServer(config))
{
var test = new RetrieveGuidService().Execute(Unit.Instance);
server.OpenAsync().Wait();
Console.ReadLine();
}
}
}
This is what my controller looks like, it doesn't do anything it's just a test.
public class RetrieveGuidServiceController : ApiController
{
public virtual Guid PostExecute(Unit request)
{
IQueryService<Unit,Guid> queryService = new RetrieveGuidService();
return queryService.Execute(request);
}
}
And here's how I'm attempting to access it via fiddler:
The same works when the service is hosted on a web project. I have followed this tutorial almost to the letter: asp.net WebApi self host tutorial which includes running nugget scripts, adding dependencies, etc.
What am I still missing?
The 411 is because you didn't put the Content-Length header. Even if you are not sending content you need to include Content-Length: 0.
Regarding having the Controller in the correct assembly I have had inconsistent behaviour. In some projects it seems to work in other it doesn't. Not sure what I am doing wrong. I have a project here that does both Web Host and Self-Host with all the controllers in a different assembly and it works just fine.
It seems that by default, the services will not look for controllers in assemblies beyond the one hosting the services.
I think it's an omission, unless I'm reading the specs wropng.
Related
I am working on a final project in my school and there are a lot of new things to discover. We will make an API that will use Google Cloud APIs and deployment to Azure.
I have not gotten anywhere just googling this, it looks like everyone just uses one service or another but not together.
I have a basic .net Core API program with the initial controller, ValuesController. After getting the ServiceKey from Google Cloud, I used one of their basic tutorial code for calling the Vision API with an image. My problem is that everything works on localhost but after deploying the site to Azure and calling that controller in the browser, Azure site always gives me 500 internal server error:
Failed to load resource:
the server responded with a status of 500
(Internal Server Error).
I tried to setup a debug for Azure but with no success.
`[Route("api/sleepy")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET api/values
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
{
List<String> da = new List<String>();
var client = ImageAnnotatorClient.Create();
var image = Image.FromFile("shit.png");
var response = client.DetectLabels(image);
foreach (var annotation in response)
{
if (annotation.Description != null)
{
Console.WriteLine(annotation.Description);
da.Add(annotation.Description);
}
}
foreach (var stuff in da)
{
Console.WriteLine(stuff);
}
System.Threading.Thread.Sleep(5000);
return Ok(da);
} `
When I just return a string or something else from my GET in ValueController, that works on the Azure site, I do not understand this problem of mine, localhost gets the right information but something is missing since it does not work on the Azure deployed site.
The site i have been trying this with is:
https://webapplication220190211113216.azurewebsites.net/api/sleepy. It is just temporary to test this. THe image shit.png is a picture of a dog and located in the root of the project.
What am i missing here? I would be very greatful if someone could at least guide me in the right directions, I have found no answers on google. :)
EDIT:
I managed to find a workaround by using regular Http requests, it would still be interesting to hear the reasons for why using the client libraries result in 500 error on Azure. :)
I setup a reactredux/dotnetcore app with the dotnet new reactredux command. I'm trying to test my api but whenever I try to fetch something, the only thing I get back is the index.html from the react client. Even for endpoints I have not specified.
I have my app running on localhost 5000 and 5001 and for both I get the same result.
Did I forget to set some setting?
Am I using the wrong address? Where can I find the right one.
ASP.NET Core SPA templates will redirect you to the SPA index.html static page whenever the MVC backend doesn't have a matching route for the request. Add this controller to your application and see if you can get a response from it. If it works compare the changes to the rest of the controllers you're calling:
[Route("api/[controller]")]
public class TestController : ControllerBase
{
[Route("[action]")]
public IActionResult Test() => Content("Hello World!");
}
I have an ASP WebAPI application that works great.
Without changing anything massive within the application, I'm trying to create a new console project with self-hosting components to host the application (mainly for running it fast when debugging other components of my solution without opening and compiling from Visual Studio).
I saw plenty of solutions for using Microsoft.AspNet.SignalR.SelfHost package,
For example: Working With OWIN Hosting and Self Hosting in ASP.Net.
All the solutions I saw, use the following structure:
string baseAddress = "http://localhost:9000/";
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/values").Result;
Console.WriteLine(response);
Console.WriteLine(response.Content.ReadAsStringAsync().Result);
}
Console.ReadLine();
This is a bit problematic for me, since using (WebApp.Start<Startup>(url: baseAddress)) is calling the Configuration method in Startup class.
public class Startup
{
// This code configures Web API. The Startup class is specified as a type
// parameter in the WebApp.Start method.
public void Configuration(IAppBuilder appBuilder)
{ ... }
}
My solution has plenty of code inside the Application_Start() (inside the Global.aspx.cs file) that I'm trying to reach when starting the console self-host application. So far without any success.
Is this a lost battle and I should move as much as I can from Application_Start method to Configuration method? Or is there any way I call call the Application_Start somehow, prior to running the Configuration method?
I tried creating an instance of the ASP application and call the Application_Start, but that didn't work.
Thanks!
Guy
I'm really new to WebApi and I've been reading information about it but I don't know how to start my application.
I already had a project with many WFC services with .Net 3.5. So, I updated my project to 4.5.1. Then I created a controller with the Visual Studio 2012 Wizard. Then, when the controller is created, I see a class as a template with the get, post, put, delete methods. So I place my post method and finally I want to test the service with a HttpClient.
I tried to apply the solution in green from the following forum:
How to post a xml value to web api?
I'm gonna receive a XML string with the structure of a Contract model.
I run my project into Visual Studio Development Server.
But I have troubles with URL to test my service.
I saw many pages where people do something like this http://localhost:port/api/contract. But I don't still know how it works. So how can I do to test my service? What is it my path or url to test my service?
WebApi, like MVC, is all based on the routing. The default route is /api/{controller}/{id} (which could, of course, be altered). This is generally found in the ~/App_Start/WebApiConfig.cs file of a new project, but given you're migrating you don't have it most likely. So, to wire it up you can modify your Application_Start to include:
GlobalConfiguration.Configure(WebApiConfig.Register);
Then, define that class:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
If you created a TestController controller and wanted to make a POST request to the instance running locally, you'd access http://localhost:12345/api/Test/ (with the appropriate verb). 12345 would be the local port that Visual Studio is using to host your service (and can be found by viewing the project's properties, then navigating to the "Web" tab).
Having said that, testing is probably best performed on the the project (without making an external call). There are several posts on the subject, but generally come down to something like the following:
[TestMethod]
public void Should_Return_Single_Product()
{
// Arrange
var repo = new FakeRepository<Widget>();
var controller = new WidgetController(repo);
var expected = repo.Find(1);
// Act
var actual = controller.GetWidget(1) as OkNegotiatedContentResult<Widget>;
// Assert
Assert.IsNotNull(actual);
Assert.AreEqual(expected.Id, actual.Content.Id);
}
So after a great deal of research I'm starting to enhance our service server stack with a webAPI entry point. Based on this thread, and especially the last post by a member of the Digerati board, we are implementing webAPI services as a facade into our WCF application layer. (Our WCF services are just facades into our Application layer where all of the behavior lives)
My question is this. I downloaded MVC 4 and created a new WebAPI project in my service solution. But wow there was a ton of crap that created in my project that I just am not going to need! For example, all of the image files, the home controller, views and models, etc.
So in stripping this down to be just a service project, what are the minimum files I need to build a functional service project? Our intent is to publish both of the service types (WCF and webAPI) side by side in the same server .. each service call doing the same identical service call and returning the specific DTO for the request. So far it looks like App_Start, Controllers, and the Glabal.asax/web.config entries. I definitely don't need Views, Models, or Images!!!
Any input on what others have done to do a pure service deployment would be of great welcome here.
Same problem here. I've found that article from Shawn Kendrot explaining how to create minimal Web API project. It was written for the beta version of Web API but it seems to be still valid.
Create an empty ASP.NET project.
Add a reference to System.Web.Http and System.Web.Http.WebHost (version 4.0.0.0)
Add a Global.asax file
Register a route in the Global.Application_Start. Something like:
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configuration.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
}
Add a controller
public class SampleController : ApiController
{
public string Get(int id)
{
return "Hello";
}
}
Run the project locally with the URL /api/sample/123 and enjoy the outcome:
FYI. I have found that I have had to reference two more .dlls:
System.Net.Http
Newtonsoft.Json