我正在嘗試將數據成員添加到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調用之前調用構造函數。有人知道這裏發生了什麼嗎?謝謝。
因爲您隱藏了變量,如果這些是您的實際名稱。將CDNManagementService.blobinformation重命名爲CDNManagementService._blobinformation。 – DrewJordan
@ DrewJordan,我將CDNManagementService.blobInformation更名爲CDNManagementService._blobInformation,並將'public void SetBlobInformation(BlobInformation blobinformation)'更改爲'public void SetBlobInformation(BlobInformation blobInformation)'。我得到了相同的輸出和日誌。 – user3869992