3

I am hosting WCF services in IIS. I have multiple hostname bindings set up in IIS for the site. However, when making requests to any of the non-default bindings, the correct url doesn't get reported by the OperationContext.IncomingMessageProperties.Via property. The URL that gets reported uses the default binding's hostname as the base, with same path and querystring.

For example, assuming the following bindings:

http://subfoo.services.myapp.com (first/default entry)
http://subbar.services.myapp.com

When making a request to: http://subbar.services.myapp.com/someservice?id=123

The Via property reports the request URI as: http://subfoo.services.myapp.com/someservice?id=123

How can I get the URL with the actual hostname that was requested?

Daniel Schaffer
  • 56,753
  • 31
  • 116
  • 165
  • Can you post your config - obviously after anonymising? It is not clear what you mean by default URI or non-default bindings. – Aliostad Mar 09 '11 at 23:46
  • I'm referring to the host header bindings in IIS. It seems to treat the first entry as the default hostname. – Daniel Schaffer Mar 10 '11 at 04:10

1 Answers1

6

it is possible, just a bit involved. You need to get the HTTP Host Header, and replace the host segment of the IncomingMessageProperties.Via Uri. Here's some sample code with comments:

OperationContext operationContext = OperationContext.Current;
HttpRequestMessageProperty httpRequest = operationContext.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
if (httpRequest != null)
{
    // Get the OperationContext request Uri:
    Uri viaUri = operationContext.IncomingMessageProperties.Via;
    // Get the HTTP Host Header value:
    string host = httpRequest.Headers[System.Net.HttpRequestHeader.Host];
    // Build a new Uri replacing the host component of the Via Uri:
    var uriBuilder = new UriBuilder(viaUri) { Host = host };

    // This is the Uri which was requested:
    string originalRequestUri = uriBuilder.Uri.AbsoluteUri;
}

HTH :)

mthierba
  • 5,587
  • 1
  • 27
  • 29