Bug 37035 - Creating service host with singleton fails
Summary: Creating service host with singleton fails
Status: RESOLVED FIXED
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
URL:
Depends on:
Blocks:
 
Reported: 2015-12-15 18:27 UTC by greg
Modified: 2015-12-22 16:48 UTC (History)
2 users (show)

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


Attachments

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 4.2.1.102/6dd2d0d 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));
            try
            {
                var ServiceBehavAttr = (ServiceBehaviorAttribute)Attribute.GetCustomAttribute(typeof(AudioVideo), typeof(ServiceBehaviorAttribute));
                if (ServiceBehavAttr != null)
                {
                    Console.WriteLine($"{ServiceBehavAttr.InstanceContextMode}");
                }
                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();

                }
                Console.ReadKey();
                host.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
        
        [ServiceContract]
        public interface IAudioVideo
        {
            [OperationContract]
            [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

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