Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - Default Port Number for a particular "Scheme"?

Reply
 
Old 05-15-2009   #1 (permalink)
Kevin Frey


 
 

Default Port Number for a particular "Scheme"?

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



My System SpecsSystem Spec
Old 05-15-2009   #2 (permalink)
Jeroen Mostert


 
 

Re: Default Port Number for a particular "Scheme"?

Kevin Frey wrote:
Quote:

> 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.
Quote:

> 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.
Quote:

> 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.

--
J.
My System SpecsSystem Spec
Old 05-17-2009   #3 (permalink)
Kevin Frey


 
 

Re: Default Port Number for a particular "Scheme"?

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.


"Jeroen Mostert" <jmostert@xxxxxx> wrote in message
news:4a0d4e6c$0$187$e4fe514c@xxxxxx
Quote:

> Kevin Frey wrote:
Quote:

>> 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.
>
Quote:

>> 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.
>
Quote:

>> 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.
>
> --
> J.

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Number of "messages" not relating to actual number Live Mail
Set the "No Sounds" scheme for the default user profile Vista installation & setup
NOW WHAT? "The maximum number of secrets that may be stored in a single system has been exceeded" Vista security
FTP Server - "FTP Port is used" error message" .NET General
"the color scheme has been changed to windows vista basic" Vista General


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46