首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WcfFacility和序列不包含任何元素错误?

WcfFacility和序列不包含任何元素错误?
EN

Stack Overflow用户
提问于 2012-11-15 08:58:02
回答 1查看 434关注 0票数 0

我有包含服务合同和实现的wcf库。

代码语言:javascript
复制
     [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!";
        }
    }

我有一个引用这个库的windows服务项目,其中MyService只是一个继承ServiceBase的空外壳。此服务在本地系统下安装和运行。

代码语言:javascript
复制
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
    }

在客户端控制台应用程序中,我有以下代码,并且收到以下错误:{"Sequence contains elements"}

代码语言:javascript
复制
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();

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-16 15:46:45

我知道这可能是什么,但如果对以下问题的答案是否定的,你现在可以停止阅读这篇文章(我很抱歉提前浪费了你的时间):

在与Request**IServiceProtoType**?**相同的命名空间中,是一个(或多个)Request**,** Response**,或** MyDemoService

我怀疑温莎对这些感到困惑,因为你在做...

代码语言:javascript
复制
Types
    .FromAssemblyContaining<IServiceProtoType>()
    .InSameNamespaceAs<IServiceProtoType>()

..。然后配置作为WCF客户端代理返回的所有内容。这意味着它将尝试为不应该创建代理的东西创建代理,因此会出现Sequence Contains no Elements异常(不是最有用的消息,但会崩溃)。

简单的解决方法就是将您的IServiceProtoType放入它自己的名称空间(我经常为我的服务契约使用一个像XXXX.Services这样的名称空间)。

如果这对你来说是不可接受的,那么你需要找出另一种方法来识别服务契约-例如,看看If方法,或者仅仅是一个好的ol‘Component.For

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13390021

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档