A lot of times, specially when using web services, we want the dev server to use a specific port. Suppose you have a web service and a website which consumes it. You put them in two separate projects. You’d want the web service to always use the same port. One option is to set it up in IIS. But what if you don’t want to use IIS? What if your putting together a simple test and wish to use the dev server? How do we do that?
Well, the process varies between websites and web application projects.
Web Application Projects
Just right click the project in solution explorer and select properties. In the web tab, you’ll get the option to set the port.
Websites
The procedure is slightly more complex for websites. First, in solution explorer, click the project. Hit F4. This brings up the properties pane. Note, this is NOT the property pages found by right-click>>Property Pages.
Set the “Use dynamic ports” option to False. At this point, you can’t assign the Port number. Run the web server once, by making any request to the project (a simple ctrl+F5 should work). Doing this will allow you to change the Port number.
Note that Port number is no longer grayed out. You can now assign the port number to be used. As an alternative to making one request, you can also set the “Use dynamic ports” to false, close the solution and open it up again.
I was recently working on a project that used WCF web services extensively. Everything was ready for deployment, I deployed to the remote host, and voila – nothing works (nothing WCF related). After a LOT of frustration, I managed to make it work. I found two areas which were causing the problem:
1. The .svc extension was NOT setup right at our host. You can set this in the web.config webhandlers section for IIS 7, but not for IIS 6.0. I had IIS 7, but decided to bug my host to set up the extension on the server ;)
2. I had multiple host headers; i.e. my site worked for both http://mysite.com and http://www.mysite.com. To resolve this issue, you can either disable all but one if your host permits. Otherwise, you need to use a factory. Here’s an example of such a factory:
|
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ServiceModel; using System.ServiceModel.Activation;
namespace Shipping.WebService { public class MultipleIISBindingSupportServiceHostFactory : ServiceHostFactory {
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses) {
Uri[] requiredAddress = GetAppropriateBase(baseAddresses);
return base.CreateServiceHost(serviceType, requiredAddress);
}
Uri[] GetAppropriateBase(Uri[] baseAddresses) { List<Uri> retAddress = new List<Uri>(); retAddress.Add(baseAddresses[0]);
return retAddress.ToArray();
}
}
} |
Granted, it can be way more efficient and you can do nifty tricks with the url to support both http:// and http://www, but I can happy to have just one for my webservice.
Next, you need to set the factory in the markup of the ,svc file. You can do it like this:
<%@ ServiceHost Language="C#" Debug="false" Factory="Shipping.WebService.MultipleIISBindingSupportServiceHostFactory" Service="Shipping.WebService.ClientService" CodeBehind="ClientService.svc.cs" %>
Hope that helps.