2014-11-24 17 views
0

縱觀該請求的生成Reference.cs文件中,有一個屬性:爲什麼更新WCF服務引用會將一個數字附加到屬性中?

[System.Runtime.Serialization.DataMemberAttribute()] 
    public TestClass1 TestClass { 
     get { 
      return this.TestClassField; 
     } 
     set { 
      if ((object.ReferenceEquals(this.TestClassField, value) != true)) { 
       this.TestClassField = value; 
       this.RaisePropertyChanged("TestClass"); 
      } 
     } 
    } 

與另一個屬性:

[System.Runtime.Serialization.DataMemberAttribute()] 
    public TestClass TestClass { 
     get { 
      return this.TestClassField; 
     } 
     set { 
      if ((this.TestClassField.Equals(value) != true)) { 
       this.TestClassField = value; 
       this.RaisePropertyChanged("TestClass"); 
      } 
     } 
    } 

引用相同的字段。當我嘗試使用這個字段:

var sampleRequest = new SampleRequest(); 
sampleRequest.TestClass = new global::SampleService.TestClass(); 

將引發錯誤:

不能源類型的TestClass轉換爲TestClass1。

sampleRequest.TestClass具有類型TestClass,而不是引用未被覆蓋的屬性,它指的是TestClass1。爲什麼會發生?有沒有辦法抑制這種行爲?

+0

刪除並重新添加服務引用完全沒有問題嗎?我敢打賭,這將避免這個問題。 – Bensonius 2014-11-24 23:44:31

+1

@Bensonius:不是。也許有兩個獨立的類型/元素名爲'TestClass'。 – 2014-11-25 04:07:45

+0

@JohnSaunders:好點!有東西要出去尋找。 – Bensonius 2014-11-25 05:38:16

回答

1

我會說你是他們合同的受害者。有人用相同的名字裝飾了兩個屬性(並且正在基於此生成引用)。像下面的內容是最有可能發生的事情:

// The original written application had the following class, but then 
// deprecated it (maybe they changed around object, changed interfaces, 
// who knows. It just got deprecated internally.) 
// So, with all the ties in the system, they probably retained the name, 
// but renamed it in the contract. 
[Obsolete] 
[DataContract(Name = "TestClass1")] 
public class TextClass // The class formerly known as TestClass 
{ 
} 

// This is the refactored TestClass that should be used from here on out. 
// They most likely created a new class with a similar name which lent 
// itself to being plumbed in a modular fashion (but wanted the same name 
// to any service consumers) 
[DataContract(Name = "TestClass")] 
public class TestClassNew 
{ 
} 

// DTO 
[DataContract] 
public ParentClass 
{ 
    // Keep the old reference (may still have back-references or 
    // functionality that hasn't been migrated). However, they should have 
    // also renamed the "Name"). 
    [DataMember(Name = "TestClass")] 
    public TestClass TestClass { get; set; } 

    // The new object that now shares the same name. 
    [DataMember(Name = "TestClass")] 
    public TestClassNew TestClassNew { get; set; } 
} 

會導致其中,在客戶端上,在具有相同名稱的兩個屬性。

相關問題