Apr
28
2009

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.

Apr
28
2009

Creating and Handling Faults in Silverlight

http://msdn.microsoft.com/en-us/library/dd470096(VS.96).aspx

 

Apr
28
2009

How to handle: An error occured creating the configuration section handler for system.serviceModel/behaviors…

Are you developing an extension to Windows Communication Foundation (for example a behavior or message encoder) with configuration file support? If you are you may be getting frustraited by an exception something like the following (the precise nature depends on the kind of extension you are developing).

I was, and the frustraiting thing was that I had built some of these in the past for various purposes without any issues so I had a real tough time figuring out what the hell was going on. Well it turns out that it is a string comparison issue. Lets take a look at the configuration file for a WCF service:

In this configuration file I have a simple math service but I have applied an endpoint behavior to it called “beep” in the configuration file. This is a custom behavior so I’ve had to implement my own BehaviorExtensionElement and in this case I’ve implemented the element and the actual IEndpointBehavior in the same class (because I can). What happens is when WCF initialises it loads in the list of the extensions into a hashtable (simplication - there is a lot more code involved than just a hashtable-like data structure) - so in this case “beep” is mapped to “ServerApplication.BeepBehavior….”, at the same time an instance of the class specified in the second value is instansiated and stored.

As execution continues and the service host is brought online the <beep /> element is encounted. The aforementioned hashtable is looked up and the fully qualified class name is retreived. This is then used as a key to find the custom BehaviorExtensionElement and this is where it comes unstuck. When the BehaviorExtensionElement is stored it is indexed with the fully qualified type name which is fetched using .GetType(), when this value is rendered as a string it looks like this:

ServerApplication.BeepBehavior, ServerApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Notice the spaces between each of the type name elements. Now - compare this to the screenshot of the configuration file above. As you can see there is no space between the type name elements. Since the lookup is based on a simple string comparison the space is significant and the WCF runtime can’t find the previously created BehaviorExtensionElement instance and it all falls in a heap - if you go and put the spaces in it works perfectly.

The frustraiting thing is that the space sensitivity of the type attribute in the WCF portions of the configuration files is completely at odds with the norms in the rest of the .NET Framework (although I am aware of some similar issues in WPF thanks to Darren). Unfortunately it looks like Tomas Restrepo found the issue as well in this feedback entry on Microsoft Connect - but it looks like all development was closed off and it obviously shipped with this quirk.

Personally I think that this needs to be treated as a bug and fixed as soon as possible because it is going to drive developers who try to extend WCF completely nuts and when they realise what the problem was the WCF team better hope they are no where within reach. Given it isn’t a critical bug I suspect that it may have to wait for a service pack to be issued though.

Reprint from:
http://notgartner.wordpress.com/2006/12/19/rant-an-error-occured-creating-the-configuration-section-handler-for-systemservicemodelbehaviors/

Apr
16
2009

A fantastic example of what Silverlight 3 can do

I highly encoruage everyone to check out Mike Harsh's sample application Slidentiy.  It does a great job of showing what is possible with Silverlight and best of all it comes with the source code in by C# and VB.NET

 http://silverlight.net/learn/appcorner/slidentity.aspx?pt=slidentity

Apr
2
2009

How to change the login screen for Windows 7

Here are two great articles on changing the login screen for Windows 7

 A tool to help make the process easier:

http://tweaks.com/software/tweakslogon/

And article that describes the process

http://www.intowindows.com/how-to-change-windows-7-logon-screen-easily-without-using-hacks-tools/

http://www.intowindows.com/download-windows-7-logon-screen-changer-now/