2010-03-24 116 views
4

我有一個小型測試Web服務來模擬我在一個真實世界的應用程序中注意到的奇怪事情。由於演示顯示與應用程序相同的行爲,因此我將使用該演示以簡化操作。我可以看到它是由VS2008創建的默認WCF服務,但我在底部添加了一個新的公共方法(GetOtherType())和兩個新類(SomeOtherType和SomeComplexType)。SomeOtherType管理型SomeComplexType的泛型列表WCF服務未公開的類型

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?

很多謝謝,我希望這很清楚。

西蒙

+0

確實很奇怪。你爲什麼要在一個地方定義數據合同和服務本身?此外,這是在SVC文件或一些單獨的.cs文件? – Slavo 2010-03-24 13:39:01

回答

4

我的問題是,如果我 包括ASP.NET Web應用程序的服務引用這個 服務, 我無法通過 intellisense查看SomeComplexType。該錯誤與 類型或名稱空間無法找到有關。 但是,SomeOtherType可以找到 (我假設這種類型是從一種公共方法返回 類型)。

我是正確的思維,我不能暴露 類型從一個WCF服務,如果該類型 在我的公開方法 (無論返回類型或參數)一個方法 簽名沒有特色?如果 那麼,我將如何能夠遍歷 客戶端上的SomeOtherType實例內的項目 ?

你是絕對正確的 - 你SomeComplexType是從來沒有在任何的服務方法使用,並且它也從來沒有標記爲[數據成員在任何確實用作服務方法的參數的類型。因此,從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也將包含在內。

+0

感謝Graham和marc_s。兩人都回答了這個問題,但將marc_s標記爲答案,因爲更完整的答案可能對其他人有幫助。再次感謝大家 – 2010-03-25 09:56:24

1

我不在此主題的所有方面的專家,所以就像在藍色拍:空DataContracts被丟棄WCF?嘗試暴露ComplexDataType中的任何內容(有些int就足夠了),看看它是否會改變任何內容。

此外,我相信你可以使用內置的wcftestclient(你需要爲此打開元數據交換)來驗證類型的可用性。

2

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

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

我們可以在服務中使用已知類型,以便在直接或間接地在操作合同簽名中未使用該類及其成員時暴露該類及其成員。

0

對於您只希望在客戶端可用的類型(即使未使用它),在屬性類下方可方便地在客戶端使用它。

[KnownType(typeof(SomeComplexType))]