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.
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
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.