wcfiis-6network-programmingwcf-binding

404 BadRequest exposing WCF service through external IP using IIS host headers


We host a WCF webservice on Windows Server 2003. This server only has 2 internal IP's. We want to expose the service externally. This is done through the firewall that maps an external IP to the service.

So, I would need to modify the service to display that external IP for the internal links. This is not an issue since it should only be used externally.

Changing the Host Header value in IIS gives a 'Bad Request (Invalid Hostname)' response from IIS. I also added an 'address' value to the endpoint entry in the web.config ... but it sill just points to the internal machine name. Any ideas?

edit: I can verify that IIS7 has the exact same behaviour. Address didn't work. Different hostname gave Invalid Hostname error. Is there seriously no way to present a different (fictive) IP? :/

edit2:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicAnonymous">
                <security mode="None"/>
            </binding>
        </basicHttpBinding>
    </bindings>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Extended">
                <serviceMetadata httpGetEnabled="true"/>
                <dataContractSerializer maxItemsInObjectGraph="2147483646"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <diagnostics>
        <messageLogging logEntireMessage="true" logMalformedMessages="false" logMessagesAtServiceLevel="false" logMessagesAtTransportLevel="true" maxMessagesToLog="3000"/>
    </diagnostics>
    <services>
        <service behaviorConfiguration="Extended" name="AnCWCFWebService.ProductInfoProvider">
            <endpoint address="" binding="basicHttpBinding" name="ASMX" bindingConfiguration="BasicAnonymous" contract="AnCWCFWebService.Interfaces.IProductInfoProvider"/>
        </service>
    </services>
</system.serviceModel>

Solution

  • 404 BadRequest Due to IIS Configuration...

    If you are receiving a 404 BadRequest error from IIS after attempting to modify host headers in IIS, this is common, but there is a fix!

    Fix By Making IIS Configuration Changes

    How can WCF support multiple IIS Binding specified per site?

    Fix By Making WCF Code Modifications

    Alternatively, the following article explains how one developer solved this issue with a combination of configuration and code:

    Well, that was fun! An adventure in WCF, SSL, and Host Headers
    http://geekswithblogs.net/rakker/archive/2008/07/03/123562.aspx

    The article references two important links...

    The first one explains how to properly set the host headers in IIS:

    After getting host headers working, you'll find that...

    "you can't have more than one host header for IIS or wcf will break"

    The solution to working around this limitation is found within this article, and there is also a comment on the GeeksWithBlogs.net article above that provides an enhanced variation:

    If you are still experiencing trouble, let us know in the comments below...


    If the Service is Not Working...

    My experience with WCF is that it is very tricky at times, especially with configuration. If one is following best practices and not doing anything non-standard, the great majority of deployment problems are the result of a botched configuration file.

    In theory (not so much in practice, due to architectural differences), setting up a WCF service on IIS should be no different than setting up a typical virtual directory and corresponding application for web application or for an ASMX web service.

    Therefore, I recommend that if this is the first WCF service you are exposing to the Internet, follow the same simple approach you would take when exposing your first website. Basically, create a new sample "WCF Service Application" (this is available in the Add New Project dialog, under the Web section of C# or VB).

    Once you have it working, follow your deployment practices to move it into a production sandbox and test it locally. This sandbox would preferably already have some web sites or web services installed and known to be accessible from the Internet, in order to eliminate any doubt about the typical network configuration issues. If you have a sample ASMX web service that is already successfully exposed on the Internet from that server, that would be best.

    Next, try testing the the ASMX and the WCF services from web browser, both locally on the server, internally on other desktops and then finally externally.

    Testing URLs

    We want to test accessing the standard SVC and ASMX file from the web browser in all the varieties of URL flavors that are available and relevant. The results should be similar, with summary page about he service rendering in the window. The difference will be that the ASMX web service's summary will likely allow you to execute the web methods on the service if that feature has not been disabled in the web.config file.

    Compare the results of browser fetches of the following styles of URLs...

    All of these tests should return similar results.

    Testing Service Methods

    If you feel like it, go head and test some web methods on the ASMX web service for any of the tested URLs, from the web browser. You'll soon see that we can test ASMX web services a different way also...

    Next we'll test web methods on both the WCF service and the ASMX web service by using the WcfTestClient.exe application that is found in the Visual Studio 2008 distribution (C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE).

    You will need to add the services through the File->Add Service menu item, typing in the URL above for each service URL that you wish to test. Make sure to include the filename of the SVC and ASMX files. If all is well, the MEX endpoint that is enabled by the "httpGetEnabled" attribute of the <serviceMetadata/> element in the web.config file will return the data that is necessary for the utility to operate, thereby populating the tree with the inventory of our service methods like this:

    https://i.sstatic.net/mNK8l.jpg

    From this point, it will be useful to refer to the following to resources:

    Conclusion

    If you make it this far, then I do not expect any other issues and you should now attempt to compare the setup of the samples to the WCF service that you are attempting to publish to the Internet, and hopefully the differences will be obvious.

    Remember to treat the WCF service like an ASMX web service during you diagnostics, assuming that the web.config is known to be set up correctly.

    If you are still unable to make things work, check this guide for further technical advice:

    Finally, if all else fails, just wrap your WCF service in an ASMX web service: