无法获得wcf数据成员值

时间:2016-02-02 21:15:11

标签: c# wcf datacontract

我正在尝试将数据成员添加到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”,在我的日志中“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调用之前调用了构造函数。有谁知道这里发生了什么?感谢。

1 个答案:

答案 0 :(得分: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);

    }

编译器无法区分您尝试设置的内容与传入的参数之间的区别,因为它们具有相同的名称。您还只会记录参数中的信息。当有两个具有相同确切名称的变量时,最接近当前范围的一个(我知道的措辞很差)是首选。这意味着在您的方法中,它总是首先尝试使用参数值。这甚至在赋值语句中也有效:它将首先看到左侧,并尝试将类字段分配给参数。