【问题标题】:ServiceStack 3.9.17.0 Implementing IAsyncServiceServiceStack 3.9.17.0 实现 IAsyncService
【发布时间】:2015-05-08 08:00:23
【问题描述】:

我正在尝试使用 ServiceStack 3.9.17.0 实现 IAsyncService 接口。

这是我的服务定义的代码:

public class TestService : IAsyncService<int>
{
    object IAsyncService<int>.ExecuteAsync(int request)
    {
        return "Yup that worked";
    }
}

这是来自我的 Global.asax.cs 的代码:

public class Global : System.Web.HttpApplication
{
    public class TestServiceAppHost : AppHostBase
    {
        public TestServiceAppHost() : base("Test Async Web Services", typeof(TestService).Assembly) { }

        public override void Configure(Funq.Container container)
        {
            Routes.Add<int>("/Test");   
        }
    }
}

当我运行并转到元数据页面时,我看到该项目中存在的其他 2 个服务仅实现了 IService(已删除以保持样本清洁),但我的 IAsyncService 不显示,如果我尝试点击它,我得到以下消息:

<Int32Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="">
    <ResponseStatus>
        <ErrorCode>KeyNotFoundException</ErrorCode>
        <Message>The given key was not present in the dictionary.</Message>
        <StackTrace>
                at System.Collections.Generic.Dictionary`2.get_Item(TKey key) 
                at ServiceStack.WebHost.Endpoints.Utils.FilterAttributeCache.GetRequestFilterAttributes(Type requestDtoType) 
                at ServiceStack.WebHost.Endpoints.EndpointHost.ApplyRequestFilters(IHttpRequest httpReq, IHttpResponse httpRes, Object requestDto) 
                at ServiceStack.WebHost.Endpoints.RestHandler.ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes, String operationName)
         </StackTrace>
    </ResponseStatus>
</Int32Response>

我们将不胜感激。

编辑:

根据mythz 的建议,我已将代码更新为如下所示(再次感谢您的回复)。

DTO 和服务:

[Route("/test")]
public class TestDTO
{
    public int request { get; set; }
}

public class TestService : IAsyncService<TestDTO>
{
    object IAsyncService<TestDTO>.ExecuteAsync(TestDTO request)
    {
        return "Yup that worked";
    }
}

我让 Global.asax.cs 保持不变,只是我将路由更改为使用 DTO 并使路由小写以匹配 DTO 上的属性。我仍然有同样的问题。

编辑#2: 我升级到 v3.9.71 仍然遇到同样的问题。

【问题讨论】:

    标签: c# servicestack servicestack-bsd


    【解决方案1】:

    ServiceStack 服务需要一个 Request DTO,即一个具体的 POCO 类。您不能使用 int 作为请求 DTO。如果需要,可以将其包装在 POCO 类中,例如:

    [Route("/test")]
    public class Request 
    {
        public int Int { get; set; }
    }
    

    【讨论】:

    • 嘿,mythz,感谢您的回复,但是添加 DTO 并没有改变任何东西。我将更新 OP 以获得最新的代码集。
    • @AdamHarris 您是否也从 Global.asax 中删除了 Route.Add&lt;int&gt;?您不应该将路线注册两次。还有v3.9.17真的老了,考虑升级到最新的ServiceStack v3
    • 我将它切换到 Route.Add。我知道它是一个超级旧的版本,但它是最后一个免费版本,管理层不会为许可证付费sigh。至于注册 Route 两次,我应该将属性保留在 DTO 上并从 Global.asax 中删除一个,反之亦然,还是两者都保留?
    • @AdamHarris 所有 v3 版本都是 BSD/free - v3.9.71 是最后一个免费版本,请参阅上面的链接以获取安装说明。我只会在 DTO 上声明路由,因为它们也可以在客户端上使用。
    • 嘿@mythz,我将尝试更新 ServiceStack,看看是否有帮助。感谢您指出这一点。
    猜你喜欢
    • 1970-01-01
    • 2017-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-04
    相关资源
    最近更新 更多