ServiceStack:非阻塞请求处理

时间:2013-06-06 21:18:52

标签: .net web-services servicestack nonblocking

我正在寻找一种基于ServiceStack框架处理服务中的非阻塞请求的方法。所以我看到有AppHostHttpListenerLongRunningBase类(我现在需要一个自托管应用程序)但是没有任何好的例子如何使用这个类。

让我们看一个简单的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;

using ServiceStack.ServiceHost;
using ServiceStack.ServiceInterface;
using ServiceStack.WebHost.Endpoints;


public class Hello
{
    public String Name { get; set; }
}

public class HelloResponse
{
    public String Result { get; set; }
}

public class HelloService : Service
{
    public Object Any(Hello request)
    {
        //Emulate a long operation
        Thread.Sleep(10000);
        return new HelloResponse { Result = "Message from " + request.Name };
    }
}

public class HelloAppHost : AppHostHttpListenerLongRunningBase 
{
    public HelloAppHost()
        : base("Hello App Services", typeof(HelloService).Assembly)
    {
    }

    public override void Configure(Funq.Container container)
    {
        Routes
            .Add<Hello>("/hello")
            .Add<Hello>("/hello/{Name}");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var appHost = new HelloAppHost();
        appHost.Init();
        appHost.Start("http://127.0.0.1:8080/");
        Console.ReadLine();
    }
}

因此,如果我运行应用程序并发出两个请求,它们将以串行模式执行,并且响应之间会有大约10秒的延迟。那么有没有办法执行非阻塞请求(如果有自主应用程序解决方案,则更好)。

P.S。:我知道有一个基于Redis的解决方案,但由于某些原因它不适合。

1 个答案:

答案 0 :(得分:1)

thread.sleep是造成10秒延迟的原因。 Web服务不一定是多线程应用程序。我发现的是通过缓存常见的响应来快速做出响应,并且你的等待时间几乎和你当前的线程睡眠一样长。