Skip to content

Instantly share code, notes, and snippets.

@rynowak
Last active March 28, 2019 23:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rynowak/921e1f9f8a0d6778e9a1006cc0b6c59f to your computer and use it in GitHub Desktop.
Save rynowak/921e1f9f8a0d6778e9a1006cc0b6c59f to your computer and use it in GitHub Desktop.

Summary

ASP.NET Core startup code follows regular patterns that are suitable for code analysis and enhancements to the coding experience through analyzers, code fixes, and smarter scaffolding.

What things that analyzers could provide would be most valuable?

Things that analyzers can easily understand:

  • What services are registered? (ex: AddMvc() includes AddRouting())
  • What options are set? (ex: AddMvcOptions(options => options.EnableEndpointRouting = false)
  • What middleware do you have? (ex: UseHealthChecks())
  • What order are middleware in? (ex: is UseStaticFiles() before routing?)
  • What routes/endpoints are registered? (ex: MapHealthChecks())

Examples

Here are some examples of the kinds of things we can do.

I did actually implement these - they aren't canned demos - it's prototype status. They don't yet have code fixes, but you could image that as well.

Find Missing AddXyz() Calls

Have you ever forgotten to register something in DI?

image

This is generic functionality and can be driven from attributes or a text file shipped with the analyzer/SDK. If based on attributes, it can be extensible.

Recommended Middleware Ordering

Middleware try to be flexible, but there are some cases that are always a mistake.

image

This is generic functionality and can be driven from attributes or a text file shipped with the analyzer/SDK. If based on attributes, it can be extensible.

Migration Guidance / Fixes

What happens when things change?

image

This is an example of something highly tailored. If we deliver migration guidance and code fixes we recommend the more modern way to using the framework without the need for breaking compatibility and breaking APIs. This is a softer/nicer way to evolve ASP.NET Core.

Feedback?

Leave a comment here, open an issue at https://github.com/aspnet/AspNetCore or tweet @aVerySpicyBoi. The POWER IS YOURS!

Things I've heard so far that I like:

  • Validating DI config at build-time is valuable, anything we can do here would help.
  • Provide the same level of support for generic-host or the Program.cs equivalents of Startup code.
  • Analyze all of my code! Not just startup.
  • The other way around would be nice: “Authentication was added using AddAuthentication but no authentication middleware is invoked”.
  • Calling app inside a lamda declared for another app call is probably a bug: app.MyBranchHelper(a=>app.BadCall());
@jarrettv
Copy link

Some detection around if number of requests stays roughly the same but there is a sudden spike in cpu usage.

@AnthonyMastrean
Copy link

@jarrettv these are static code analyzers, not operational/runtime.

@Yves57
Copy link

Yves57 commented Mar 25, 2019

Maybe out of context, but something that find [FromBody] and other cases where these attributes are obsolete with the new [ApiController] attribute? Probably even more tricky, be able detect the ModelState.IsValid pattern. Of course, it is necessary to deal with the SuppressModelStateInvalidFilter and SuppressInferBindingSourcesForParameters (disable the warning if the tokens are found somewhere in the assembly?).
Otherwise it should be possible to log some information at runtime, but it is not a static analysis.

@rynowak
Copy link
Author

rynowak commented Mar 27, 2019

@Yves57 we already have this. Will be shipping it in the box in 3.0

@Yves57
Copy link

Yves57 commented Mar 27, 2019

@rynowak Great! I have no specific idea about Startup: I don't write new projects evey day, so in my opinion I don't expect Microsoft spends too much time too create tools to help users :-).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment