Как подключить внедрение зависимостей для функции Azure Python v2Python

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Как подключить внедрение зависимостей для функции Azure Python v2

Сообщение Anonymous »


I am a .net developer recently started using python, I am trying to create a Azure function v2 using python and looking for help on how to inject dependencies into my function. I looked at various examples of python dependency injection and able to do that in general. But I couldn't to figure it out where to call my bootstraping.

In .net this can done simply by extending FunctionStarup class, I cannot find such thing in python azure-functions package. Microsoft documentation also doesn't mentioned anything about Dependency Injection in python function v2 programming guide python developer guidance for azure function v2

This is partial code snippet of sample I tried but it didn't work. I used kink for DI in below example

Class I am trying to Inject

class IocService: def __init__(self, message: str): self.message = message def say_hello(self, name: str) -> str: return f"{self.message}, {name}!" #Function definition using blueprint

from kink import inject import logging from IocService import IocService import azure.functions as func bp = func.Blueprint() @inject @bp.route(route="default_template") def default_template(req: func.HttpRequest, context: func.Context, service: IocService) -> func.HttpResponse: logging.info('Python HTTP trigger function processed a request.') #ioc_service = context.get_service(IocService) name = req.params.get('name') if not name: try: req_body = req.get_json() except ValueError: pass else: name = req_body.get('name') if name: return func.HttpResponse( f"Hello, {name}. This HTTP-triggered function " f"executed successfully.") else: return func.HttpResponse( "This HTTP-triggered function executed successfully. " "Pass a name in the query string or in the request body for a" " personalized response.", status_code=200 ) function_app.py

import azure.functions as func from http_blueprint import bp from bootstrap import start_up print("before start up") start_up() print("After start up") app = func.FunctionApp() app.register_functions(bp) Here below is the error message

> File "\function_app.py", line 2, in > from http_blueprint import bp File "\http_blueprint.py", line 9, in > @inject > ^^^^^^ File "\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\kink\inject.py", > line 203, in inject > return _decorator(_service) > ^^^^^^^^^^^^^^^^^^^^ File "\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\kink\inject.py", > line 193, in _decorator > service_function = _decorate(bind or {}, _service, container) > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File > "\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\kink\inject.py", > line 105, in _decorate > service in [ABC.__init__, _no_init] or service.__name__ == "_no_init" > > https://learn.microsoft.com/en-us/azure ... decorators

When i run the function by removing injection from httptrigger method, the logs are showing very interesting things:
  • It printed the logs in the function
  • Then printed the logs in function_app.py


Источник: https://stackoverflow.com/questions/774 ... -python-v2
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»