Type 'Microsoft.Practices.EnterpriseLibrary.Data.CommandAccessor<MyDataObject>' Is Not Marked Serializable

Topics: Data Access Application Block
Jun 29, 2011 at 9:55 PM

I am currently running into the following exception when using the Enterprise Library 5.0 from within EnterpriseServices.ServicedComponents that are running as Server Applications:
Type 'Microsoft.Practices.EnterpriseLibrary.Data.CommandAccessor<MyDataObject>' Is Not Marked Serializable
The code executes without exception when the ServicedComponent is running as a Library Application.

Is there a way to fix this exception, or is Enterprise Library 5.0 not compatible with EnterpriseServices.ServicedComponents running as Server Applications?

Below is an example of the code in question with the GetData() method throwing the exception:

using System;
using System.Collections.Generic;
using System.EnterpriseServices;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace MyCompany
{
	public class MyBizClass : ServicedComponent
	{
		public IEnumerable<MyDataObject> GetData()
		{
			SqlDatabase myDB = new SqlDatabase(connString);
			return myDB.ExecuteSprocAccessor<MyDataObject>("Sproc", MyDataObject.GetRowMapper(), new object[0]);
		}
	}
	
	[Serializable]
	public MyDataObject
	{
		public string PropA { get; set; }
		public string PropB { get; set; }
		
		public static IRowMapper<MyDataObject> GetRowMapper()
		{
			return MapBuilder<MyDataObject>.MapAllProperties()
				.Map(x => x.PropA).ToColumn("PropADBColumn")
				.Map(x => x.PropB).ToColumn("PropBDBColumn")
				.Build();
		}
	}
}

Jun 30, 2011 at 3:08 AM

Hi,

I haven't tried Serviced Component yet but I guess it is more of a serialization issue. Try adding [NonSerialized] attribute to PropA and PropB and see if it works.

 

Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jun 30, 2011 at 2:25 PM

Hi,

I tried your suggestion, but it didn't work.  I finally was able to get the code to work by changing the GetData() method to:

		public List<MyDataObject> GetData()
		{
			SqlDatabase myDB = new SqlDatabase(connString);
			return myDB.ExecuteSprocAccessor<MyDataObject>("Sproc", MyDataObject.GetRowMapper(), new object[0]).ToList<MyDataObject>();
		}

which seems a bit counter-intuitive as I was under the impression that returning the generic IEnumerable was best practices rather than returning specific IEnumerable implementations.