Saturday, March 29, 2008

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

Description:
When you try to access a WCF service hosted in IIS, you get the following error:

This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.

Context:
You have a website hosted in IIS and the site has multiple host headers defined for the site (e.g. www.domain.com, domain.com).

Fix:
There doesn’t appear to be a nice workable solution for this as of VS 2008 initial release on .NET 3.5. The only “hack” found out there is to define a new Factory for the service.

Step 1:

namespace Foo
{
public class CustomHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(
Type serviceType,
Uri[] baseAddresses)
{
// Specify the exact URL of your web service from the config file:
// e.g. http://www.domain.com/service/myservice.svc
Uri webServiceAddress =
new Uri(ConfigurationManager.AppSettings["ServiceUri"]);

ServiceHost webServiceHost =
new ServiceHost(serviceType, webServiceAddress);

return webServiceHost;
}
}
}

Some web articles had you also creating a new host derived from ServiceHost, but that is not needed.

Step 2:
Modify the .svc file on the site hosting the service (not the .svc in Visual Studio):

<%@ ServiceHost Service="Foo.Service1" Factory="Foo.CustomHostFactory" %>

The above .svc content may also include the language and debug specifiers.

Can't add web reference in VS 2008 due to computer name being used for schemaLocation

Description:
You can’t add a web reference using Visual Studio 2008 for a WCF service since the WSDL is using the computer name rather than the service address. If you develop using "localhost" as the service name this is NOT an issue.

Context:
You have a computer/website with more than one IP address and are developing a WCF web service on one of the extra IP addresses (Site A).

Issue:
When you use Visual Studio 2008 to add a reference to a web service on Site A, you will get an error something like:

The document at the url http://192.168.0.54:8000/abcService.svc was not
recognized as a known document type.
The error message from each known type may help you fix the problem:
- Report from 'WSDL Document' is 'The document format is not recognized (the
content type is 'text/html; charset=utf-8').'.
- Report from 'DISCO Document' is 'There was an error downloading
'http://192.168.0.54:8000/abcService.svc?disco'.'.
- The request failed with HTTP status 404: Not Found.
- Report from 'XML Schema' is 'The document format is not recognized (the
content type is 'text/html; charset=utf-8').'.

If you look manually enter the service url in a browser for the service at the IP address (e.g. 192.168.0.54), you will see in the XML WSDL that it is trying to import schemas (schemaLocation) using the computer name, not the service address (192.168.0.54).

Fix:
I fixed this by just making sure I entered the IP address as the host header in IIS. Then the IP address was used rather than the computer name for all WSDL url references.

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...