Question

I have wcf library with service contracts and implementations.

     [ServiceContract]
    public interface IServiceProtoType 
    {
        [OperationContract]
        Response GetMessage(Request request);

        [OperationContract]
        String SayHello();

    }
    [DataContract]
    public class Request
    {
        private string name;

        [DataMember]
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

    }
    [DataContract]
    public class Response
    {
        private string message;

        [DataMember]
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

    }
 public class MyDemoService : IServiceProtoType
    {
        public Response GetMessage(Request request)
        {
            var response = new Response();
            if (null == request)
            {
                response.Message = "Error!";
            }
            else
            {
                response.Message = "Hello, " + request.Name;
            }
            return response;
        }
        public string SayHello()
        {
            return "Hello, World!";
        }
    }

I have windows service project that references this library, where MyService is just an empty shell that inherits ServiceBase. This service is installed and running under local system.

static void Main()
        {
          ServiceBase.Run(CreateContainer().Resolve());
        }

        private static IWindsorContainer CreateContainer()
        {
            IWindsorContainer container = new WindsorContainer();
            container.Install(FromAssembly.This());
            return container;
        }

 public class  ServiceInstaller : IWindsorInstaller
    {

        #region IWindsorInstaller Members

        public void Install(IWindsorContainer container, Castle.MicroKernel.SubSystems.Configuration.IConfigurationStore store)
        {

            string myDir;

            if (string.IsNullOrEmpty(AppDomain.CurrentDomain.RelativeSearchPath))
            {
                myDir = AppDomain.CurrentDomain.BaseDirectory;
            }
            else
            {
                myDir = AppDomain.CurrentDomain.RelativeSearchPath;
            }

            var wcfLibPath  = Path.Combine(myDir , "WcfDemo.dll");
            string baseUrl = "http://localhost:8731/DemoService/{0}";
            AssemblyName myAssembly = AssemblyName.GetAssemblyName(wcfLibPath);

            container
                .Register(
                    AllTypes
                        .FromAssemblyNamed(myAssembly.Name)
                        .InSameNamespaceAs<WcfDemo.MyDemoService>()
                        .WithServiceDefaultInterfaces()
                        .Configure(c =>
                                   c.Named(c.Implementation.Name)
                                       .AsWcfService(
                                           new DefaultServiceModel()
                                               .AddEndpoints(WcfEndpoint
                                                                 .BoundTo(new WSHttpBinding())
                                                                 .At(string.Format(baseUrl,
                                                                     c.Implementation.Name)
                                                                 )))), Component.For<ServiceBase>().ImplementedBy<MyService>());
        }

        #endregion
    }

In Client Console app I have the following code and I am getting the following error: {"Sequence contains no elements"}


static void Main(string[] args)
        {
            IWindsorContainer container = new WindsorContainer();
            string baseUrl = "http://localhost:8731/DemoService/{0}";
            container.AddFacility<WcfFacility>(f => f.CloseTimeout = TimeSpan.Zero);

            container
                .Register(
                    Types
                        .FromAssemblyContaining<IServiceProtoType>()
                        .InSameNamespaceAs<IServiceProtoType>()
                        .Configure(
                            c => c.Named(c.Implementation.Name)
                                     .AsWcfClient(new DefaultClientModel
                                     {
                                         Endpoint = WcfEndpoint
                                             .BoundTo(new WSHttpBinding())
                                             .At(string.Format(baseUrl,
                                                 c.Name.Substring(1)))
                                     })));


            var service1 = container.Resolve<IServiceProtoType>();

            Console.WriteLine(service1.SayHello());

            Console.ReadLine();

        }
Was it helpful?

Solution

I have an idea what this may be but you can stop reading this now (and I apologize for wasting your time in advance) if the answer to the following is no:

Is one (or more) of Request, Response, or MyDemoService in the same namespace as IServiceProtoType?

I suspect that Windsor is getting confused about those, since you are doing...

Types
    .FromAssemblyContaining<IServiceProtoType>()
    .InSameNamespaceAs<IServiceProtoType>()

... and then configuring everything which that returns as a WCF client proxy. This means that it will be trying to create proxies for things that should not be and hence a Sequence Contains no Elements exception (not the most useful message IMHO but crushing on).

The simple fix would be just to put your IServiceProtoType into its own namespace (I often have a namespace like XXXX.Services for my service contracts).

If that is not acceptable to you then you need to work out another way to identify just the service contracts - take a look at the If method for example or just a good ol' Component.For perhaps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top