locked
How to store the MetaDataService? RRS feed

  • Question

  • Hi,

    How can I store the MetaDataService? I tried to store the MetaDataService in the ViewState, but then I get an 'Not Serializable'-error.

     

    I want to retrieve atrribute-info from the MetaDataService, so I  1) set crm metadata service; 2) create and execute the request and 3) display the retrieved AttributeMetadata on the screen.

    Right now, I set the MetadataService for every request. To improve performance, I want to store the MetadataService, so I don't have to set it every time.

    Any suggestions?

    Best regards, Leonard

    Tuesday, December 28, 2010 8:43 PM

Answers

  • Well, it's not possible to serialize the MetadataService object, because it doesn't implement ISerializable .  Also, consider the fact that if you could serialize it, you wouldn't be gaining any performance improvements by doing so.  You can't serialize a socket connection.  MetadataService doesn't even make a connection until you call the Execute() method, and even when it does, it disposes of it.

    If you simply would rather pass the configuration information for the MetadataService instance between page loads, with ViewState, you could load those up into a serializable class/structure/primitive (if that saves you from having to retrieve that information from somewhere, which might improve performance by some minor degree); however you may want to review security considerations for using ViewState to hold account information.


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    • Marked as answer by Leonard_Str Tuesday, December 28, 2010 10:49 PM
    Tuesday, December 28, 2010 10:40 PM
    Moderator

All replies

  • First thing, if you intend to use ViewState, be sure to read DavidJennaway's article about implementation considerations with CRM .  So long as you have no obstacles there to prevent your usage of ViewState, I think the problem is being able to serialize the MetadataService object--obviously this is what the page is complaining about.  You'll probably have much better luck serializing the return from a MetadataService query--would it be possible for you to do that?  MetadataServiceResponse and derivatives are flagged as "Serializable".
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    Tuesday, December 28, 2010 9:01 PM
    Moderator
  • Thanks for your quick replay. I have the article and explicitly enabled the ViewState ( EnableViewState="true" in page directive).

    I could store the MetadataServiceResponse, but I don't think this is usefull, because every time I sent a different request (with different parameters).

     

    So, do you know whether it is possible to store the MetadataService object (e.g. by serializing and storing it in the ViewState)? Or do you have to set the MetaDataService for every request?

     

        // set crm service
        var metadataService = CrmServiceHelper.SetMetaDataService(CrmOrganization, CrmUsername, CrmPassword, CrmDomain, CrmServer, CrmPort);
        //MetaDataService = _metadataService; //Here I receive a 'not serializable'-error
    
        // Create the request
        var attributeRequest = new RetrieveAttributeRequest();
        attributeRequest.RetrieveAsIfPublished = false;
        attributeRequest.EntityLogicalName = CrmEntity;
        attributeRequest.LogicalName = CrmAttribute;
    
        // Execute the request
        var attributeResponse = (RetrieveAttributeResponse)metadataService.Execute(attributeRequest);
    
    
      public Object MetaDataService
      {
        get
        {
          return ViewState["metadataservice"];
        }
        set
        {
          ViewState["metadataservice"] = value;
        }
      }
    
    
    Tuesday, December 28, 2010 9:27 PM
  • Well, it's not possible to serialize the MetadataService object, because it doesn't implement ISerializable .  Also, consider the fact that if you could serialize it, you wouldn't be gaining any performance improvements by doing so.  You can't serialize a socket connection.  MetadataService doesn't even make a connection until you call the Execute() method, and even when it does, it disposes of it.

    If you simply would rather pass the configuration information for the MetadataService instance between page loads, with ViewState, you could load those up into a serializable class/structure/primitive (if that saves you from having to retrieve that information from somewhere, which might improve performance by some minor degree); however you may want to review security considerations for using ViewState to hold account information.


    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    • Marked as answer by Leonard_Str Tuesday, December 28, 2010 10:49 PM
    Tuesday, December 28, 2010 10:40 PM
    Moderator
  • I thought that every time I was making two connections (1 to set the MetadataService and 1 sending a request and retrieving the response). But now I get that there is just one call. So, no performance to gain here. Thanks for your explanation.
    Tuesday, December 28, 2010 10:48 PM
  • If you really need to improve performance with calls to the MetadataService, you might look into enabling UnsafeAuthenticatedConnectionSharing .  Since both CrmService and MetadataService implement SoapHttpClientProtocol, this feature should apply to both--however, do not use this lightly; it's called "Unsafe" for a reason.  That whole article has some good pointers on improving performance (hence why it's titled "Performance Best Practices").
    Dave Berry - MVP Dynamics CRM - http:\\crmentropy.blogspot.com Please follow the forum guidelines when inquiring of the dedicated CRM community for assistance.
    Tuesday, December 28, 2010 11:40 PM
    Moderator