EnableDependencyInjection in OData extension library - asp.net-web-api2

Our service has been using ApiController with EnableQuery attribute and we were using System.web.http.odata (odata < 6). When we moved to new ODataControllers in Odata 6.0 dlls, we started getting error no non-odata route registered.
This got solved by using EnableDependencyInjection which apparently creates non-odataroutes containers.
My question is what could be the other side effect (security, route configuration) of calling this method.

Related

Issue when configuring Hangfire in ASP.NET app

I'm attempting to wire up Hangfire in my ASP.NET application (not Core) using the Global.asax.cs route explained in their documentation. However, none of their extensions methods on GlobalConfiguration.Configuration seem to be working for me. It appears, for one example, that SetDataCompatibiltyLevel() method extends Hangfire.IGlobalConfiguration, which my GlobalConfiguration.Configuration does not inherit from. I even backed off a few versions of Hangfire and it doesn't seem to make a difference. When I attempt to access the extensions method directly, I get this:
Prefix GlobalConfiguration class with the Hangfire namespace – System.Web.Http namespace defines its own GlobalConfiguration class, so you get a conflict. And that another class is referenced, most likely because you have the System.Web.Http namespace included in the beginning of the file.
Hangfire.GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibiltyLevel.Version_170);
You can also combine all the calls to the GlobalConfiguration class in a single chain:
Hangfire.GlobalConfiguration.Configuration
.SetDataCompatibilityLevel(CompatibiltyLevel.Version_170)
.UseXXX()
.UseYYYStorage();

WebApi Core 2.2 OData resource/path not found

I'm using WebApi Core 2.2. The Microsoft OData Client is adding a new parent record plus a subrecord (Deal+DealFee) from a WPF application. I'm hosting in IIS on Windows 10.
When I call container.SaveChanges(), it successfully calls the service to add the parent Deal record, but then it does a SECOND POST operation to this url (this is generated by the MS odata client lib):
POST http://localhost/mysite/odata/Deals(14)/DealFees
(note this includes the ID 14 which was just generated when adding the Deal)
This is two separate POSTs from the MS odata client lib, not a "deep insert" apparently. However, this results in a 404 (NotFound), which I can observe in Fiddler. The following urls DO work perfectly:
/odata/Deals
/odata/Deals(14)
/odata/DealFees
It seems like either the WebApi Core 2.2 service is not handling the POST to /Deals(14)/DealFees path, OR /Deals(14)/DealFees isn't a valid odata Uri? Is this kind of path generally supported in OData?
I don't know. Can anyone shed some light on what's going on?
Deep insert is not supported in WebAPI OData as of now. To me, it seems like the client is updating the resource set and the resource set for the navigation with two separate post requests and the reason you are getting a 404 is that there is no action mapped to the second request URI in the service.
The service can support this either by introducing a PostToDealFeesFromDeals controller action with default OData routing convention or use attribute routing to map the action for such requests.
If the action already exists then it might be that the first request did not finish creating the new record and the second request was fired, hence 404.

Error management in ASP.NET Core using a BaseController

In previous ASP.NET versions I was used to create a BaseController inherited from the other controllers and there intercepting the general error and logging each error with a simple logging method and passing the ExceptionContext filterContext.
Should I do the same in ASP.NET 5?
I see in file Startup.cs that there is a if/else statement that basically separate the debug/live condition, with line
app.UseErrorHandler("/Home/Error");
for a production application.
How am I supposed to hook in the process and log the errors?
Handling and logging errors in ASP.NET 5 involves a few elements.
To handle the error in a production scenario and show an error page, the app.UseExceptionHandler() method is the way to go. The Diagnostics repository on the ASP.NET GitHub organisation includes a sample that shows this. For development-time scenarios where you want to see a full stack trace and other diagnostic information, use app.UseDeveloperExceptionPage() as seen in this sample.
If the application is using ASP.NET MVC 6, then there is an MVC-specific way of handling errors, much as there was in earlier versions of ASP.NET MVC. In ASP.NET MVC 6, a filter (or the Controller itself, which is also a filter) can handle/override the OnActionExecuted method and inspect the ActionExecutedContext parameter to attempt to handle the error.
When it comes to logging, there's a new logging infrastructure in ASP.NET 5 that reports a great deal of information to any registered ILogger. The default project templates in Visual Studio 2015 register some loggers that log errors (and other data) to the console and the Visual Studio debug output window. However, when running in IIS or IIS Express there is no console window (yet!). But if you run from the command line (using dnx web) then you'll see the error. Or, of course, you can register a different logger that writes to a log file or database and see the logs there.
To register a custom ILogger in a web app:
Write a logger that implements the ILogger interface. See the DNX implementations for how to do this.
Add a parameter of type ILoggerFactory to your app's Startup class's Configure method.
In the Configure method call loggerFactory.AddProvider(<some provider>) and pass in an instance of your logger.

Calling WebApi Controller action, causes Inheritance security rules violated HostedHttpRouteData.get_Route(

I have a .Net Mvc4 project, I've added a WebApi Controller and a basic action in for an ajax call.
When I call the method I get:
Inheritance security rules violated while overriding member:
'System.Web.Http.WebHost.Routing.HostedHttpRouteData.get_Route()'.
Security accessibility of the overriding method must match the
security accessibility of the method being overriden.
I haven't made any custom modifications or set up anything for web Api to work (was I supposed to?)
I noticed the DefaultApi route is in my routeConfig.
I'm also using Unity Mvc4 & Unity Web Api packages, (which I've disabled to test if it was causing the issue but doesn't seem to be).
My controller is TestController
action:
public bool ClearAwaitingNotifications()
{}
& ajax call is going to: api/test/clearawaitingnotifications
How can I fix this?
My exception occured when hitting controller because:
WebApiConfig.Register(GlobalConfiguration.Configuration);
was never called, although the route existed inside my Routeconfig.cs. I think this occurred because I installed my Mvc4 project using a template, which installs some WebApi stuff and it is all a bit old. I then added this line into Global.ascx and received the same security warning but for this line in Global.ascx.
https://stackoverflow.com/a/18713412/314963 then helped me resolve the issue, and now everything is working. In short, it is because of installing Mvc project with a template, or because one of the libraries in webapi 4 was outdated.

SignalR and Ninject - Ninject not returning a controller for the route

I'm trying to set up SignalR in an existing ASP.Net MVC 4 project that uses Ninject for DI, however whenever I try and access the SignalR default route (which I have confirmed is in the route table), I get the following exception thrown:
Exception message: The IControllerFactory 'Infrastructure.NinjectControllerFactory' did not return a controller for the name 'signalr'.
I have tried injecting the Ninject kernel into SignalR via:
GlobalHost.DependencyResolver = new NinjectSignalRDependencyResolver(ninjectKernel);
And I am setting the default routes via:
RouteTable.Routes.MapHubs();
in Global.asax Application_Start
I can't seem to find any way to resolve this issue.
Ok, resolved it myself - it was not a ninject issue, it was a routing order issue.
I needed the ~/signalr route to be higher up in the routing table order, which meant shifting the RouteTable.Routes.MapHubs(); entry to the top of my Application_Start, above the RegisterRoutes call.