how to use Elmah.Contrib.WebApi 1.0.9? - asp.net-web-api

I am working on WEB API project. I want to implement global error handling and i choose ELMAH for that.
On googling i found i can implement ELMAH in WEB API with Elmah.Contrib.WebApi package.
So i installed the package Elmah.Contrib.WebApi and as written in author's github site i registered it.
so my global.asax looks like following.
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());
GlobalConfiguration.Configuration.MessageHandlers.Add(new MessageLoggingHandler());
}
but it does not seem to work. i also tried to find documentation on how to implement this package in project but could not find it.
can someone help me so i can get work that pacakge?

If you are using Web API 2, you should use the new exception logger:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
...
config.Services.Add(typeof(IExceptionLogger), new ElmahExceptionLogger());
...
}
}
Also shown in this post:
http://blog.elmah.io/logging-to-elmah-io-from-web-api/
(without the elmah.io package if you are using another log store)

Related

Unable to resolve service for type 'AspNetCoreRateLimit.IProcessingStrategy' while attempting to activate 'AspNetCoreRateLimit.IpRateLimitMiddleware'

I have used the package AspNetCoreRateLimit Version="4.0.1" and I get the following exception: Unable to resolve service for type 'AspNetCoreRateLimit.IProcessingStrategy' while attempting to activate 'AspNetCoreRateLimit.IpRateLimitMiddleware' but when I use the package AspNetCoreRateLimit Version="3.2.2" it works. Although Version="4.0.1" is the latest stable version I am not being able to use it. What sort of bugs can I expect later if I keep on using Version="3.2.2"?
According to the author of AspNetCoreRateLimit, you have to add following line of code in Startup.cs
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
It seems as they no longer have a default IProcessingStrategy registered. See here: https://github.com/stefanprodan/AspNetCoreRateLimit/issues/236#issuecomment-883311511
In the new versions, some code is embedded in the method,
public static IServiceCollection AddInMemoryRateLimiting(this IServiceCollection services)
{
services.AddSingleton<IIpPolicyStore, MemoryCacheIpPolicyStore>();
services.AddSingleton<IClientPolicyStore, MemoryCacheClientPolicyStore>();
services.AddSingleton<IRateLimitCounterStore, MemoryCacheRateLimitCounterStore>();
services.AddSingleton<IProcessingStrategy, AsyncKeyLockProcessingStrategy>();
return services;
}
Therefore, it will be sufficient to add the following services;
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
services.AddMemoryCache();
services.Configure<IpRateLimitOptions>(Configuration.GetSection("IpRateLimiting"));
services.Configure<IpRateLimitPolicies>(Configuration.GetSection("IpRateLimitPolicies"));
services.AddInMemoryRateLimiting();
services.AddMvc();
services.AddSingleton<IRateLimitConfiguration, RateLimitConfiguration>();
}
You can get information about changes and option differences from here.

How to fix the Veracode Flaw: CWE-489: Leftover Debug Code?

Here in my application I have class testapp in that I have some methods and main method. When I'm using veracode tool its showing flaw at main method saying Veracode CWE-489:Leftover Debug Code. In my psvm main method I have some Syso lines only.
public class testapp {
public static void main(String[] args) {
System.out.println("Test_One");
System.out.println("Test_Two");
System.out.println("Test_Three");
System.out.println("Test_Four");
System.out.println("Test_Five");
}
void dotest() {
}
void runtest() {
}
}
Can anybody guide me how to fix this issue.
The Veracode engine is looking for the presence of the main() function for this vulnerability within the Java code. All you can do in essence is remove it.
The check within Veracode SAST for this issue is controlled by the fact that for a Java Enterprise web application, you shouldn't have a main function as per the spec for a J2EE app.
As long as you don't have any CWE-470 Unsafe Reflection issues in the app that are exploitable from the outside or that the app server is insecurely configured then you shouldn't need to worry about this.

How to modify subject of Elmah emailing when using WebApi

Normally in my Mvc project, I simply have a ErrorMail_Mailing method in Global.asax.cs which gives access to ErrorMailEventArgs.
But in WebApi, this method does not fire, so how can I access this information in Webapi?
I am currently using this methodology (which is working fine)
public class ApiErrorHandler : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext context)
{
DepResolver.ExceptionHelper().LogToElmah(context.Exception);
base.OnException(context);
}
}
ELMAH doesn't fire on Web API by default. You need to either catch the error yourself and log it to ELMAH or even better, use the Elmah.Contrib.WebApi NuGet package: http://www.nuget.org/packages/Elmah.Contrib.WebApi/. With that package installed, simply add the following code to your Application_Start:
GlobalConfiguration.Configuration.Filters.Add(new ElmahHandleErrorApiAttribute());
This should trigger that your ErrorMail_Mailing method is called.

Web API Self hosting from a test assembly

I'm currently evaluating WebAPI and NancyFx for a new project about to start. I've managed to get Nancy to self host from a test assembly (by itself it uses asp.net hosting).
Is there any way to do the same with Web API? I would like to keep the web api project hosted on IIS, but i would like to spin it up from my test assembly, so i can run tests against it.
I have found some blogposts on how to use Autofac to scan controllers from another assembly (seems a little backwards only to get hosting from another assembly to work, but if it can be done, i guess that would be an option), but i would like to keep using Structuremap ioc for this project.
Managed to get it working with help from Mark Jones link. This is what i ended up with in my test assembly.
private static HttpSelfHostServer _server;
[BeforeTestRun]
public static void Setup()
{
var config = new HttpSelfHostConfiguration(Settings.TestUri);
WebApiConfig.Register(config); //map routes
IocConfig.Bootstrap(config); //configure dependency injection
_server = new HttpSelfHostServer(config);
_server.OpenAsync().Wait();
}
[AfterTestRun]
public static void TearDown()
{
_server.CloseAsync().Wait();
}

Testing If a class is being activated using WebActivator and if add a IModelBinder to ModelBinderProviders.BinderProviders.

Dear fellows from Stack Exchange.
I'm trying to test if my Custom Model Binder is being added to the ModelBinderProviders.BinderProviders collection.
I decided to activate this through WebActivator, to avoid messing global.asax,
Everything works fine, but the Test:
I tried using the WebActivator.ActivationManager.Run() method, but my things weren't loaded.
I've something like this in my test:
[TestMethod]
public void TemplateModelBinderProvider_Should_Be_Registered_In_BinderProviders()
{
WebActivator.ActivationManager.Run();
IModelBinderProvider templateModelBinderProvider = ModelBinderProviders.BinderProviders.
Where(x => x is TemplateModelBinderProvider).
FirstOrDefault();
Assert.IsNotNull(templateModelBinderProvider);
}
And this is my app_Start class:
[assembly: WebActivator.PreApplicationStartMethod(typeof(MVC.App_Start.MVCBindings), "Start")]
namespace MVC.App_Start
{
public static class MVCBindings
{
public static void Start()
{
ModelBinderProviders.BinderProviders.Add(new TemplateModelBinderProvider());
}
}
}
Sorry you have problems with the piece of code I wrote.
I don't have access to the source code right now but will take a look in the evening (UK time).
Do you think you could send me your solution so I could replicate it locally? My email is jkonecki at gmail.com
UPDATE
I have received your source code but unfortunately it contains references to libraries I cannot obtain so I cannot compile it.
I have created a separate solution (emailed to you) with MVC3 web app and unit test projects that uses your custom model binder provide. There are two tests that prove that WebActivatorManager.Run method properly registers a custom provider.
Try debugging your unit test to make sure that Run method calls your static Start method.
WebActivator source code is here - you might want to get it and step through.

Resources