无法获取 wcf 数据成员值
本文关键字:数据成员 wcf 获取 | 更新日期: 2023-09-27 18:32:53
我正在尝试将数据成员添加到 wcf 应用程序,但未能获取其值。我在服务中创建了一个 DataContract 类,并在控制台应用程序中将该值设置为此 DataContract 类的对象。但是,我未能获得我分配的值。以下是服务代码。
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",在日志中没有收到"In 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
似乎在每次调用操作合同之前都调用了构造函数。有人知道这里发生了什么吗?谢谢。
您的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); }
编译器无法区分您尝试设置的内容和您传入的参数之间的区别,因为它们具有相同的名称。您也只会记录参数中的信息。当有两个变量具有相同的确切名称时,最好选择最接近当前范围的变量(我知道措辞不佳)。这意味着在您的方法中,它始终会首先尝试使用参数值。这甚至在赋值语句中也有效:它将首先看到左侧,并尝试将类字段分配给参数。