How to prevent application pool stop, on POST requests? - c#

I have a web api service placed on https sub domain. I used castle Windsor to create all classes (per web request), from controller to domain objects.
I tested all actions on local machine and also using fiddler. everything works fine. And no memory leak was happened. and also we have a mobile programmer which worked with all actions and again every thing was fine.
but after hosting on https, by each request to web api urls, application pool stops.
but the request to authorization works fine. What is wrong with my application? here is my code to Config file:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config, IAppBuilder app)
{
// Web API configuration and services
var container =Bootstrapper.WireUp();
ConfigCors(container, config);
RegisterControllers(container);
UserManagement.Config.Bootstrapper.WireUp(container);
config.Services.Replace(typeof(IHttpControllerActivator), new WindsorControllerActivator(container));
container.Register(Component.For<IDataProtectionProvider>().ImplementedBy<DpapiDataProtectionProvider>()
.UsingFactoryMethod(x => app.GetDataProtectionProvider()));
// Web API routes
config.MapHttpAttributeRoutes();
config.AddFiveLevelsOfMediaType();
config.Routes.MapHttpRoute(
name: "DefaultApiWithAction",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//config.Routes.MapHttpRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = #"\d+" });
}
private static void ConfigCors(IWindsorContainer container, HttpConfiguration config)
{
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
}

There is no specific reason that application pool is stopping. It could be a windows registry error, system error or assembly permission error. The best way to find out why is to go and check event viewer.
From Administrative Tools select Event Viewer
Alternatively, press Windows Key and type Event Viewer. Once there look into the into Windows Logs and then Application logs. Search for any error (red icon) or warning (yellow icon) that is related to IIS / ASP.Net or .Net framework. I have attached a sample image from my system -
Another Instance -
** EDIT **: There is one other possibility since it stopped after enabling https. Check the SSL certificate. The most common reason is using same certificate in multiple sites. Only wild card certificates can be used in multiple site. If the certificate is self signed, delete it and create a new one with * , being the prefix for the certificate name. Then use that one.

Related

Simple controller routing doesn't work in IIS for asp.net site hosted in IIS

Trying to host locally a rest service in IIS. Everything works fine in IIS Express but as soon as I try to host locally in IIS the routing doesn't work.
It's compiled to x86 and is hosted inside of an app pool that allows for 32 bit processes.
I have the site mapped to the binary output folder from my build msbuild /p:Configuration=Release /p:ProcessorArchitecture=x86 -r:False
I have Anonymous Authentication enabled
Binding is type http to port 8000
I have enabled directory browsing and see the below at http://localhost:8000/
However whenever I try to navigate to http://localhost:8000/api/HeartBeat I get a 404 - Not Found.
My WebApiConfig class which sets a default route.
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 }
);
}
}
Here is the controller
public class HeartBeatController : ApiController
{
// GET: api/HearBeat/5
public HttpResponseMessage Get()
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
If I run this in IIS Express and navigate to that same url I get a 200. Also I've tried to examine the URL in powershell using Get-WebUrl and it shows a ProtocolError but I have no clue what or why. And IIS Manager doesn't seem to complain about anything.
PS C:\Windows\system32> Get-WebURL -PSPath "IIS:\Sites\Test" | Format-list
Status : ProtocolError
ResponseUri : http://localhost:8000/
Description : Forbidden
A couple of things were amiss.
Publishing via Visual Studio is quite different than the build output that was created via the build command above.
I was missing under Windows features/IIS/Application Development Features
Finally was still getting a security error which required adding an ACL rule to the directory

How to connect container deployed in AKS

I am using the code in the below link to create a web api.
https://learn.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api
I am using only the Products controller and Products code from the link and not using the UI related code. With local docker container, it is working fine.
starting Point:
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 }
);
}
}
When I deploy the same in AKS and try to access the api, it is giving an error.
I am using the url "https://{service external ip}/api/products".
Can someone help me to connect to the AKS in the right way if I am doing any mistake.

MVC Web Api error 403.14

I am doing an MVC5 Web API Application. I am doing an simple example.
Create an Web Asp.Net web Application.
Select Empty and API.
Then I add a Api2 Controller called Home, and add a Simple Method called Get()
Method Get() looks like this.
public string Get()
{
return "Hello World";
}
I run the application and complete the URL.
http://localhost:56464/Home/Get
Got an error
Error HTTP 404.0 - Not Found
I test changing WebApiConfig adding
{action}
but I get the same error.
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
When I start the Application http://localhost:56464/, I got this error
Error HTTP 403.14 - Forbidden
I always run the Application from Visual Studio 2013. I did not publish it it IIS
What is missing?
There are a few issues.
First the web api route template is
api/{controller}/{action}/{id}
Note the api prefix.
So that would mean that you have to browse to
http://localhost:56464/api/Home/Get
http://localhost:56464/ wont work because the route has the api prefix. So the forbidden error is default for what ever is hosting at that address.
To be able to use the URL you want in the question you would need to change the route template to match your desired template.

Self Hosted Owin Web API 2 application: How do I start the web app with arguments

I am trying to add a REST API to a console application using self hosted Owin Web API 2.
// Starting the REST API
string baseAddress = "http://localhost:8000/";
WebApp.Start<WebApi.WebApiStartup>(url: baseAddress);
I am relying on previously instantiated objects to access a third party API (used in the console application).
Since the Web API mimics the console application behavior it should use the same objects. Thus my question: How do I pass those objects to the constructor of the web app?
Once the webapp started I use unity to Inject the objects to the controllers:
var container = new UnityContainer();
container.RegisterInstance<AggregatePosition>(position);
HttpConfiguration config = new HttpConfiguration();
config.DependencyResolver = new UnityResolver(container);
config.Routes.MapHttpRoute(name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
in this example the object "position" should be passed to the web app at initialization. Is there a way to Inject it there?

Azure Mobile App - Getting 405 (Method Not Allowed) when trying POST

I'm trying to migrate my Azure Mobile Service .NET backend to an Azure Mobile App.
I was using some custom Web Api controllers, and after migration I'm getting a 405 (Method Not Allowed) / The requested resource does not support http method 'POST'. error when trying to POST to a controller method that worked before.
I spent hours trying diffent CORS settings but I had no success so far.
This is how I currently configure Web Api:
HttpConfiguration config = new HttpConfiguration();
new MobileAppConfiguration()
.UseDefaultConfiguration()
.ApplyTo(config);
var cors = new EnableCorsAttribute("*", "*","*");
//var cors = new EnableCorsAttribute("*", "*","GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS");
config.EnableCors(cors);
config.Routes.MapHttpRoute(
name: "Rest",
routeTemplate: "rest/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.MapHttpAttributeRoutes();
The controller looks like that:
[Authorize]
[RoutePrefixAttribute("rest/companies")]
public class CompaniesController : ApiController
{
[HttpPost]
[Route("my-active")]
//[EnableCors("*","*","*")]
public HttpResponseMessage SetActive(/*[FromBody]*/Company company)
{
// Implementation
}
}
What I tried too:
Set CORS settings in web.config (custom headers / different settings), eg. <add name="Access-Control-Allow-Methods" value="GET,POST,DELETE,HEAD,PUT,PATCH,OPTIONS" />
Added a cors message handler according this blog post
(http://blog.bittercoder.com/2012/09/09/cors-and-webapi/)
This handler is also removed: <remove name="OPTIONSVerbHandler" />
One thing I noticed is, that a Azure Mobile App component seems to override the allowed methods and allowed headers that I configured using config.EnableCors(cors). I was only able to control all settings using web.config and the message handler. But it did not solve the 405 problem anyway.
At this point, I'm not sure if it's a CORS problem at all.
Any ideas? It's currently hard to find good documentation on Mobile Apps and I would appreciate if the .NET backend part would be open sourced... It's somewhat of a black box for me.
It could happen when you activate App Service Authorization and forget to change your mobile client url from http to https.
If so, your http Post will be redirected to the https url but with a Get message.
Found it thanks to Fiddler.
OMG, I found the problem with my code. I had to swap this two statements:
// Needs to be called before MapHttpRoute
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "Rest",
routeTemplate: "rest/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
As I was using Azure Mobile Services, calling MapHttpAttributeRoutes caused an error 'An item with the same key has already been added', so I removed that line. I had to re-insert it for Azure Mobile Apps again in order to get attribute routing to work, but I did it at the wrong place, so be careful.
If http Post is redirected to the https url as Get, try calling https directly.
Azure logs looks as follows in this case:
Received request: POST http://xxx.azurewebsites.net/api/Data/test
Information Redirecting: https://xxx.azurewebsites.net/api/Data/test
Received request: GET https://xxx.azurewebsites.net/api/Data/test
in this case call https://xxx.azurewebsites.net/api/Data/test

Categories