Bug 37035 - Creating service host with singleton fails
Summary: Creating service host with singleton fails
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: 4.2.0 (C6)
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Aleksey Kliger
Depends on:
Reported: 2015-12-15 18:27 UTC by greg
Modified: 2015-12-22 16:48 UTC (History)
2 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:


Description greg 2015-12-15 18:27:39 UTC
The following code fails on Linux with exception:" When creating a Service host with a service instance, use InstanceContextMode.Single in the ServiceBehaviorAttribute." and works on windows:

 mono --version
Mono JIT compiler version 4.2.1 (Stable Thu Nov 12 09:52:44 UTC 2015)

using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;

namespace foo

    static class Program
        static void Main(string[] args)
            var uri = $"http://localhost:8732/Design_Time_Addresses/AudioVideo/";
            WebServiceHost host = new WebServiceHost(new AudioVideo(), new Uri(uri));
                var ServiceBehavAttr = (ServiceBehaviorAttribute)Attribute.GetCustomAttribute(typeof(AudioVideo), typeof(ServiceBehaviorAttribute));
                if (ServiceBehavAttr != null)
                ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IAudioVideo), new WebHttpBinding(), "");
                host.Open();  // fails on linux with ... use InstanceContextMode.Single in the ServiceBehaviorAttribute
                using (var cf = new ChannelFactory<IAudioVideo>(new WebHttpBinding(), uri))
                    cf.Endpoint.Behaviors.Add(new WebHttpBehavior());

                    var channel = cf.CreateChannel();

            catch (Exception ex)
        public interface IAudioVideo
            [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.Wrapped)]
            void Start();
        [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
        public abstract class Component

        public class AudioVideo : Component, IAudioVideo
            public void Start() { }
Comment 1 Aleksey Kliger 2015-12-21 19:38:21 UTC
Mono ServiceHost implementation was using GetCustomAttributes(..., false) to get the ServiceBehaviorAttribute from the service type.

As a workaround, you can put the attribute on the AudioVideo class directly, not on its parent class.
Comment 2 Aleksey Kliger 2015-12-22 16:48:16 UTC
Fixed in master commit 5c7b6ea2200218f57e4feb657dc2926ca30a31e9

Notice (2018-05-21): bugzilla.xamarin.com will be switching to read-only mode on Thursday, 2018-05-25 22:00 UTC.

Please join us on Visual Studio Developer Community and GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs and copy them to the new locations as needed for follow-up. The See Also field on each Bugzilla bug will be updated with a link to its new location when applicable.

After Bugzilla is read-only, if you have new information to add for a bug that does not yet have a matching issue on Developer Community or GitHub, you can create a follow-up issue in the new location. Copy and paste the title and description from this bug, and then add your new details. You can get a pre-formatted version of the title and description here:

In special cases you might also want the comments:

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Note You need to log in before you can comment on or make changes to this bug.