WcfFacility和Sequence包含没有元素错误?

时间:2012-11-15 00:58:02

标签: castle-windsor castle wcffacility

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

     [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的空shell。此服务已在本地系统下安装并运行。

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
    }

在Client Console应用程序中,我有以下代码,我收到以下错误: {“序列不包含元素”}


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();

        }

1 个答案:

答案 0 :(得分:1)

我知道这可能是什么但你现在可以停止阅读(我为提前浪费你的时间道歉)如果以下答案是否定的:

Request位于同一名称空间中的ResponseMyDemoServiceIServiceProtoType中的一个(或多个)?

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

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

...然后配置作为WCF客户端代理返回的所有内容。这意味着它将尝试为不应该出现的事物创建代理,因此Sequence Contains no Elements例外(不是最有用的消息恕我直言,但要碾压)。

简单的解决方法就是将你的IServiceProtoType放入自己的命名空间(我的服务合约经常有XXXX.Services这样的命名空间。)

如果您无法接受,那么您需要找出另一种方法来确定服务合同 - 例如,查看If方法,或者只看一个好的“Component.For