Wednesday, October 1, 2008

IIS hosted WCF service base address

What is the base address of WCF service hosted on IIS? Certainly not the base address you configure in your web.config. It's what the IIS considers to be a base address, a computer name by default. This causes some problems. For example this is a fragment of WSDL the service generated:

<xsd:schema targetNamespace="http://tempuri.org/Imports">
    <xsd:import schemaLocation="http://myhost:8432/MyService/Service.svc?xsd=xsd0" 
                namespace="http://tempuri.org/" />
    <xsd:import schemaLocation="http://myhost:8432/MyService/Service.svc?xsd=xsd1" 
                namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
    <xsd:import schemaLocation="http://myhost:8432/MyService/Service.svc?xsd=xsd2" />
    <xsd:import schemaLocation="http://myhost:8432/MyService/Service.svc?xsd=xsd3" 
                namespace="http://schemas.datacontract.org/2004/07/MyService.Model" />
    <xsd:import schemaLocation="http://myhost:8432/MyService/Service.svc?xsd=xsd4" 
                namespace="http://schemas.datacontract.org/2004/07/System" />
</xsd:schema>

As you can see there are references to myhost server, but that name is only visible in local network. Let's assume, that from internet, the server is available at myhost.example.com . When a service client tries to download service's WSDL (presumably to generate a proxy), it is redirected to myhost and the process fails. We need to change IIS configuration and set the base address explicitly. I'll show how to do this in IIS 6.0: go to your website's properties, then click "Advanced..." button on the "Web Site" tab. You should see something like this:

image

As you can see, the "Host header value" column is empty next to HTTP port configuration. Click "Edit...", and enter desired base address:

image

Close configuration windows by clicking OK and restart your application's Application Pool. After this, the WSDL should contain desired address:

<xsd:schema targetNamespace="http://tempuri.org/Imports">
    <xsd:import schemaLocation="http://myhost.example.com:8432/MyService/Service.svc?xsd=xsd0" 
                namespace="http://tempuri.org/" />
    <xsd:import schemaLocation="http://myhost.example.com:8432/MyService/Service.svc?xsd=xsd1" 
                namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
    <xsd:import schemaLocation="http://myhost.example.com:8432/MyService/Service.svc?xsd=xsd2" />
    <xsd:import schemaLocation="http://myhost.example.com:8432/MyService/Service.svc?xsd=xsd3" 
                namespace="http://schemas.datacontract.org/2004/07/MyService.Model" />
    <xsd:import schemaLocation="http://myhost.example.com:8432/MyService/Service.svc?xsd=xsd4" 
                namespace="http://schemas.datacontract.org/2004/07/System" />
</xsd:schema>

2 comments:

Alex S said...
This comment has been removed by a blog administrator.
Marcin Budny said...

Positioning sites by comments on my blog will not be tolerated

Share