none
Help needed getting Custom Marshaling working RRS feed

  • Question

  • Hello,

    I am trying to get custom marshaling working in a particular scenario but it does not appear to be working or not jumping into my marshaling class when I try to debug it. I am try to implement a performance improvement in the transfer of thousands of objects (ClientInfoDescriptor) over a remote call. The objects all have a model object (IDataDrivenModel) to describe them but there may only two or three model objects in total. These model object could be large objects. Instead of transfering duplicate models I would like to marshal in the model id and when I retrieve the objects on the client side I will un marshal the model id and lookup the model from the cache.

    I would appreciate it if anyone could tell me where I may be going wrong or give me tips on how to better debug this custom marshaling. Or maybe there is a better way to get this performance improvement without custom marshaling?

    Thanks,
    JP

        [Serializable]
        public abstract class ADDInfoDescriptor : IDDInfoDescriptor
        {
            [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(DDMarshaler))]
            private IDataDrivenModel ddModel = null;
            ...
        }

        // Class that has attribute being customed marshaled
        [Serializable]
        public class ClientInfoDescriptor : ADDInfoDescriptor
        {
           ...
        }

        // Marshaling class
        public sealed class DDMarshaler : System.Runtime.InteropServices.ICustomMarshaler
        {
            private static DDMarshaler marshaler = null;
            public static ICustomMarshaler GetInstance(string cookie)
            {
                if (marshaler == null)
                {
                    marshaler = new DDMarshaler();
                }
                return marshaler;
            }

            public DDMarshaler()
            {
            }

            public object MarshalNativeToManaged(System.IntPtr pNativeData)
            {
                int id = Marshal.ReadInt32(pNativeData);
                return DDModelCache.Instance.GetModel(id);
            }

            public System.IntPtr MarshalManagedToNative(object managedObj)
            {
                IDataDrivenModel ddModel = (IDataDrivenModel)managedObj;
                IntPtr unmanagedArray = Marshal.AllocHGlobal(sizeof(Int32));
                Marshal.WriteInt32(unmanagedArray, ddModel.ID);
                return unmanagedArray;
            }

            public void CleanUpManagedData(object ManagedObj)
            {
            }

            public int GetNativeDataSize()
            {
                return sizeof(Int32);
            }

            public void CleanUpNativeData(System.IntPtr pNativeData)
            {
                Marshal.FreeHGlobal(pNativeData);
            }
        }
    Wednesday, February 13, 2008 9:40 AM

All replies

  • When you say "remote call", are you using .NET remoting for that? If so I think you're confusing two different kinds of marshaling. The MarshalAs attribute is used for interop between managed and native code, not in remoting scenarios.

     

    Wednesday, February 20, 2008 9:58 AM
    Moderator