微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 如何从Visual Studio 2017预览版2指定Azure功能的输出绑定?

在Azure门户中,可以从该功能的“集成”页面轻松配置Azure功能输出绑定.
这些设置最终进入function.json.

我的问题是,如何从Visual Studio中设置这些值?
代码如下所示:

public static class SomeEventProcessor
{
    [FunctionName("SomeEventProcessor")]

    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function,"get","post",Route = null)]HttpRequestMessage req,TraceWriter log,IAsyncCollector<EventInfo> outputQueue)
    {
        log.Info("C# HTTP trigger function processed a request.");

        EventInfo eventInfo = new EventInfo(); //Just a container
        eventInfo.someID = req.Headers.Contains("SomeID") ? req.Headers.GetValues("SomeID").First() : null;

        //Write to a queue and promptly return
        await outputQueue.AddAsync(eventInfo);

        return req.CreateResponse(HttpStatusCode.OK);

    }
}

我想从VS中指定要使用哪个队列和哪个存储,以便我可以控制我的代码和配置.我已经检查了类似的问题,建议的问题等,但没有一个被证明是方便的.

我在用
Visual Studio 2017预览版,15.3.0版预览版3

VS Extension:VS的Azure函数工具,版本0.2

解决方法

绑定被指定为触发器,使用它们应绑定的参数的属性.绑定配置(例如,队列名称,连接等)作为属性参数/属性提供.

以您的代码为例,队列输出绑定如下所示:

public static class SomeEventProcessor
{
    [FunctionName("SomeEventProcessor")]

    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function,"post")]HttpRequestMessage req,[Queue("myQueueName",Connection = "myconnection")] IAsyncCollector<EventInfo> outputQueue)
    {
        log.Info("C# HTTP trigger function processed a request.");

        EventInfo eventInfo = new EventInfo(); //Just a container
        eventInfo.someID = req.Headers.Contains("SomeID") ? req.Headers.GetValues("SomeID").First() : null;

        //Write to a queue and promptly return
        await outputQueue.AddAsync(eventInfo);

        return req.CreateResponse(HttpStatusCode.OK);

    }
}

如果您只是从HTTP函数返回200(确定),则可以通过将该属性应用于方法的返回值来进一步简化代码,再次使用您的代码作为示例,它将如下所示:

[FunctionName("SomeEventProcessor")]
[return: Queue("myQueueName",Connection = "myconnection")]
public static EventInfo Run(
    [HttpTrigger(AuthorizationLevel.Function,TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");

    EventInfo eventInfo = new EventInfo(); //Just a container
    eventInfo.someID = req.Headers.Contains("SomeID") ? req.Headers.GetValues("SomeID").First() : null;

    return eventInfo;
}

使用上面的代码,当您的函数成功时,Azure Functions将自动返回200,如果抛出异常,则返回500.

原文地址:https://www.jb51.cc/csharp/100949.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐