How does Visual Studio decide which port to run applications on? - asp.net

When you run an application from within visual studio, it seems to pick a random port on localhost. For example, running an application right now, I get
http://localhost:3240/ApplicationStuffHere
I realize this port gets loaded into the solution file, via
VWDPort = "3240"
How does this get decided on? it seems fairly random.

By default, ASP.NET Web Application Projects are configured to launch and run using the built-in VS Web Server (aka Cassini) on a random HTTP port on the machine.
This port number can be changed if this port is already in use, or if you want to specifically test and run using a different number:
(source: scottgu.com)

I know this is a little bit old, but I was wondering the same thing. I eventually found the answer for VS2005, but figured I'd share it with the stack overflow community.
Open your project
Click (not right click) on the name of the project in the solution explorer (that top, bolded line)
Hit f4 - this will bring up a properties panel
Change "Use dynamic ports" to False
Optionally change the Port number to whatever port you want to use
And you're done.

This port number is initially assigned randomly, but Visual Studio will try to use that same port number every time the application starts.

Check out the Properties of your web application in solution explorer (Right click > Properties)
The web tab is the one you are looking for.
Kindness,
Dan

Related

Visual Studio 2010 Web Development Port

I've noticed that Visual Studio like to change the port used for my application from time to time, and I was wondering why. After some Stack Overflow reading, I've found solutions to prevent this (it is possible to assign a static port). However, I do not find it to be a problem (merely a curiosity) and do not need to a assign a static port. I just want to know why it does that.
While debugging my application, VS will run on one port. On another run 5 or ten minutes later, it will run on an entirely different port. As far as I know, no other processes on my machine are using these same ports, so why the change? Once it finds an available port, why does it not continue to use it?
This is by design. Either you can have random ports every time, or you can assign a fixed one per project. Keeps things simple I guess (rather than figuring out if the previously used port is taken or not and then based on that, use the old one or a new one)!
As Mrchief said, this is by design. You can opt to use IIS by creating a virtual directory and making the appropriate selection on the Web tab of the project properties.
Then your URL (can debug also) will look like:
http://localhost/myproject
Port 80 will now be the default :)

Cannot run webmatrix site

I have created a site with webmatrix. When I try to run it, it says it cannot because it needs admin right to make the connection to port.
I tried to run Webmatrix with admin right it doesn't change anything. What should I do ?
Your question is a bit hard to understand. But I assume you are trying to connect to the site externally meaning from a different computer than the one running webMatrix. If so, there are a couple of things to get right and this post by Vaidy will help you: Serving external traffic with WebMatrix
First thing is that you need to be sure that the port you want to use not used by any application.
the problem is that many time user want to debug application in VS oR VWD and Webmatix. because they use same port so Webmatix not run because VWD already use them.
well first thing is that try to change the port and run if that's not work then they need to run as administator for doing that:-
select webmatix icon open the propety go to advanced and choose tor run as administration.

Setting up IIS7.5 for local ASP.Net Development

Are there any particular settings one should optimally enable/disable/tweak when doing ASP.Net MVC development on local test machine Windows 7 using IIS 7.5 and moving in and out the debugger & recompiling refrequnetly (integration/troubleshooting stage now before TDD fantactics throw stones - although admittedly I could have more under test), I work with 64 bit edition but figure this probably applicable at both x86/x64?
I'll start with one:
Ping Period (seconds) - increase from 90 to 3000 (or something somewhat higher) so you can if unfortunately need to a good bit of time whilst debugging or disable ping on local test machine.
Credit: http://blogs.msdn.com/johan/archive/2007/09/12/my-web-application-times-out-when-debugging-in-iis7.aspx
However I see over stuff such as:
Disable Overlapped Recycle & Recycling settings etc.. that I wonder if could increase performance or make debugging less friction
Question prompted by the annoyance that I've ran across a few recent debugging issues (not apparent in production) including a random, and tempormental error "An assembly with the same simple name blah-blah-lah-assembly-definiton has already been imported . Try removing one of the references or sign them to enable side-by-side." (iisreset resovles) and generally slow debugging attaching. The points and answers to this question need not help with the above (I believe it may be related to spark view engine as that where the stacktrace ends) but figure it worth mentioning incase someone has a direct suggestion *
quick tip: if you're experiencing slow response times (~1-1.5 sec) from browsers other than internet explorer (eg: firefox, chrome, safari) while running your mvc/ other web app on your local machine using win7/vista, it is due to dns resolution with ipv6.
firefox solution: disable ipv6 in about:config (boolean cfg 'network.dns.disableIPv6')
machine wide soft solution: uncomment the good old localhost address in the hosts file (%WINDIR%\System32\drivers\etc\hosts):
# localhost name resolution is handled within DNS itself.
127.0.0.1 localhost
# ::1 localhost
machine wide hard solution: disable ipv6 completely
credit goes to this blogpost: http://weblogs.asp.net/dwahlin/archive/2007/06/17/fixing-firefox-slowness-with-localhost-on-vista.aspx
Embarcadero guys just published a fresh article on similar topic for Delphi Prism (aka Delphi for .NET), so why not take a look on their suggestions?
http://edn.embarcadero.com/article/40108
From the experience i have working with asp.net mvc, i can tell that there are no special settings for IIS 7 or IIS 7.5 for working on asp.net mvc projects. It works fine in the default form, you just need to create a new website and point it to the folder that has the files for you application.
For debugger if you ask, you can simply put a breakpoint in the code and hit that breakpoint when you run the application from visual studio. But by default the application will use the development web server that fires up when you run a web application from visual studio. If you want to the application to run using the IIS installed on your system you will have to change the project settings. See here for a screen shot of how to do so
http://blogs.bootcampedu.com/blog/post/Debugging-aspnet-mvc-application-using-IIS.aspx
Additionally you can also use System.Diagnostics.Debugger.Break(); for putting a break point in the code.
If you only want to debug your application, I recommend to use the built-in development server of Visual Studio.
If you debugged the most of it or want to do that on IIS, I recommend you the Ctrl+Alt+P shortcut, which enables you to attach a debugger. Select w3wp.exe and you can debug with IIS.

Visual Studio Development Server using wrong port

Related to a previous issue that I thought was resolved and actually isn't...
My Visual Studio 2008 installation may be a bit messed up, I think.
When my ASP.NET project is set up to use VS Dev Server with a fixed port, I get the "Port in use" error described in the linked question.
When my project is set up to use a random (auto-assigned) port number, it works, but it launches the browser using a port number 3 less than the actual Dev Server port number (e.g. if the port number is 1903, the browser launches to http://localhost:1900/)
If I make changes to the project settings, they do not "take" until I save and restart Visual Studio.
Any ideas how to track this one down?
Thanks!
I had a similar problem which hit my 2 main machines at the same time. On investigating I found it to be related to the Eset personal security (guessing a recent update messed something up). To solve it I excluded VS2008 from the active browser filtering - this is in:
setup -> advanced firewall setup -> antivirus & anti spyware -> web access protection -> HTTP -> webbrowsers
Deselecting vsdev in here fixed the problem - interestingly enough disabling the firewall and antivirus / antispyware did not solve the issue, so it is worth looking for a similar setting if you are running different security software
First try to kill all "WebDev.WebServer.exe" processes.
In Solution Explorer, click the name of the application.
In the Properties pane, click the down-arrow beside Use dynamic ports and select False from the dropdown list.This will enable editing of the Port number property.
In the Properties pane, click the text box beside Port number and type in a port number.
Click outside of the Properties pane. This saves the property settings.
Hope this helps
I do absolutely agree with Macros' answer. Just want to share solution for Eset Nod32 v5
In ESET NOD32 v5 to allow Visual Studio to run Development or IIS Express server you must uncheck Visual Studio in Nod32 Advanced Setup => Web and Email => Protocol filtering => Web and email clients
Weird!
The port number is stored in the .sln file. So, I'd blow that away the solution file first, re-create it and see what happens. If that doesn't help, I'd then move onto the web.config file and blow that away and start again too.
I also encountered the same error message:
Unable to launch Visual Studio development server because port [xxxx] is in use.
However, I do not have ESET installed. Instead, I had recently installed GlassFish server on my machine and that was causing the problem. Therefore, in Windows Task Manager, I killed the process it runs under which is java.exe and it fixed the problem.
This also applies to Visual Studio 2010.
And there is more to it.
Symptoms:
A Web (Services) project is configured to run at a specific port, e.g. 10080.
After a while Visual Studio compains “Unable to launch the Visual Studio Development Server because port ’10080′ is in use”
The reason is still unclear. It might have something to do with the webdev server crashing.
Restarting the pc doesn't solve the problem.
Netstat doen’t show an entry for the port 10080
Manually startin WebDev.WebServer40.exe at port 10080 works fine.
Since I'd like to start from within Visual Studio, I moved to port 10081, then to 10082, and today to 10083. I’m running out of ports.
Solutions that did not work:
Restart Visual Studio
Tweaking Trendmicro security settings (couldn't access them)
Disabling Forticlient antivirus/firewall
Workaround that DOES work:
Configuring my project to manually start the server
Right click the project, choose properties
Click the tab "Web"
Pick for start action "Start external program" and point it to Webdev.Webserver40.EXE
(for me: C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\WebDev.WebServer40.EXE)
Command line arguments: /port:10080 /path:C:\Solution\Project
Working directory: C:\Solution\Project
Under servers check "Use Custom Web Server"
Do not check any debugger checkbox
Side effect: my project thinks break points are not getting hit. ("no symbols loaded"). Turns out they work like they should.
I hope anybody ever finds a definitive solution, but up until then this workaround does the trick for me.
To solve your problem, just restart your PC. I've had the same problem, I did the same thing.

Determining the port a Visual Studio Web App runs on

I've been using the macro from this blog entry for attaching the Visual Studio debugger to an already running instance of the Web Application I'm currently working on. However, if I have more than one instance of the Visual Studio web server running it's pot luck which one it'll attach to.
Is there a way to determine what port is configured in the Web project so I can modify the macro to filter its choice of process to that one?
Further Info
I'm aware that you can set the port number to a static one - I've done just that from the get-go, what I'm trying to determine is how to programatically determine the defined port-number so I can modify the macro (in the linked blog entry) and ensure it connects to the right instance of the Visual Studio web server.
The way I have things running is I have two (or more) instances of Visual Studio running, each of which contains a Solution, which contains a web project and one or more other projects - typically an Installer project), so when I trigger the macro from a given instance of Visual Studio, I want to find the Web Project within that instances loaded solution and determine the port it's running against.
This code will get you a list of all ports for the projects in the current solution:
Sub GetWebProjectPorts()
Dim ports As String
For Each prj As Project In DTE.Solution.Projects
For Each p As EnvDTE.Property In prj.Properties
If p.Name.Contains("DevelopmentServerPort") Then
If Not String.IsNullOrEmpty(ports) Then
ports += ","
End If
ports += p.Value.ToString()
End If
Next
Next
MsgBox(ports)
End Sub
If that can help you, I have found a link that might actually to the trick.
However, it require DllImport call and a lot of fun.
You can take a look at that article there: http://bytes.com/forum/thread574901.html
Quotes from the actual site:
By calling into iphlpapi.dll using
PInvoke interop. Google around for
GetExtendedTcpTable and iphlpapi.dll,
I'm sure you will find some existing
stuff.
Willy.
And one last:
Here are some API-Methods for my
purposes:
GetTcpTable()
AllocateAndGetTcpExTableFromStack()
GetExtendedTcpTable()
I wrote a small programm that is able
to show me all Processes with the
belonging TCP-Ports (with
AllocateAndGetTcpExTableFromStack())
running under Windows XP.
Now my problem is, that under Windows
2000 I can't use
AllocateAndGetTcpExTableFromStack() or
GetExtendedTcpTable() so I'm only able
to use GetTcpTable() to list all
TCP-Ports but without the belonging
processes.
Did somebody have the same problem or
is there another way (.Net or WMI
etc.) to solve my problem?
Thanks in advance,
Werner
If you go to the properties for your web project and look under the "Web" tab, you can specify which port the project will always start up on. Then you can click "Enable Edit and Continue" so you don't have to stop debugging and restart continuously.

Resources