Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to add a data member to wcf application but failed to get its value. I created a DataContract class in my service and set the value to an object of this DataContract class in my console application. However, I failed to get the value I assigned. Here are the service codes.

C#
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;
            }
        }
    }
}

And

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

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

        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;            
        }
    }
}


Client code

C#
namespace StringParser
{
    class Program
    {
        static void Main(string[] args)
        {
            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);

            Console.WriteLine("Hello World");

        }
    }
}


Console outputs

DefaultUrl
DefaultUrl
Hello World


Messages logged

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


I didn't get "MyBlobUrl" when I invoke GetBlobInformation the second time. It seems that the constructor was invoked before invoking GetBlobInformation the second time. Does anybody know what is going on here? Thanks.

What I have tried:

I added a constructor to the service to check the values.
Posted
Updated 4-Feb-16 10:27am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900