簡體   English   中英

類型未由WCF服務公開

[英]Type not exposed by WCF Service

我有一個小的測試Web服務來模擬我在真實應用程序中注意到的奇怪的東西。 由於演示顯示與應用程序相同的行為,因此我將使用演示簡潔。

簡而言之,我的服務接口文件如下所示(您可以看到它是VS2008創建的默認WCF服務,但我添加了一個新的公共方法(GetOtherType())和底部的兩個新類(SomeOtherType和SomeComplexType).TomeOtherType管理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?

非常感謝,我希望這很清楚。

西蒙

我遇到的問題是,如果我在ASP.NET Web應用程序中包含對此服務的服務引用,我無法通過intellisense看到SomeComplexType。 該錯誤與無法找到的類型或命名空間有關。 但是,可以找到SomeOtherType(我假設類型是來自其中一個公共方法的返回類型)。

我是否正確地認為如果該類型在我的一個公共方法(返回類型或參數)的方法簽名中沒有特征,我就無法從WCF服務公開類型? 如果是這樣,我如何能夠在客戶端上的SomeOtherType實例中迭代Items?

您絕對正確 - 您的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也將包含SomeComplexType

看起來你需要SomeOtherType.Items屬性的[DataMember]屬性,即

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

我不是這個主題的專家,所以就像一個藍色的鏡頭:WCF放棄了空DataContracts? 嘗試在ComplexDataType中公開任何東西(某些int就足夠了)並查看是否有任何改變。

此外,我相信您可以使用內置的wcftestclient驗證類型的可用性(您需要為此啟用元數據交換)。

我們可以在Service中使用Known類型,以便在直接或間接不在操作契約簽名中使用時暴露類及其成員。

對於您希望它在客戶端可用的類型,即使它沒有被使用,下面的屬性類也很方便在客戶端使用它。

    [KnownType(typeof(SomeComplexType))]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM