Thursday, April 11, 2013

WCF Data Service: The type initializer for 'System.Data.Services.Client.TypeSystem' threw an exception.

In an application, we are consuming an OData feed. In development, this worked without any issue but the moment we deployed the application to our test environment, it started to fail with the following exception:

The type initializer for 'System.Data.Services.Client.TypeSystem' threw an exception.

Looking in the logs on the server, we found the following stacktrace:

Sync.exe Information: 0 : Error occured during synchronization: The type initializer for 'System.Data.Services.Client.TypeSystem' threw an exception.
Sync.exe Information: 0 : Stacktrace:    at System.Data.Services.Client.TypeSystem.IsPrivate(PropertyInfo pi)
   at System.Data.Services.Client.ResourceBinder.PatternRules.MatchNonPrivateReadableProperty(Expression e, PropertyInfo& propInfo, Expression& target)
   at System.Data.Services.Client.ResourceBinder.VisitMemberAccess(MemberExpression m)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ALinqExpressionVisitor.VisitBinary(BinaryExpression b)
   at System.Data.Services.Client.ResourceBinder.VisitBinary(BinaryExpression b)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ALinqExpressionVisitor.VisitLambda(LambdaExpression lambda)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ALinqExpressionVisitor.VisitUnary(UnaryExpression u)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ALinqExpressionVisitor.VisitExpressionList(ReadOnlyCollection`1 original)
   at System.Data.Services.Client.ALinqExpressionVisitor.VisitMethodCall(MethodCallExpression m)
   at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ResourceBinder.AnalyzeProjection(MethodCallExpression mce, SequenceMethod sequenceMethod, Expression& e)
   at System.Data.Services.Client.ResourceBinder.VisitMethodCall(MethodCallExpression mce)
   at System.Data.Services.Client.ALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.DataServiceALinqExpressionVisitor.Visit(Expression exp)
   at System.Data.Services.Client.ResourceBinder.Bind(Expression e, DataServiceContext context)
   at System.Data.Services.Client.DataServiceQueryProvider.Translate(Expression e)
   at System.Data.Services.Client.DataServiceQuery`1.Execute()
   at System.Data.Services.Client.DataServiceQuery`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Sync.Synchronizer.GetChangedPortfolios(DateTime lastSyncDate)
   at Sync.Synchronizer.Start(DateTime lastSyncDate)
   at Sync.Program.StartSynchronization(DateTime lastSyncDate)
   at Sync.Program.Main(String[] args)

There are 2 things you need to check if you see this error:

  1. Are all required assemblies copied to the server? (In our case Copy Local was set to false for some assemblies). You’ll need the following assemblies to be copied:
    • System.Spatial.dll
    • Microsoft.Data.Edm.dll
    • Microsoft.Data.OData.dll
    • Microsoft.Data.Services.dll
    • Microsoft.Data.Services.Client.dll
  2. Do all these assemblies have the same version number? (In our case, one project contained a newer version of System.Spatial.dll)

3 comments:

Derick Empey said...

Thank you for this. It saved my life today.

Edgar Knapp said...

Check for the Copy Local attribute as well in case you see the following error upon deployment of a WCF data service:

The type 'TetraSubscribersService.TetraSubscribersService', provided as the Service attribute value in the ServiceHost directive, or provided in the configuration element system.serviceModel/serviceHostingEnvironment/serviceActivations could not be found.

Unfortunately, it took me hours to find out I needed to correct this.

Auri Rahimzadeh said...

Helpful, thanks!