Bug 37035

Summary: Creating service host with singleton fails
Product: [Mono] Class Libraries Reporter: greg
Component: WCF assembliesAssignee: Aleksey Kliger <aleksey>
Severity: normal CC: aleksey, mono-bugs+mono
Priority: ---    
Version: 4.2.0 (C6)   
Target Milestone: Untriaged   
Hardware: PC   
OS: Linux   
Tags: 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