加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:09:50 所属栏目:百科 来源:网络整理
导读:在Azure门户中,可以从该功能的“集成”页面轻松配置Azure功能的输出绑定. 这些设置最终进入function.json. 我的问题是,如何从Visual Studio中设置这些值? 代码如下所示: public static class SomeEventProcessor{ [FunctionName("SomeEventProcessor")] pu
在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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读