2016-02-02 85 views
0

我正在嘗試將數據成員添加到wcf應用程序,但未能獲得其值。我在我的服務中創建了一個DataContract類,並在我的控制檯應用程序中將該值設置爲此DataContract類的一個對象。但是,我沒有得到我分配的價值。這裏是服務代碼。無法獲取wcf數據成員值

namespace WcfServices 
{ 
    [ServiceContract] 
    public interface ICDNManagementService 
    { 
     [OperationContract] 
     [WebInvoke(Method = "POST", UriTemplate = "SetBlobInformation")] 
     void SetBlobInformation(BlobInformation blobInformation); 
     [OperationContract] 
     [WebInvoke(Method = "GET", UriTemplate = "GetBlobInformation")] 
     BlobInformation GetBlobInformation(); 
    } 

    [DataContract] 
    public class BlobInformation 
    { 
     string _azureBlobUrl; 

     [DataMember] 
     public string azureBlobUrl 
     { 
      get 
      { 
       return _azureBlobUrl; 
      } 
      set 
      { 
       _azureBlobUrl = value; 
      } 
     } 
    } 
} 

而且

namespace WcfServices 
{ 
    public class CDNManagementService : ICDNManagementService 
    { 
     BlobInformation blobInformation = new BlobInformation(); 

     public void SetBlobInformation(BlobInformation blobinformation) 
     { 
      Logger.Log("azureBlobUrl= " + blobinformation.azureBlobUrl); 
      Logger.Log("Before SetBlobInformation: " + blobInformation.azureBlobUrl); 
      blobInformation = blobinformation; 
      Logger.Log("After SetBlobInformation: " + blobInformation.azureBlobUrl); 

     } 

     public BlobInformation GetBlobInformation() 
     { 
      Logger.Log("In GetBlobInformation: " + blobInformation.azureBlobUrl); 
      return blobInformation;    

     } 
    } 
} 

我的客戶端代碼如下。

namespace StringParser 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      string blobUrl = "MyBlobUrl"; 

      CDNManagementService objCDNManagementService = new CDNManagementService(); 

      BlobInformation objBlobInformation = new BlobInformation(); 
      objBlobInformation.azureBlobUrl = blobUrl; 
      objCDNManagementService.SetBlobInformation(objBlobInformation); 

      BlobInformation tmpBlobInformation = new BlobInformation(); 

      tmpBlobInformation = objCDNManagementService.GetBlobInformation(); 
      Console.WriteLine(tmpBlobInformation.azureBlobUrl); 

      Console.WriteLine("Hello World"); 

     } 

    } 
} 

控制檯輸出:

Hello World 

我記錄的信息是:

azureBlobUrl= MyBlobUrl 

Before SetBlobInformation: 

After SetBlobInformation: MyBlobUrl 

In GetBlobInformation: 

我爲什麼沒有在控制檯輸出得到 「MyBlobUrl」 和」 GetBlobInformation:MyBlobUrl 「在我的日誌?我在這裏做錯了什麼或錯過了什麼?任何幫助表示讚賞。

更新:

我添加一個構造到服務:

public class CDNManagementService : ICDNManagementService 
{ 
    BlobInformation _blobInformation = new BlobInformation(); 

    public CDNManagementService() 
    { 
     _blobInformation.azureBlobUrl = "DefaultUrl"; 
    } 
    ...... 
} 

客戶機代碼:

string blobUrl = "MyBlobUrl"; 

    CDNManagementService objCDNManagementService = new CDNManagementService(); 
    BlobInformation objBlobInformation = new BlobInformation(); 

    objBlobInformation = objCDNManagementService.GetBlobInformation(); 
    Console.WriteLine(objBlobInformation.azureBlobUrl); 

    objBlobInformation.azureBlobUrl = blobUrl; 
    objCDNManagementService.SetBlobInformation(objBlobInformation); 

    BlobInformation tmpBlobInformation = new BlobInformation(); 
    tmpBlobInformation = objCDNManagementService.GetBlobInformation(); 
    Console.WriteLine(tmpBlobInformation.azureBlobUrl); 

控制檯輸出:

DefaultUrl 
DefaultUrl 
Hello World 

中的消息記錄:

In GetBlobInformation: DefaultUrl 
azureBlobUrl= MyBlobUrl 
Before SetBlobInformation: DefaultUrl 
After SetBlobInformation: MyBlobUrl 
In GetBlobInformation: DefaultUrl 

似乎在每個OperationContract調用之前調用構造函數。有人知道這裏發生了什麼嗎?謝謝。

+0

因爲您隱藏了變量,如果這些是您的實際名稱。將CDNManagementService.blobinformation重命名爲CDNManagementService._blobinformation。 – DrewJordan

+0

@ DrewJordan,我將CDNManagementService.blobInformation更名爲CDNManagementService._blobInformation,並將'public void SetBlobInformation(BlobInformation blobinformation)'更改爲'public void SetBlobInformation(BlobInformation blobInformation)'。我得到了相同的輸出和日誌。 – user3869992

回答

0

CDNManagementService應該是這樣的:

namespace WcfServices 
{ 
    public class CDNManagementService : ICDNManagementService 
    { 
     BlobInformation _blobInformation = new BlobInformation(); 

     public void SetBlobInformation(BlobInformation blobinformation) 
     { 
      Logger.Log("azureBlobUrl= " + blobinformation.azureBlobUrl); 
      Logger.Log("Before SetBlobInformation: " + _blobInformation.azureBlobUrl); 
      _blobInformation = blobinformation; 
      Logger.Log("After SetBlobInformation: " + _blobInformation.azureBlobUrl); 

     } 

     public BlobInformation GetBlobInformation() 
     { 
      Logger.Log("In GetBlobInformation: " + _blobInformation.azureBlobUrl); 
      return _blobInformation;    

     } 
    } 
} 

這就是你寫的:

public void SetBlobInformation(BlobInformation blobinformation) 
    { 
     Logger.Log("azureBlobUrl= " + blobinformation.azureBlobUrl); 
     Logger.Log("Before SetBlobInformation: " + blobInformation.azureBlobUrl); 
     blobInformation = blobinformation; 
     Logger.Log("After SetBlobInformation: " + blobInformation.azureBlobUrl); 

    } 

編譯器不能告訴你試圖設置之間的差異和你傳入的參數,因爲它們具有相同的名稱。您也只會記錄參數中的信息。當有兩個具有相同名稱的變量時,最接近當前範圍的變量(我知道的措辭較差)是首選。這意味着在你的方法中,它總是試圖首先使用參數值。即使在賦值語句中,它也可以工作:它將首先看到左側,並嘗試將類字段分配給參數。

+0

感謝您的答案。在您的第一條評論之後,我對我的代碼進行了修改,並且在答案中與您的代碼看起來相同。我更新了Web Reference並獲得了相同的結果。不太確定這裏發生了什麼。 – user3869992