Dealing with unwanted port numbers on localhost when debugging in Visual Studio
So when I'm trying to debug a project in Visual Studio, I'll often have 1 or more services that I need to call. These services are separate projects and I'll launch them in separate instances of VS. ASP dev server kindly launches them with temporary port numbers attached. This would be fine except for my programs are looking for an IP without a port attached. (Typically it's as simple as "localhost"!) I've coped by using web config settings and changing the xml to use the port number while debugging. This still doesn't always work and occasionally I'll still hav开发者_开发问答e to fudge it and insert port numbers as strings in code! UGH! Then I have to remember to undo all the port herding code before deployment...
I want to stop doing this:
string _strBaseURL = String.Format("http://{0}:2277", ConfigurationManager.AppSettings["myservice_ip"].ToString());
What is the best practice here? Am I doomed to port herding while debugging VS apps?
I solved this by developing against IIS rather than Visual Studio's Development Server. Visual Studio deploys all my projects to IIS which is on port 80, so you have similar environment to deployment. During debug, VS attachs to IIS. I also have to do this to debug HTTPS services.
You can change it in the Web tab of the Project properties, but it requires you to have IIS and some support packages installed.
I don't get the problem. How else do you propose for the client to guess the port number that the server is using? Maybe you can create a DNS style "port-number lookup server". But then the client wouldn't know what port to use for that server either.
Perhaps you can create a .config file for both the server and client and get somebody to make sure they contain the exact same number. Which would be a one-time install hassle, they'll never change the number again.
You can specify which port each project will run on in the project configuration. Can you include the port number in your local configuration? Appears your already doing that with IP Address/URL.
I usually solve this by generating the URI with UriBuilder and including the port number, using app.config as needed to target the right port + URI. But that requires the client code to be under your control, so your mileage may vary. (Note that if the real port is 80, the string generated by removes the :80, so things look prettier in a real deployment).
I frequently have to simulate multiple web projects being deployed but debug them against a local machine; there are occasionally other complications, like cross domain auth, but this solves most of them for me.
精彩评论