title: Interface as ServiceStack Razor Model
body: <p>I am relatively new to ServiceStack, but I love what I see so far. However I've come up against a wall on this one - </p>
  
  <p>This is what my razor template looks like - </p>
  
  <pre><code>@inherits ViewPage&lt;IChallenge&gt;
  
  @{
      foreach (var codeFile in Model.CodeFiles)
      {
  //do something here
      }
  }
  </code></pre>
  
  <p>here, IChallenge is an interface, and the service dynamically selects a given implementation of this interface while returning. The service is wired to return Interface itself as so - </p>
  
  <pre><code>public class WebChallenge : IReturn&lt;IChallenge&gt; { /* properties */ }
  </code></pre>
  
  <p>However I get the following error when I run the code - </p>
  
  <p><em>Cannot implicitly convert type 'ServiceStack.Razor.Compilation.RazorDynamicObject' to 'IChallenge'. An explicit conversion exists (are you missing a cast?)</em></p>
  
  <p>with this as the response with the full stack - </p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Challenge1Response xmlns="" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
  &lt;ResponseStatus&gt; 
  &lt;ErrorCode&gt;RuntimeBinderException&lt;/ErrorCode&gt; 
  &lt;Message&gt;Cannot implicitly convert type 'ServiceStack.Razor.Compilation.RazorDynamicObject' to 'CodeGuru.Exercises.IChallenge'. An explicit conversion exists (are you missing a cast?)&lt;/Message&gt; 
  &lt;StackTrace&gt; at CallSite.Target(Closure , CallSite , Object ) 
  at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0) 
  at ServiceStack.Razor.Templating.TemplateBase`1.get_Model() 
  at CompiledRazorTemplates.Dynamic.faaccadc.Execute() 
  at ServiceStack.Razor.Templating.TemplateService.ExecuteTemplate[T](T model, String name, String defaultTemplatePath, IHttpRequest httpReq, IHttpResponse httpRes) 
  at ServiceStack.Razor.RazorFormat.ExecuteTemplate[T](T model, String name, String templatePath, IHttpRequest httpReq, IHttpResponse httpRes) 
  at ServiceStack.Razor.RazorFormat.ProcessRazorPage(IHttpRequest httpReq, ViewPageRef razorPage, Object dto, IHttpResponse httpRes) 
  at ServiceStack.Razor.RazorFormat.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, Object dto) 
  at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.&amp;lt;&amp;gt;c__DisplayClass1.&amp;lt;SerializeToStream&amp;gt;b__0(IViewEngine x) 
  at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate) 
  at ServiceStack.WebHost.Endpoints.Formats.HtmlFormat.SerializeToStream(IRequestContext requestContext, Object response, IHttpResponse httpRes) 
  at ServiceStack.WebHost.Endpoints.Extensions.HttpResponseExtensions.WriteToResponse(IHttpResponse response, Object result, ResponseSerializerDelegate defaultAction, IRequestContext serializerCtx, Byte[] bodyPrefix, Byte[] bodySuffix)
  &lt;/StackTrace&gt; 
  &lt;/ResponseStatus&gt; 
  &lt;/Challenge1Response&gt;
  </code></pre>
  
  <p>This error disappears if I use a specific implementation - however I need to be able to send any implementation selected at runtime. Any ideas?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I noticed that if the Interface is wrapped as a property of another strongly typed object, and that object is used as the model in the view, it works fine and I can access the Interface properties as well. This could be a work-around but I'd like to know if someone knows a better answer, since this feels like jumping through hoops. </p>