none
Can I have a data member inside a wcf class RRS feed

  • Question

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

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


    Thursday, February 4, 2016 9:19 PM

Answers

All replies

  • Hi Mimimontreal,

    I have copied your code snippet and test in my side, but I would not reproduce your issue in my side, and the output is:
    Default Url
    MyBlobUrl
    Hello World

    My development environment is: Visual Studio Enterprise 2015/ .Net Framework 4.6.

    Could you please tell us what is your VS and .NET version? Besides, please try to copy your code to a new blank project to see what happens.

    Best regards,
    Amy Peng

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Monday, February 8, 2016 10:08 AM
    Moderator
  • Hi Amy, thanks for your reply. I am using the following development environments:

    Microsoft Visual Studio Express 2013 for Windows Desktop
    Version 12.0.31101.00 Update 4
    Microsoft .NET Framework
    Version 4.5.50938

    Installed Version: Desktop Express

    Team Explorer for Visual Studio 2013   06157-004-0441005-02888
    Microsoft Team Explorer for Visual Studio 2013

    Visual Basic 2013   06157-004-0441005-02888
    Microsoft Visual Basic 2013

    Visual C# 2013   06157-004-0441005-02888
    Microsoft Visual C# 2013

    Visual C++ 2013   06157-004-0441005-02888
    Microsoft Visual C++ 2013

    Common Azure Tools   1.3
    Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

    NuGet Package Manager   2.8.50926.663
    NuGet Package Manager in Visual Studio. For more information about NuGet, visit http://docs.nuget.org/.

    PowerShell Tools   1.3
    Provides file classification services using PowerShell

    SQL Server Data Tools   12.0.41012.0
    Microsoft SQL Server Data Tools

    I did more tests and found the following:

    When I declared an object of proxy class(CDNManagementService objCDNManagementService = new CDNManagementService();), the constructor was not invoked. However, EACH TIME when I invoked an OperationContract (SetBlobInformation(BlobInformation blobInformation) or BlobInformation GetBlobInformation()), the constructor was invoked before the OperationContract was invoked.

    I will try to create a new blank project and give you feedback.

    Best Regards

    Mimimontreal


    Monday, February 8, 2016 4:45 PM
  • Hi Amy

    I created a new blank project and added web reference to the project. My service class constructor looks like this:

            public CDNManagementService()
            {
                Logger.Log("Constructor called");
                _blobInformation.azureBlobUrl = "DefaultUrl";
            }

    At first try, my client code looks like this:

    ......

    CDNManagementService objCDNManagementService = new CDNManagementService();

    Console.WriteLine("Hello World");

    ......

    The console output:

    Hello World

    And no logs are recorded.

    At second try, my client code looks like this:

    ......

    CDNManagementService objCDNManagementService = new CDNManagementService();

    BlobInformation objBlobInformation = new BlobInformation();

    objBlobInformation = objCDNManagementService.GetBlobInformation();

    Console.WriteLine(objBlobInformation.azureBlobUrl);

    Console.WriteLine("Hello World");

    ......

    The console output:

    DefaultUrl

    Hello World

    The logs recorded:

    Constructor called

    In GetBlobInformation: DefaultUrl

    Here the service class constructor was not invoked when I declare an object but invoked before GetBlobInformation was invoked.

    Best Regards

    Mimimontreal 

    Monday, February 8, 2016 8:16 PM
  • Hello,

    I have copied your code in the visual studio 2013.

    At first try, my client code looks like this:

    ......

    CDNManagementService objCDNManagementService = new CDNManagementService();

    Console.WriteLine("Hello World");

    ......

    The console output:

    Hello World

    The logs recorded:

    Constructor called

    At second try, my client code looks like this:

    ......

    CDNManagementService objCDNManagementService = new CDNManagementService();

    BlobInformation objBlobInformation = new BlobInformation();

    objBlobInformation = objCDNManagementService.GetBlobInformation();

    Console.WriteLine(objBlobInformation.azureBlobUrl);

    Console.WriteLine("Hello World");

    ......

    The console output:

    DefaultUrl

    Hello World

    The logs recorded:

    Constructor called

    In GetBlobInformation: DefaultUrl

    It works fine in my side, if it is possible, could you please share your project to us via OneDrive to make sure that we have tested with a same project.

    Best regards,
    Fang Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, February 17, 2016 8:48 AM
    Moderator
  • Hi Fang Peng, thanks for your reply. What you got is what are expected. I didn't log anything on my first try. I already uploaded my project for your reference. 

    My objective is to upload a big file to Azure storage blob via wcf service. If I can keep the blob url as a data member of the service, Then I can just pass a stream object to the service method. 

    I also tried using MessageContract (see http://stackoverflow.com/questions/35444303/streaming-big-file-to-azure-over-wcf-failed) but failed too. I am really struggling with this.

    Best Regards

    Mimimontreal

    Wednesday, February 17, 2016 7:58 PM
  • Hello,

    Based on your code snippet, it seems that your service is a WCF Rest Service. But on your client, it seems that you only reference your WCF service as a library to use it. Please try to use HttpWebRequest to access your WCF service and check if it works for you.

    Here is sample about using HttpWebRequest to access the WCF Rest Service, please try to check it:
    https://msdn.microsoft.com/en-us/library/cc656724.aspx?f=255&MSPPError=-2147217396 .

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, February 19, 2016 8:52 AM
    Moderator
  • Hi Amy,

    Thanks for the reply. I made modifications to make my service  a WCF SOAP service.

    namespace MilkboxGames.Services
    {
    
        [ServiceContract]
        public interface ICDNManagementService
        {
            [OperationContract]
            void SetBlobInformation(BlobInformation blobInformation);
            [OperationContract]
            BlobInformation GetBlobInformation();
    
        }
    
        [DataContract]
        public class BlobInformation
        {
            string _azureBlobUrl;
    
            [DataMember]
            public string azureBlobUrl
            {
                get
                {
                    return _azureBlobUrl;
                }
                set
                {
                    _azureBlobUrl = value;
                }
            }
        }
    }
    namespace MilkboxGames.Services
    {
    
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    
        public class CDNManagementService : ICDNManagementService
        {
            BlobInformation _blobInformation = new BlobInformation();
    
            public CDNManagementService()
            {
                Logger.Log("Constructor called");
                _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;            
    
            }
        }
    }

    And Web.config:

      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_CDNManagementService" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" receiveTimeout="00:10:00" sendTimeout="00:10:00" openTimeout="00:10:00" closeTimeout="00:10:00" transferMode="Streamed">
              <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
              <security mode="None"/>
            </binding>
          </basicHttpBinding>
        </bindings>
        <services>
    		  <service name="MilkboxGames.Services.CDNManagementService">
            <endpoint address="" 
    				binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_CDNManagementService" contract="MilkboxGames.Services.ICDNManagementService" />
            <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
          </service>
        </services>
        <protocolMapping>
            <add binding="basicHttpsBinding" scheme="https" />
        </protocolMapping>    
        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
      </system.serviceModel>

    I deployed the service to Azure and added it to Service References of my console application. The client code:

    namespace XuConsoleApplication { class Program { static void Main(string[] args) { CDNManagementServiceClient objCDNManagementService = new CDNManagementServiceClient(); // BlobInformation objBlobInformation = new BlobInformation(); // objBlobInformation = objCDNManagementService.GetBlobInformation(); // Console.WriteLine(objBlobInformation.azureBlobUrl); Console.WriteLine("Hello World");

    } } }

    I still got the same results as before, i.e., at first try, the constructor was not invoked;

    at second try, the constructor was invoked before GetBlobInformation() was invoked. By the

    way, it worked out if I make azureBlobUrl a MessageContract header instead of service class

    data member.

    Thanks

    Mimimontreal



     
    Wednesday, February 24, 2016 4:28 PM