我对 ServiceStack 比较陌生,但我喜欢我目前所看到的。但是,我在这件事上碰壁了-
这就是我的剃须刀模板的样子——
@inherits ViewPage<IChallenge>
@{
foreach (var codeFile in Model.CodeFiles)
{
//do something here
}
}
这里,IChallenge 是一个接口,服务在返回时动态选择该接口的给定实现。该服务被连接以返回接口本身,如下所示 -
public class WebChallenge : IReturn<IChallenge> { /* properties */ }
但是,当我运行代码时出现以下错误 -
无法将类型“ServiceStack.Razor.Compilation.RazorDynamicObject”隐式转换为“IChallenge”。存在显式转换(您是否缺少演员表?)
以此作为完整堆栈的响应-
<?xml version="1.0"?>
<Challenge1Response xmlns="" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<ResponseStatus>
<ErrorCode>RuntimeBinderException</ErrorCode>
<Message>Cannot implicitly convert type 'ServiceStack.Razor.Compilation.RazorDynamicObject' to 'CodeGuru.Exercises.IChallenge'. An explicit conversion exists (are you missing a cast?)</Message>
<StackTrace> 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.<>c__DisplayClass1.<SerializeToStream>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)
</StackTrace>
</ResponseStatus>
</Challenge1Response>
如果我使用特定的实现,这个错误就会消失 - 但是我需要能够发送在运行时选择的任何实现。有任何想法吗?
编辑:
我注意到,如果接口被包装为另一个强类型对象的属性,并且该对象用作视图中的模型,它工作正常,我也可以访问接口属性。这可能是一种解决方法,但我想知道是否有人知道更好的答案,因为这感觉就像跳过篮球。