WCF and Multiple IIS Site Bindings
December 1, 2008We ran into an issue last week when we were deploying a WCF service on an IIS web site which had multiple IIS bindings. It manifested itself by throwing the following exception:
This collection already contains an address with scheme http. There can be at most one address per scheme in this collection.
Turns out that multiple IIS site bindings results in multiple base addresses while WCF (.NET 3.0) only supports a single base address in this scenario. Fortunately there are a couple of solutions to bypass this annoying shortcoming. First, you can provide a filter for the base addresses by providing a custom ServiceHostFactory:
public class InvoiceServiceHostFactory
: ServiceHostFactory
{
protected override ServiceHost CreateServiceHost
(Type serviceType, Uri[] baseAddresses)
{
Uri preferredHostBaseAddress =
Settings.PreferredHostBaseAddress;
return base.CreateServiceHost(serviceType,
preferredHostBaseAddress);
}
}
This custom ServiceHostFactory can now be used by specifying it in the .svc file like so:
<%@ ServiceHost Language=”C#”
Debug=”true”
Service=”InvoiceService.InvoiceService”
Factory=”InvoiceService.Wcf.InvoiceServiceHostFactory“
CodeBehind=”InvoiceService.svc.cs” %%>
This has the major disadvantage that requests can only be received by a single base address, which we retrieve from the configuration file in this case. To solve this issue, we can tweak our custom ServiceHostFactory like so:
public class InvoiceServiceHostFactory
: ServiceHostFactory
{
protected override ServiceHost CreateServiceHost
(Type serviceType, Uri[] baseAddresses)
{
return base.CreateServiceHost(serviceType,
new Uri[0]);
}
}
Now we have to provide a base address for each WCF binding in the configuration file:
<service name="InvoiceService.InvoiceService">
<endpoint
address="http://huppeldepup.be/InvoiceService/InvoiceService.svc"
binding="wsHttpBinding"
contract="InvoiceService.IInvoiceService" />
<endpoint
address="http://huppeldepup.com/InvoiceService/InvoiceService.svc"
binding="wsHttpBinding"
contract="InvoiceService.IInvoiceService" />
</service>
This way, we can start receiving requests through multiple bindings, which is what we wanted in the first place. This approach has one disadvantage though. When a corresponding IIS binding ever changes, we also have to remember to check every configuration of every WCF service that lives inside the respective IIS web site. This is error prone and involves friction.
If you are using .NET 3.5 however, then this is your lucky day. WCF that comes with .NET 3.5 now supports BaseAdressPrefixFilters.
<serviceHostingEnvironment>
<baseAddressPrefixFilters>
<add prefix="http://huppeldepup.be/"/>
<add prefix="http://huppeldepup.com"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
There you go. Too bad this feature is not available with .NET 3.0. Until you switch to .NET 3.5, you have to roll your own solution I’m afraid.
Till next time.
If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at info. @ principal-it .be
Jan Van Ryswyck
Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.
Comments
Writing Maintainable
Unit Tests
Watch The Videos
Latest articles
-
Contract Tests - Parameterised Test Cases
June 28, 2023
-
Contract Tests - Abstract Test Cases
April 12, 2023
-
Contract Tests
February 1, 2023
-
The Testing Quadrant
June 15, 2022
-
Tales Of TDD: The Big Refactoring
February 2, 2022
Tags
- .NET
- ALT.NET
- ASP.NET
- Agile
- Announcement
- Architecture
- Behavior-Driven Development
- C++
- CQRS
- Clojure
- CoffeeScript
- Community
- Concurrent Programming
- Conferences
- Continuous Integration
- Core Skills
- CouchDB
- Database
- Design Patterns
- Domain-Driven Design
- Event Sourcing
- F#
- Fluent Interfaces
- Functional Programming
- Hacking
- Humor
- Java
- JavaScript
- Linux
- Microsoft
- NHibernate
- NoSQL
- Node.js
- Object-Relational Mapping
- Open Source
- Reading
- Ruby
- Software Design
- SourceControl
- Test-Driven Development
- Testing
- Tools
- Visual Studio
- Web
- Windows
Disclaimer
The opinions expressed on this blog are my own personal opinions. These do NOT represent anyone else’s view on the world in any way whatsoever.
About
Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.
Latest articles
Contract Tests - Parameterised Test Cases
Contract Tests - Abstract Test Cases
Contract Tests
The Testing Quadrant
Contact information
(+32) 496 38 00 82
info @ principal-it .be