35

Using Visual Studio 2017, I just created a simple API project as shown below. And in the Startup.cs file I have this code.

public void ConfigureServices(IServiceCollection services) {

    services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

}

Can someone please throw some light as to what is means? Do we need to keep this code?

I think MS should put some comments to indicate what such code does.

SetCompatibilityVersion

3
  • 6
    I've you see this post learn.microsoft.com/en-us/aspnet/core/mvc/… "By opting in, you get the latest behavior, and the long-term behavior of ASP.NET Core" otherwise The SetCompatibilityVersion method allows an app to opt-in or opt-out of potentially breaking behavior changes introduced in ASP.NET Core MVC 2.1 or later. Commented Jan 15, 2019 at 6:46
  • ....therefore, the above code sets the compatibility mode to ASP.NET Core 2.2. Commented Jan 15, 2019 at 6:50
  • 1
    What do you guys mean by compatibility mode and optin optout Commented Jan 15, 2019 at 7:21

3 Answers 3

28

When you call the AddMvc method, several components are registered with certain options. You call one method and the whole mvc framework is wired up.

However, if the mvc team in the future decides to change a default value, or decides that a component is no longer to be registered by default, or changes an expected side effect of this method, the user code relying on that would break. To avoid such breakage, you can call the set compatibility method which the mvc team will use to preserve the behavior provided to you.

Suppose they introduce a new feature, which exists only when you are targeting the 2.3 platform: if your code declares that it targets the 2.2 api, the mvc team will know that you are not using that feature because it was not existing at that time. This way the can make safe assumptions about what should be provided and how.

For further details, please look at MSDN.

Sign up to request clarification or add additional context in comments.

2 Comments

It's a pity there is no clear description what will break (which is the most important for developers) when changing Version_2_1 <-> Version_2_2
do you know, if I can remove this altogether from .Net 8
13

FYI, SetCompatibilityVersion is a no-op for .Net Core 3 or later and deprecated in .Net 6.

https://learn.microsoft.com/en-us/aspnet/core/mvc/compatibility-version?view=aspnetcore-5.0

2 Comments

I see it says "no-op for .NET Core 3" in the doc, but it does not say "or later". Can anyone clarify on this?
So far, so good. It is still no-op in .Net 7. learn.microsoft.com/en-us/aspnet/core/mvc/…
10

Yennefer's answer is great, i'll just add some things.

  • For ASP.NET Core 2.X it might very well be needed to use SetCompatibilityVersion, for the reasons Yennefer described.

  • For ASP.NET Core 3.0 it's a no-op, meaning it does absolutely nothing. Read more here.

  • For ASP.NET Core 3.X (but not 3.0) and forward, it might have a value. Read more here

5 Comments

so for the Core 5 and above should it be used at all? Or we can remove this?
It seems to me here that microsoft hasn't updated their official docs @Eru so I don't know. But if you find something please provide a link here and I can update the answer
Sure, but for now I have just removed that part of setup and all looks as working with no problem. But I guess that any possible break/error would happen during certain runtime scenarios; usage of old nugets etc.
@Eru did you find any issues after removing this all together?
@PrashantAgrawal as for now I did not run into any issues. Here is a link: learn.microsoft.com/en-us/aspnet/core/mvc/… maybe this will add some value - for me it did not, but still worth to check out :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.