类型未由WCF服务公开

时间:2010-03-24 13:15:09

标签: wcf service intellisense

我有一个小型测试网络服务,可以模拟我在真实应用中注意到的奇怪内容。由于演示显示与应用程序相同的行为,因此我将使用演示简洁。

简而言之,我的服务接口文件如下所示(您可以看到它是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应用程序中包含对此服务的服务引用,我无法通过intellisense看到SomeComplexType。该错误与无法找到的类型或命名空间有关。但是,可以找到SomeOtherType(我假设类型是来自其中一个公共方法的返回类型)。

我是否正确地认为如果我的某个公共方法(返回类型或参数)的方法签名中没有显示该类型,则无法从WCF服务公开类型?如果是这样,我如何能够在客户端上的SomeOtherType实例中迭代Items?

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

西蒙

5 个答案:

答案 0 :(得分:4)

  

我遇到的问题是,如果我   包括对此的服务引用   ASP.NET Web应用程序中的服务,   我看不到SomeComplexType via   智能感知。错误与此有关   无法找到类型或命名空间。   但是,可以找到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也将包含在内。

答案 1 :(得分:2)

您的[DataMember]属性似乎需要SomeOtherType.Items属性,即

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

答案 2 :(得分:1)

我不是这个主题的专家,所以就像一个蓝色的镜头:WCF放弃了空DataContracts?尝试在ComplexDataType中公开任何内容(某些int就足够了)并查看是否会发生任何变化。

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

答案 3 :(得分:1)

我们可以在服务中使用已知类型,以便在未直接或间接在操作合同签名中使用类及其成员时公开它。

答案 4 :(得分:0)

对于您希望它在客户端可用的类型,即使它未被使用,下面的属性类也很方便在客户端提供。

    [KnownType(typeof(SomeComplexType))]
相关问题