Unable to serialize the session state, which is related to System.Data.Linq. EntitySet - asp.net-mvc-3

Recently I am playing with the Windows Server 2008,
and would like to make some configuration for my web application.
I have changed the Session State for my application to "State Server" so it can make use of more work process (Now it is set to 4).
Afterwards I have got this error when I attempt to login to the application.
Unable to serialize the session state. In 'StateServer' and 'SQLServer' mode, ASP.NET will serialize the session state objects, and as a result non-serializable objects or MarshalByRef objects are not permitted. The same restriction applies if similar serialization is done by the custom session state store in 'Custom' mode.
I suspect it is related to the EntitySet I have declared because I see this in the Stack Trace when the error occur:
[SerializationException: Type
'System.Data.Linq.EntityRef`1[[CtcSystem.Domain.Entities.Account.Account,
CtcSystem.Domain, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null]]' in Assembly 'System.Data.Linq, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as
serializable.]
I have used [Serializable] tag on my classes,
so is there any way I have to do to make the EntitySet serializable so that the application can run under State Server?
Thanks

Best way is to implement the ISerializable interface on a partial class...

Related

AppFabric: Cache host cannot load provider assembly for read-through and write-behind

I want to try out the new read-through/write-behind feature of AppFabric 1.1. I implemented a provider that references some other own assemblies, according to http://msdn.microsoft.com/en-us/library/hh361698%28v=azure.10%29.aspx. All are signed and compiled for AnyCPU. I put the provider and all referenced assemblies in the GAC (see http://msdn.microsoft.com/en-us/library/hh361703(v=azure.10).aspx). Then I stopped the cache cluster and created a new cache with the read-through and write-behind options passing the full name of my provider assembly (that I got from gacutil -l).
New-Cache ReadThroughWriteBehindCache -ReadThroughEnabled true -WriteBehindEnabled true
-WriteBehindInterval 60 -ProviderType "CachingDemo.Provider, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=236fd28d53a6f1ad" -ProviderSettings #{"connectionString"="Data Source=.;Initial
Catalog=Demo;Persist Security Info=True;User
ID=sa;Password=password;MultipleActiveResultSets=True";"logFilePath"="C:\Logs\CacheProvider"}
When starting the cache cluster again I got a timeout and following error message in the event log:
AppFabric Caching service crashed with exception Microsoft.ApplicationServer.Caching.DataCacheException:
ErrorCode<ERRCMS0007>:SubStatus<ES0001>:Provider "CachingDemo.Provider, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=236fd28d53a6f1ad" instantiation failed: The given assembly name
or codebase was invalid. (Exception from HRESULT: 0x80131047) ---> System.IO.FileLoadException:
The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)
What could possibly be wrong? I triple-checked the assembly name. The name I passed when creating the cache is exactly the same as the one I get from gacutil -l. The assembly is AnyCPU what should work in any case. Since the assembly is not even loaded errors inside the assembly can be ruled-out.
The reason was that the provider type information was not correctly passed when creating the cache. I assumed that AppFabric would find the provider itself (by reflection). But we have to add the provider type in the ProviderType argument. Additionaly between the type and the assembly information only a comma is allowed (not a comma with a space for example):
New-Cache ReadThroughWriteBehindCache -ReadThroughEnabled true -WriteBehindEnabled true
-WriteBehindInterval 60 -ProviderType CachingDemo.Provider.Provider,CachingDemo.Provider,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=236fd28d53a6f1ad"
-ProviderSettings #{"connectionString"="Data Source=.;
Initial Catalog=Demo;Persist Security Info=True;
User ID=sa;Password=password;MultipleActiveResultSets=True";
"logFilePath"="C:\Logs\CacheProvider"}

CLR Can't find a type, even though it's in the same assembly

I'm troubleshooting a WCF problem in our application, and turned on WCF tracing with the WCF Service Configuration Editor in VS 2010, which added the following sharedListener to the web.config:
<sharedListeners>
<add initializeData="D:\Logs\CRCCustomerService\Web_tracelog.svclog"
type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
<filter type="" />
</add>
</sharedListeners>
Now, when I take steps to reproduce my error, I'm getting an exception thrown from the System.Diagostics library saying that it can't find XmlWriterTraceListener:
Stack Trace:
System.TypeInitializationException: The type initializer for 'System.ServiceModel.DiagnosticUtility' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Couldn't find type for class System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
at System.Diagnostics.TraceUtils.GetRuntimeObject(String className, Type baseType, String initializeData)
at System.Diagnostics.TypedElement.BaseGetRuntimeObject()
at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElement.GetRuntimeObject()
at System.Diagnostics.ListenerElementsCollection.GetRuntimeObject()
at System.Diagnostics.TraceSource.Initialize()
at System.Diagnostics.TraceSource.get_Listeners()
And it goes on for several more lines, but this establishes the context.
My question is, since, according to ObjectBrowser and Reflector, XmlWriterTraceListener is a member class of the same assembly that System.Diagnostics is, how can it NOT not be able to find it?
And secondly, since every single line in the exception stack comes from system code, how on earth do I begin to debug the root cause here?
Are you using .NET 4.0?
If not, you have to beware the service config utility. It will hard code the version of the listener to 4.0 and you'll need to manually change your web.config

System.Collections.Generic.IEnumerable

when trying to query my database with entityframework i get the following error:
The navigation property of type 'System.Collections.Generic.IEnumerable1[[TASK.Warehouses.Domain.NoteProducts.NoteProduct, TASK.Warehouses.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' is not a single implementation of 'System.Collections.Generic.ICollection1[T]'.
what could be the problem and how could i fix it
You wrote Code-First classes that have IEnumerable<T> navigation properties.
Entity Framework requires that your navigation properties be of type ICollection<T>.
Sounds like you have many classes named "TASK.Warehouses.Domain.NoteProducts.NoteProduct, , TASK.Warehouses.Domain]. If this is the case, you are confusing CLR with violations of OO.

System.ComponentModel.DataAnnotations MethodAccessException in Automated Controller Unit Test, MVC

First of all I'd like to say that I wouldn't doubt if this is a configuration error, because it's only happening on our CI build server and not on anyone's local development machine. So if anyone has a seemingly obvious configuration suggestion please feel free to point it out, my team doesn't configure the build server so there could be something simply not set up correctly.
I'm running into an issue with one of my automated tests, I'm having this issue in any test that involves the (System.ComponentModel.)DataAnnotations on the Entity that is being used. It seems to only occur with any test that involves the Controller calling TryUpdateModel.
Before I provide the entire error message, here’s a list of some of the things we’ve already tried:
Adding the main MVC assemblies to the “fullTrustAssemblies” section of the app.config of the Test project
Adding the System.ComponentModel.DataAnnotations assembly to the “partialTrustVisibleAssemblies” section of the app.config of the Test project
Adding the System.ComponentModel.DataAnnotations assembly to the AppDomainSetup PartialTrustVisibleAssemblies in code
Trying to use ReflectionPermission.Demand to see if there was a problem with permissions
Ensuring that .NET Framework 4 and MVC3 were installed on the machine
Checking for the registry key(s) talked about in this article
Here's the full error:
Test method qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState threw exception:
System.MethodAccessException: Attempt by security transparent method 'System.Web.Mvc.TypeDescriptorHelper.Get(System.Type)' to access security critical method 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider..ctor(System.Type)' failed.
Assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain
Test method qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState threw exception:
System.MethodAccessException: Attempt by security transparent method 'System.Web.Mvc.TypeDescriptorHelper.Get(System.Type)' to access security critical method 'System.ComponentModel.DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider..ctor(System.Type)' failed.
Assembly 'System.ComponentModel.DataAnnotations, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is a conditionally APTCA assembly which is not enabled in the current AppDomain. To enable this assembly to be used by partial trust or security transparent code, please add assembly name 'System.ComponentModel.DataAnnotations, PublicKey=0024000004800000940000000602000000240000525341310004000001000100B5FC90E7027F67871E773A8FDE8938C81DD402BA65B9201D60593E96C492651E889CC13F1415EBB53FAC1131AE0BD333C5EE6021672D9718EA31A8AEBD0DA0072F25D87DBA6FC90FFD598ED4DA35E44C398C454307E8E33B8426143DAEC9F596836F97C8F74750E5975C64E2189F45DEF46B2A2B1247ADC3652BF5C308055DA9' to the the PartialTrustVisibleAssemblies list when creating the AppDomain.<br> at System.Web.Mvc.TypeDescriptorHelper.Get(Type type)
at System.Web.Mvc.ModelBinders.GetBinderFromAttributes(Type type, Func`1 errorMessageAccessor)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, IModelBinder fallbackBinder)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, Boolean fallbackToDefault)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at qTrade.UI.Web.Controllers.Maintenance.TypeLookupController.Create(FormCollection collection) in d:\cibuild\qTrade\qTradeCI\Sources\source\UI\qTrade.UI.Web\Controllers\Maintenance\TypeLookupController.cs:line 55
at qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState() in d:\cibuild\qTrade\qTradeCI\Sources\source\Tests\qTrade.UnitTests\Web\Controllers\Maintenance\TypeLookupControllerTest.cs:line 178
at System.Web.Mvc.TypeDescriptorHelper.Get(Type type)
at System.Web.Mvc.ModelBinders.GetBinderFromAttributes(Type type, Func`1 errorMessageAccessor)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, IModelBinder fallbackBinder)
at System.Web.Mvc.ModelBinderDictionary.GetBinder(Type modelType, Boolean fallbackToDefault)
at System.Web.Mvc.Controller.TryUpdateModel[TModel](TModel model, String prefix, String[] includeProperties, String[] excludeProperties, IValueProvider valueProvider)
at qTrade.UI.Web.Controllers.Maintenance.TypeLookupController.Create(FormCollection collection) in d:\cibuild\qTrade\qTradeCI\Sources\source\UI\qTrade.UI.Web\Controllers\Maintenance\TypeLookupController.cs:line 55
at qTrade.UnitTests.Web.Controllers.Maintenance.TypeLookupControllerTest.CreatePostSaveInvalidEntityAndModelState() in d:\cibuild\qTrade\qTradeCI\Sources\source\Tests\qTrade.UnitTests\Web\Controllers\Maintenance\TypeLookupControllerTest.cs:line 178
Any suggestions would be greatly appreciated, thanks!
I Fixed the problem !
This is a combination of running in Medium Trust, having MVC installed in the GAC and using "dynamic" (or a shape) as a ViewModel. Only (and only if) these 3 conditions are true you run into this error. The solution is to change your controller code: instead of returning "View(model)", return "View((object)model)".

Why do I need nhibernate.dll referenced in my asp.net mvc project?

All my data logic is in another data layer project that I reference in my asp.net mvc project.
Now if I call:
CategoryDAO.GetById(1);
Everything works fine.
If I call:
CategoryDAO.GetBlah(1);
VS.NET complains saying
"Error 102: The type 'NHibernate.Criterion.Order' is defined in an assembly
that is not referenced. You must add a reference to assembly 'NHibernate,
Version=2.1.2.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4'."
If I add nhibernate.dll as a reference in my web project, it works.
Why is this? The logic/code is in my data layer, it is return the same as the call to GetById(1), why do I need to add the reference?
I'm guessing it's because of the lazy-loading that comes build into your objects. NHibernate creates proxy objects that inherit from your class (which is why you need to declare your methods as virtual). I'm guessing the reference is needed because of the proxy objects.

Resources