Skip to content

Invocation

MiNG edited this page Jan 28, 2019 · 3 revisions

调用接口

SDK 中所有的接口调用均采用 request-response 模式,入参均为 XxxRequest 对象且返回 IResponse<XxxResult>。

异步调用(推荐)

使用扩展方法(推荐)

在简单入参的接口上,SDK 添加了扩展方法将简单的请求对象的属性展开到方法入参中,以便调用者使用。

扩展方法详情见:LogServiceClientExtensions

using System;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Infrastructure.Protocol;

namespace Examples
{
    static class Program
    {
        static async Task<IResponse> Invoke(ILogServiceClient client)
        {
            return await client.GetLogsAsync
            (
                // 「必填参数」作为方法的普通必须参数
                "example-logstore",
                DateTimeOffset.MinValue,
                DateTimeOffset.UtcNow,

                // 「可选参数」作为方法的可选参数,可通过命名参数方式指定
                offset: 1,
                line: 10
            );
        }
    }
}

使用原始接口

原始接口总是接受 XxxRequest 对象作为入参。 使用对象作为入参可以方便地在调用链路上传递方法的所有参数。

using System;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Domain.Log;
using Aliyun.Api.LogService.Infrastructure.Protocol;

namespace Examples
{
    static class Program
    {
        static async Task<IResponse> Invoke(ILogServiceClient client)
        {
            return await client.GetLogsAsync
            (
                // 「必填参数」会在 Request 构造器中列出,并且不可set;
                new GetLogsRequest("example-logstore", beginTime, endTime)
                {
                    // 「可选参数」不会在 Request 构造器中列出,可通过 setter 设置。
                    Offset = 1,
                    Line = 100,
                }
            );
        }
    }
}

通过 TPL 调用

async/await 语法只在 C# 5.0 以上受到支持,低于此版本的开发或基于高级使用场景的原因,可通过 TPL 形式调用 SDK 方法。

注:TPL - Task Parallel Library,只在 .NET Framework 4.0 以上受到支持, .NET Core 所有版本均支持。

using System;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Infrastructure.Protocol;

namespace Examples
{
    static class Program
    {
        static async Task<IResponse> InvokeWithTpl(ILogServiceClient client)
        {
            return client
                .GetLogsAsync("example-logstore", DateTimeOffset.MinValue, DateTimeOffset.UtcNow)
                .ContinueWith(r => r.Result as IResponse);
        }
    }
}

同步调用(不建议使用)

在部分本地调试或单元测试场景中,可能为了方便调试过程,会使用同步方式调用 SDK。

using System;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Infrastructure.Protocol;

namespace Examples
{
    static class Program
    {
        static IResponse InvokeSynchronously(ILogServiceClient client)
        {
            return client
                .GetLogsAsync("example-logstore", DateTimeOffset.MinValue, DateTimeOffset.UtcNow)
                .Result; // 访问 `Result` 属性会阻塞当前线程直到取得结果。
        }
    }
}

同步调用死锁

在 WinForm/ASP.NET 环境中,由于设置了 SynchronizationContext,所有异步后续操作(Continuation)都会被回传到(SynchronizationContext.Post)调用线程上执行,此时不能直接阻塞请求,否则会造成当前线程无限等待。

详情请见:http://blog.stephencleary.com/2012/07/dont-block-on-async-code.html

在此场景下,需要借助 TPL 在其他线程上进行等待操作。

using System;
using System.Threading.Tasks;
using Aliyun.Api.LogService;
using Aliyun.Api.LogService.Infrastructure.Protocol;

namespace Examples
{
    static class Program
    {
        static IResponse InvokeSynchronouslyInAspNet(ILogServiceClient client)
        {
            var asyncTask = client.GetLogsAsync("example-logstore", DateTimeOffset.MinValue, DateTimeOffset.UtcNow);

            var response = Task.Run(() => asyncTask.Result).Result;

            return response;
        }
    }
}

Clone this wiki locally