Ahmad Masykur

Share your knowledge although one function!

About the author

Ahmad Masykur is a Software Architecture Engineer at PT. Freeport Indonesia Jakarta Indonesia.
In this blog, I share things of interest to me. Most topics are likely to be related to software development, but don't hold me to it.

Certificates



Awards


Powered by

Widget Prayer Time not found.

There is an error in XML document (4, 16278).X

Page List

Validators


Ahmad Masykur

How to Resolve WCF Issue: Can't host WCF service in a website with multiple identities

When a WCF service is hosted in a IIS website which has multiple identities, that is, responds on different hostnames/ports, the WCF service, when created, throws the exception below:

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

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: This collection already contains an address with scheme http.  There can be at most one address per scheme in this collection.
Parameter name: item

I am aware that this behavior is by design and I believe that it can be resolved. I'm searching to forums, MSDN and blogs but no solution was found. I'm only found one solution to create a custom ServiceHostFactory which filters the additional base addresses and instantiates the service on one of them only. Thank to Zeddy for the helps. This issue can be resolved by creating a custom ServiceHostFactory which clear all baseAddresses then override Behaviors and ServiceEndPoint described below.

Create Custom ServiceHostFactory

Create new class for custom ServiceHostFactory below.

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Linq;
using System.ServiceModel.Description;

public class MultipleIISBindingSupportServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        // return the emply list Uri to make it automatically select baseAddresses by endpoint configuration
        var host = base.CreateServiceHost(serviceType, new Uri[] {/*empty*/ });

        // Setup MEX dynamically
        var behavior = new ServiceMetadataBehavior
                           {
                               HttpGetEnabled = true,
                               HttpGetUrl = baseAddresses.Where(addr => addr.Scheme == "http").First()
                           };
        host.Description.Behaviors.Add(behavior);
        
        // Setup Endpoint configuration dynamically
        foreach (var uri in baseAddresses)
        {
            // Service endpoint support http scheme only, exclude https scheme
            if (uri.Scheme == "http")
            {
                host.AddServiceEndpoint(serviceType,
                                        new BasicHttpBinding(BasicHttpSecurityMode.None),
                                        uri
                    );
            }
        }
        return host;
    }
}

 

Modify WCF Service Markup

To modify WCF Service Markup, right click on the MyService.svc file and then click "View Markup".

<%@ ServiceHost Language="C#" Debug="true" 
    Service="MyService" 
    CodeBehind="MyService.svc.cs"
    Factory="MultipleIISBindingSupportServiceHostFactory" %>

Modify Web.config File

Open web.config file and going to line with <system.serviceModel> element, replace <system.serviceModel> and all child elements with following config.

<system.serviceModel>
  <diagnostics>
    <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
  </diagnostics>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>

After all steps above was done, the multiple identities issue should be resolved.

I hope this tips will be helpful.


Categories: WCF
Permalink | Comments (9) | Post RSSRSS comment feed

Comments

Ahmad Masykur | Reply

Tuesday, January 20, 2009 11:02 AM

trackback

Membuat WCF Service yang di-Host pada Website dengan Multiple Identities

Ketika WCF service di-host di IIS website yang mana terdapat multiple identities yang dapat merespon

Busby SEO Test United States | Reply

Tuesday, January 20, 2009 1:16 PM

Busby SEO Test

It's complicate steps, Have Googled it correctly to find the solution?

Jeff Johnson United States | Reply

Monday, January 26, 2009 10:25 PM

Jeff Johnson

I keep getting the following error

Could not find a base address that matches scheme http for the endpoint with binding BasicHttpBinding. Registered base address schemes are [].

The error occurs when attempting to create the host variable...

var host = base.CreateServiceHost(serviceType, new Uri[] {/*empty*/ });

Silverlight Testing United States | Reply

Monday, February 02, 2009 11:35 PM

Silverlight Testing

This works great!  Thanks.  Do you also have a solution for HTTPS host?

Indianapolis Web Design United States | Reply

Tuesday, February 03, 2009 3:23 PM

Indianapolis Web Design

i read some article about that, i think google made a new trends of solution

Forsikring United States | Reply

Tuesday, February 03, 2009 5:40 PM

Forsikring

Yes, That's ay be right.

Ronald Widha U.A.E. | Reply

Monday, March 30, 2009 3:03 AM

Ronald Widha

I've got similar issue serving WCF on an IIS config where the web app is serving 2 different ports (secure and unsecure). Would this technique work too in that case you think Ahmad? Help would be appreciated.

IT Ramblings | Reply

Wednesday, April 29, 2009 1:20 AM

trackback

How to Resolve WCF Issue: Can't host WCF service in a website with multiple identities

How to Resolve WCF Issue: Can't host WCF service in a website with multiple identities

Ronald Widha U.A.E. | Reply

Monday, May 04, 2009 5:08 PM

Ronald Widha

This is awesome. It makes sense, applying the binding on runtime to all the urls.
I'm going to try it and see if it works!
Good stuff Ahmad.

Btw, just sent you an invitation for a chat on skype if you're up for it for http://www.temanmacet.com

Add comment




  Country flag

biuquote
  • Comment
  • Preview
Loading