Note that there are some explanatory texts on larger screens.

plurals
  1. POIntermittent "Specified cast is invalid" with StructureMap injected data context
    primarykey
    data
    text
    <p>I am intermittently getting an <code>System.InvalidCastException: Specified cast is not valid.</code> error in my repository layer when performing an abstracted <code>SELECT</code> query mapped with LINQ. </p> <p>The error can't be caused by a mismatched database schema since it works intermittently and it's on my local dev machine.</p> <p>Could it be because StructureMap is caching the data context between page requests? If so, how do I tell StructureMap v2.6.1 to inject a new data context argument into my repository for each request?</p> <p><strong>Update:</strong> I found <a href="https://stackoverflow.com/questions/318716/asp-net-mvc-iis6-error-on-high-traffic-specified-cast-is-not-valid">this question</a> which correlates my hunch that something was being re-used. Looks like I need to call Dispose on my injected data context. Not sure how I'm going to do this to <em>all</em> my repositories without copypasting a lot of code.</p> <p><strong>Edit</strong>: These errors are popping up all over the place whenever I refresh my local machine too quickly. Doesn't look like it's happening on my remote deployment box, but I can't be sure.</p> <p>I changed all my repositories' StructureMap life cycles to <code>HttpContextScoped()</code> and the error persists.</p> <h2>Code:</h2> <pre><code>public ActionResult Index() { // error happens here, which queries my page repository var page = _branchService.GetPage("welcome"); if (page != null) ViewData["Welcome"] = page.Body; ... } </code></pre> <h2>Repository:</h2> <p>GetPage boils down to a filtered query mapping in my page repository.</p> <pre><code>public IQueryable&lt;Page&gt; GetPages() { var pages = from p in _db.Pages let categories = GetPageCategories(p.PageId) let revisions = GetRevisions(p.PageId) select new Page { ID = p.PageId, UserID = p.UserId, Slug = p.Slug, Title = p.Title, Description = p.Description, Body = p.Text, Date = p.Date, IsPublished = p.IsPublished, Categories = new LazyList&lt;Category&gt;(categories), Revisions = new LazyList&lt;PageRevision&gt;(revisions) }; return pages; } </code></pre> <p>where _db is an injected data context as an argument, stored in a private variable which I reuse for SELECT queries.</p> <h2>Calling code:</h2> <pre><code>public Page GetPage(string slug) { return _pageRepository.GetPages() .WithSlug(slug).FirstOrDefault(); } </code></pre> <p><code>WithSlug</code> is just a pipeline filter that adds a where clause to the query.</p> <h2>Error:</h2> <pre><code>Specified cast is not valid. Exception Details: System.InvalidCastException: Specified cast is not valid. </code></pre> <h2>Stack Trace:</h2> <pre><code>[InvalidCastException: Specified cast is not valid.] System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500 System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +50 System.Linq.Queryable.FirstOrDefault(IQueryable`1 source) +383 Manager.Controllers.SiteController.Index() in C:\Projects\Manager\Manager\Controllers\SiteController.cs:68 lambda_method(Closure , ControllerBase , Object[] ) +79 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +258 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 System.Web.Mvc.&lt;&gt;c__DisplayClassd.&lt;InvokeActionMethodWithFilters&gt;b__a() +125 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +640 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +312 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +709 System.Web.Mvc.Controller.ExecuteCore() +162 System.Web.Mvc.&lt;&gt;c__DisplayClass8.&lt;BeginProcessRequest&gt;b__4() +58 System.Web.Mvc.Async.&lt;&gt;c__DisplayClass1.&lt;MakeVoidDelegate&gt;b__0() +20 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +371 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload