【问题标题】:Type not exposed by WCF ServiceWCF 服务未公开的类型
【发布时间】:2010-03-24 13:15:08
【问题描述】:

我有一个小型测试网络服务来模拟我在现实世界应用程序中注意到的一些奇怪的东西。由于演示显示与应用程序相同的行为,为了简洁起见,我将使用演示。

简而言之,我的服务接口文件如下所示(如您所见,它是 VS2008 创建的默认 WCF 服务,但我在底部添加了一个新的公共方法(GetOtherType())和两个新类(SomeOtherType 和 SomeComplexType ). SomeOtherType 管理 SomeComplexType 类型的通用 List

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFServiceTest
{

    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        SomeOtherType GetOtherType();

    }


    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }

    [DataContract]
    public class SomeOtherType
    {
        public List<SomeComplexType> Items { get; set; }    
    }

    [DataContract]
    public class SomeComplexType
    {

    }
}

我的服务实现如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WCFServiceTest
{

    public class Service1 : IService1
    {


        #region IService1 Members

        public string GetData(int value)
        {
            throw new NotImplementedException();
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IService1 Members


        public SomeOtherType GetOtherType()
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}

我遇到的问题是,如果我在 ASP.NET Web 应用程序中包含对此服务的服务引用,我无法通过智能感知看到 SomeComplexType。错误与找不到类型或命名空间有关。但是,可以找到 SomeOtherType(我假设该类型是公共方法之一的返回类型)。

如果我的一个公共方法(返回类型或参数)的方法签名中没有该类型,我是否认为我不能从 WCF 服务公开该类型?如果是这样,我将如何遍历客户端上 SomeOtherType 实例中的 Items?

非常感谢,我希望这很清楚。

西蒙

【问题讨论】:

  • 这确实很奇怪。为什么要在一个地方定义数据合同和服务本身?另外,这是在 SVC 文件中还是在某个单独的 .cs 文件中?

标签: wcf service intellisense


【解决方案1】:

我的问题是,如果我 包括对此的服务参考 ASP.NET Web 应用程序中的服务, 我看不到 SomeComplexType 通过 智能感知。该错误与 找不到类型或命名空间。 但是,可以找到 SomeOtherType (我假设类型是返回 从其中一种公共方法中键入)。

我认为我不能暴露是对的吗 来自 WCF 服务的类型,如果该类型 方法中没有 我的一种公共方法的签名 (返回类型或参数)?如果 那么,我将如何迭代 在一个实例内的项目之上 客户端上的 SomeOtherType?

您是绝对正确的 - 您的 SomeComplexType 从未在任何服务方法中使用,并且它也从未在任何确实用作服务方法中的参数的类型中标记为 [DataMember]。因此,从 WCF 的角度来看,它不是必需的,也不会出现在服务的 WSDL/XSD 中。

正如格雷厄姆已经指出的那样 - 您在一个地方使用 SomeComplexType

[DataContract]
public class SomeOtherType
{
    public List<SomeComplexType> Items { get; set; }    
}

但由于Items 元素未标记为[DataMember],因此它(以及它使用的类型)不会包含在您的服务的WSDL/XSD 中。由于Items 未标记为DataMember,因此它们也不会出现在您的序列化 WCF 消息中,因此您永远不需要遍历此集合 :-)

所以很可能,您真正想要的只是将[DataMember] 属性添加到您的Items 属性;然后它将包含在 WSDL/XSD 中,SomeComplexType 也将包含。

【讨论】:

  • 感谢 Graham 和 marc_s。两人都回答了这个问题,但将 marc_s 标记为答案,因为更完整的答案可能对其他人有帮助。再次感谢大家
【解决方案2】:

看起来您需要 [DataMember] 属性上的 SomeOtherType.Items 属性,即

[DataMember]
public List<SomeComplexType> Items { get; set; }  

【讨论】:

    【解决方案3】:

    我根本不是这个主题的专家,所以就像一个晴天霹雳:WCF 丢弃了空 DataContracts?尝试暴露 ComplexDataType 中的任何内容(一些 int 就足够了),看看是否会改变任何内容。

    另外,我相信您可以使用内置的wcftestclient 验证该类型的可用性(您需要为此打开元数据交换)。

    【讨论】:

      【解决方案4】:

      我们可以在 Service 中使用 Known 类型,以便在操作合约签名中不直接或间接使用时暴露类及其成员。

      【讨论】:

        【解决方案5】:

        对于那些你只希望它在客户端可用的类型,即使它没有被使用,下面的属性类可以方便地让它在客户端可用。

            [KnownType(typeof(SomeComplexType))]
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多