8

I am migrating a console app (REST client app) from .NET framework to .NET Core.

In my current (framework) version, I use the app.config file to set the System.Net configuration:

<system.net>
    <connectionManagement>
      <add address="*" maxconnection="65535"/>
    </connectionManagement>
</system.net>

In .NET Core, I have to use a JSON file for configuration. There is no documentation for implementing these settings using the new configuration schema. Does anyone know how this might look inside the new JSON config, or the correct way to implement this in Core? Do I need to build a designated "System.Net.json" config file (separate from an AppSettings.json) specifically to do this?

Thanks.

mholberger
  • 337
  • 3
  • 14

3 Answers3

12

I assume you're trying to avoid the limit of 2 connections per endpoint, which is default on .NET Framework. Such limit does not exist on .NET Core. So you don't need the above setting at all.

Note that to achieve better perf, we recommend to use HttpClient/HttpClientHandler over HttpWebRequest/ServicePoint on .NET Core. HttpWebRequest/ServicePoint APIs are compat-only.

If you want to limit HttpClient connections, then use HttpClientHandler.MaxConnectionsPerServer

Karel Zikmund
  • 209
  • 2
  • 7
  • I'm so happy to hear this, I once lost DAYS to that infuriating default setting that is hard to diagnose and Google-Fu the answer to. – Quibblesome Mar 11 '19 at 15:26
1

Assuming you are using Kestrel as your web server (and not doing it through IIS implementation), you should be able to set this in your UseKestrel in your BuildWebHost.

It would go something like this:

.UseKestrel(options =>
{
    options.Limits.MaxConcurrentConnections = 100;
})

You can also add this in your HttpClientHandler, It's called MaxConnectionsPerServer. It can be seen here.

Scott Craig
  • 275
  • 2
  • 10
  • My application is mainly a REST client. Im not really sure about BuildWebHost. (edited my OP to mention that) – mholberger Oct 04 '17 at 19:12
  • You still have to use a web server to host your requests. You either have to specify it's using Kestrel or another server (like IIS integration). If you used a template to build it (either CLI or Visual Studio), you should be able to see it in your Program.cs. – Scott Craig Oct 04 '17 at 19:16
  • This is not the case. I am not sure where the server that fires my http requests from (within system.net.http), but its not implemented inside my Program.cs. Im using Visual Studio. – mholberger Oct 04 '17 at 19:19
  • Can I set this option on my HttpClient object somehow? – mholberger Oct 04 '17 at 19:32
  • You would need to know what server fires your requests because that is where this needs to be set. The web.config in non-core versions of .net was used because it was a set of instructions to IIS for that particular site. – Scott Craig Oct 04 '17 at 19:34
  • 2
    You can add it in your HttpClientHandler, yes. It's called MaxConnectionsPerServer. [link](https://github.com/dotnet/corefx/blob/master/src/System.Net.Http/ref/System.Net.Http.cs#L83) – Scott Craig Oct 04 '17 at 19:36
  • Or could I possibley just set the configuration on Sytem.Net using a JSON config file? Is it really that much more complicated in .NET Core? – mholberger Oct 04 '17 at 19:36
  • It is more complicated in .NET core because you are developing on a cross-platform language. Whereas web.config was t he default before because of IIS, there is no default anymore because IIS isn't required. However, as I stated above, if you are using HttpClient, you can add it in there too. – Scott Craig Oct 04 '17 at 19:39
  • I honestly cant find the MaxConnectionsPerServer property in my HttpClientHandler object. Only the ones listed here: https://blogs.msdn.microsoft.com/henrikn/2012/08/07/httpclient-httpclienthandler-and-webrequesthandler-explained/ – mholberger Oct 04 '17 at 19:48
  • What version of .Net Core are you using? – Scott Craig Oct 04 '17 at 19:52
  • My bad. I was looking for it inside my 4.6 Framework project. It DOES EXIST in the Core project. I will set this on my handler, and pass it to my client object! Thank you. – mholberger Oct 04 '17 at 19:58
  • Awesome! I'll edit my answer to clarify for your situation. – Scott Craig Oct 04 '17 at 19:58
0

Some addition to Karel Zikmund answer. (As i don’t have permissions to comment).

According to this doc connections are limited since .net core 2.0: https://learn.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.defaultconnectionlimit?view=netcore-3.1

What is missed in doc is if ServicePointManager used for .net core HttpClient implementation. According to this info it is used in .net core, but for HttpWebRequest, not HttpClient: https://github.com/dotnet/runtime/issues/26048

Slava Zak
  • 1
  • 3