简体   繁体   English

如何调试ServiceStack服务方法?

[英]How can I debug a ServiceStack service method?

I am using the JsonServiceClient to test a self-hosted service to the database. 我正在使用JsonServiceClient来测试对数据库的自托管服务。 The client is receiving an empty array without the 8 records that are in the database. 客户端收到的空数组中没有数据库中的8条记录。 My problem is that if I put a breakpoint in the Get method of the service, it never fires. 我的问题是,如果在服务的Get方法中放置一个断点,它将永远不会触发。

Here is the code for the get method: 这是get方法的代码:

    public PracticesResponse Get(PracticesGetRequest request)
    {
        var parm = Request.QueryString.Get("source");
        var source = Uri.UnescapeDataString(parm);

        var response = new PracticesResponse
        {
            Practices = Repo.GetBySource(source).ToArray()
        };
        return response;
    }

If I put a breakpoint anywhere in the code, it never fires, so I can't tell whether my query parameter isn't being processed correctly, or my repository isn't getting wired up, or what. 如果我在代码中的任何地方放置一个断点,它将永远不会触发,因此我无法判断我的查询参数是否未正确处理,或者我的存储库没有连接好,或者发生了什么。 If I break execution on the line of code that calls the JsonServiceClient get method and attempt to step into it here: 如果我中断了调用JsonServiceClient get方法的代码行的执行,并尝试在此处执行以下操作:

var dto = Client.Get<PracticesResponse>("/practices?source=Desire%20%Map");
Assert.That(dto.Practices.Length > 7);

the debugger processes the call without stepping in and the next break is on the Assert. 调试器将在不介入的情况下处理调用,而下一个中断将在Assert上进行。

Here is a link to simple Visual Studio application that exhibits exactly the same behavior when I try to step into the JsonClient.Get method. 这是指向简单Visual Studio应用程序的链接,当我尝试进入JsonClient.Get方法时,该应用程序表现出完全相同的行为。

How can I get inside this method to see what is going on? 我如何进入该方法以查看发生了什么?

If the breakpoint never gets hit in debug mode than either the code you think is getting fired is not being executed or the VS.NET debugger is not properly hooked up. 如果断点从未在调试模式下命中,则可能是您认为要触发的代码未执行或VS.NET调试器未正确连接。 But this is normally only an issue in ASP.NET applications where you need to restart the AppDomain by "touching" and resaving the Web.config or can also be that the source code is out-of-sync with what's being debugged (normally an issue with trying to debug compiled .dlls with out-of-sync .pdb's). 但这通常只是ASP.NET应用程序中的一个问题,在该应用程序中,您需要通过“触摸”并重新保存Web.config来重新启动AppDomain,或者也可能是源代码与所调试的内容不同步(通常是尝试使用不同步的.pdb调试已编译的.dll的问题)。

Since this is a self-hosted my guess is that the code is just not getting hit. 由于这是自托管的,所以我的猜测是代码没有被使用。 Can you expand your question to provide the full Request DTO with any custom routes defined, the code you're using to call the Service as well as the raw HTTP Request / Response (which you can get with Fiddler) which will show what's happening underneath. 您是否可以扩展您的问题,以向完整的Request DTO提供定义的任何自定义路由,用于调用服务的代码以及原始的HTTP Request / Response(您可以从Fiddler那里获得),从而显示出底层情况。 Also double-check to make sure that your Service is inheriting the Service class or implements IService . 还要仔细检查以确保您的Service继承了Service类或实现了IService

On Service design I would rename the Request DTO into something more readable like GetPractices and instead of manually fetching the parameter from the QueryString to just read it from the Request DTO, also if you're only going to specify the return type in one place it's better that it's annotated on the Request DTO that way the return type information is also available on the client, eg: 在服务设计中,我会将Request DTO重命名为更易读的名称,例如GetPractices ,而不是从QueryString手动获取参数以仅从Request DTO读取参数,即使您只打算在一个地方指定返回类型,更好的是,它在请求DTO上进行了注释,以便在客户端上也可以使用返回类型信息,例如:

PracticesResponse response = client.Get(new GetPractices { Source = src });

And I would re-write the Server to something like: 然后我将服务器重写为:

public class GetPractices : IReturn<PracticesResponse> 
{
    public string Source { get; set; }
}

public object Get(GetPractices request)
{
    return new PracticesResponse {
        Practices = Repo.GetBySource(request.Source)
    };
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM