Default Port Number for a particular "Scheme"?

  • Thread starter Thread starter Kevin Frey
  • Start date Start date
K

Kevin Frey

Is there a .NET class where I can do something like this:

int defaultPort = SomeDotNetClass.GetDefaultPort( Uri.UriSchemeHttps );

The Uri class is able to advise default port numbers for various schemes
(but does not seem extensible) but I don't know whether they have been
hard-coded or whether they have been pulled from somewhere else, and I would
dearly like to avoid hard-coding 80, 443 etc in my application.

Thanks

Kevin
 
Kevin said:
Is there a .NET class where I can do something like this:

int defaultPort = SomeDotNetClass.GetDefaultPort( Uri.UriSchemeHttps );
No. However, the following will approximate it:

int defaultPort = new Uri(Uri.UriSchemeHttps + "://dummy").Port;

Although this approach produces invalid URIs (for example, "mailto://dummy"
isn't a valid URI as the mailto: scheme doesn't allow hierarchies and the
"/" characters would need to be escaped to be part of a mailbox name), all
the built-in parsers will accept this format as they all support hierarchies.
The Uri class is able to advise default port numbers for various schemes
(but does not seem extensible)

You can derive a new UriParser and use UriParser.Register() to associate a
default port with a new scheme. There is no way to simply associate a scheme
with a port number without providing parsing capabilities, but there doesn't
seem to be much use for that either. You could easily write your own class
for this, though.
but I don't know whether they have been hard-coded or whether they have
been pulled from somewhere else, and I would dearly like to avoid
hard-coding 80, 443 etc in my application.
I wouldn't fret over this. The default ports are codified in RFCs. If you
compare the effort required to make them changeable to the probability that
they'll change within your application's lifetime it's obvious that it
doesn't matter if you hard-code them.

The solution I presented above is kludgy in that it might break down if you
introduce custom parsers that don't allow hierarchies at all, so it has
little advantage over simply putting in a

const int HttpDefaultPort = 80;

where needed. In this particular case, it's more important that people know
what the magic number 80 means than that it's easy to change. The least
redundant solution that's still guaranteed not to break is to write your own
specific class to associate schemes with port numbers, but this will be
divorced from the framework's own mapping.
 
Kevin said:
Is there a .NET class where I can do something like this:

int defaultPort = SomeDotNetClass.GetDefaultPort( Uri.UriSchemeHttps );
No. However, the following will approximate it:

int defaultPort = new Uri(Uri.UriSchemeHttps + "://dummy").Port;

Although this approach produces invalid URIs (for example, "mailto://dummy"
isn't a valid URI as the mailto: scheme doesn't allow hierarchies and the
"/" characters would need to be escaped to be part of a mailbox name), all
the built-in parsers will accept this format as they all support hierarchies.
The Uri class is able to advise default port numbers for various schemes
(but does not seem extensible)

You can derive a new UriParser and use UriParser.Register() to associate a
default port with a new scheme. There is no way to simply associate a scheme
with a port number without providing parsing capabilities, but there doesn't
seem to be much use for that either. You could easily write your own class
for this, though.
but I don't know whether they have been hard-coded or whether they have
been pulled from somewhere else, and I would dearly like to avoid
hard-coding 80, 443 etc in my application.
I wouldn't fret over this. The default ports are codified in RFCs. If you
compare the effort required to make them changeable to the probability that
they'll change within your application's lifetime it's obvious that it
doesn't matter if you hard-code them.

The solution I presented above is kludgy in that it might break down if you
introduce custom parsers that don't allow hierarchies at all, so it has
little advantage over simply putting in a

const int HttpDefaultPort = 80;

where needed. In this particular case, it's more important that people know
what the magic number 80 means than that it's easy to change. The least
redundant solution that's still guaranteed not to break is to write your own
specific class to associate schemes with port numbers, but this will be
divorced from the framework's own mapping.
 
Thanks. I did consider the fact that the port numbers were codified in RFCs
etc but it was still a question of elegance over whether I could avoid
defining them myself. Given there appears only a kludgey way of getting the
port numbers (in the sense that it appears less elegant than simply defining
the port numbers as constants), I'll just go with the constant idea for now.

Kevin.
 
Thanks. I did consider the fact that the port numbers were codified in RFCs
etc but it was still a question of elegance over whether I could avoid
defining them myself. Given there appears only a kludgey way of getting the
port numbers (in the sense that it appears less elegant than simply defining
the port numbers as constants), I'll just go with the constant idea for now.

Kevin.
 
Back
Top