#python #c# #dll #async-await
#python #c# #dll #асинхронное ожидание
Вопрос:
У меня есть небольшой фрагмент кода на C #, который создает клиент MS Graph API. В VS code он работает, как и ожидалось, теперь, чтобы использовать этот код из Python, я создал DLL и использую эту DLL в Python.
Это фрагмент кода C #-
public static async Task<string> TestMethod()
{
//some variable defination
Console.WriteLine("Before creation of client");
//method call to get the token
GraphServiceClient graphServiceClient_Test = new GraphServiceClient(new DelegateAuthenticationProvider(async (requestMessage) =>
{
Console.WriteLine("inside sync meethod ");
requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
Console.WriteLine("out sync meethod ");
})
);
Console.WriteLine("before message ");
var message = new Message
{
//some message defination
};
Console.WriteLine("before await 2 ");
await graphServiceClient_Test.Me
.SendMail(message, false)
.Request()
.PostAsync();
return "Success";
}
и вот как я вызываю этот код из Python — sendMail — это класс
newfeature = asyncio.run(sendMail.TestMethod())
и вот как выглядит моя консоль python-
Before creation of client
before message
before await 2
inside sync meethod
out sync meethod
Traceback (most recent call last):
File "C:/DLLImportTest/main.py", line 12, in <module>
newfeature = asyncio.run(sendMail.TestMethod())
File "C:UsersusernameAppDataLocalProgramsPythonPython37libasynciorunners.py", line 37, in run
raise ValueError("a coroutine was expected, got {!r}".format(main))
ValueError: a coroutine was expected, got <System.Threading.Tasks.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] object at 0x000001EA0185EEC8>
Process finished with exit code 1
Код ломается, когда я использую клиент для отправки электронной почты. Я совершенно новичок в Python и не уверен, где я ошибаюсь. Может быть, есть другой способ вызвать «ожидаемый» модуль? Любая помощь приветствуется.