无法将 ServiceBusTrigger BrokeredMessage 成员绑定到 Azure WebJobs SD



我无法使用 Azure WebJobs SDK 添加服务总线代理消息的任何成员的动态绑定。

不工作

// using properties on BrokeredMessage object
public static void ProcessDup([ServiceBusTrigger("dup")] **BrokeredMessage** q, [Blob("dup/{Label}")] string json, TextWriter log)
{
log.WriteLine($"Content(Id) {q.Label}rnBlob {json}");
Console.WriteLine($"Content(Id) {q.Label}rnBlob {json}");
}       
// using properties inside of the BrokeredMessage.Properties
public static void ProcessDup([ServiceBusTrigger("dup")] **BrokeredMessage** q, [Blob("dup/{JsonFilename}")] string json, TextWriter log)
{
log.WriteLine($"Content(Id) {q.JsonFilename}rnBlob {json}");
Console.WriteLine($"Content(Id) {q.JsonFilename}rnBlob {json}");
}       

它在处理来自服务总线的消息时为我提供以下消息

Function had errors. See Azure WebJobs SDK dashboard for details. Instance ID
is '0a957645-f042-4dca-a38f-643229fbbc21'
Microsoft.Azure.WebJobs.Host.FunctionInvocationException: Exception while execut
ing function: Functions.ProcessDup ---> System.InvalidOperationException: Except
ion binding parameter 'json' ---> System.ArgumentNullException: Value cannot be
null.
Parameter name: bindingData
at Microsoft.Azure.WebJobs.Host.Bindings.BindingDataPathHelper.ConvertParamet
ers(IReadOnlyDictionary`2 bindingData)
at Microsoft.Azure.WebJobs.Host.Blobs.ParameterizedBlobPath.Bind(IReadOnlyDic
tionary`2 bindingData)
at Microsoft.Azure.WebJobs.Host.Blobs.Bindings.BlobBinding.<BindAsync>d__0.Mo
veNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.Azure.WebJobs.Host.Triggers.TriggeredFunctionBinding`1.<BindCore
Async>d__7.MoveNext()
--- End of inner exception stack trace ---
at Microsoft.Azure.WebJobs.Host.Executors.DelayedException.Throw()
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithWatche
rsAsync>d__31.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggin
gAsync>d__2c.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggin
gAsync>d__13.MoveNext()
--- End of inner exception stack trace ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<ExecuteWithLoggin
gAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNot
ification(Task task)
at Microsoft.Azure.WebJobs.Host.Executors.FunctionExecutor.<TryExecuteAsync>d
__1.MoveNext()

确实有效

public static void ProcessDup([ServiceBusTrigger("dup")] **TheDup** q, [Blob("dup/{JsonFilename}")] string json, TextWriter log)
{
log.WriteLine($"Content(Id) {q.JsonFilename}rnBlob {json}");
Console.WriteLine($"Content(Id) {q.JsonFilename}rnBlob {json}");
}       

服务总线消息基于此类

[DataContract(Name = "TheDup", Namespace = "")]
public class TheDup
{
[DataMember]
public Guid Id { get; set; }
public string JsonFilename => $"{Id}.json";
[DataMember]
public string Json { get; set; }
}

服务总线消息添加有

var client = QueueClient.CreateFromConnectionString(_queueConnectionString, _queueName);
var message = new BrokeredMessage(new TheDup() {Id = id, Json = sSourceData?.Length > 128 ? string.Empty : sSourceData})
{
SessionId = sessionid == null ? id.ToString() : sessionid.ToString(),
Label = $"{id}.json"
};
message.Properties.Add(new KeyValuePair<string, object>("JsonFilename", $"{id}.json"));
client.Send(message);
client.Close();

如您所发现的,在绑定到定义这些成员的 POCO 时,只能包含绑定参数(如dup/{JsonFileName})。这是设计使然。

如果这不适合你,并且你确实需要执行动态命令性绑定,则可以使用IBinder在代码中而不是在参数绑定中执行 blob 绑定。下面是一个简单的示例:

public static void BlobIBinder(
[QueueTrigger("persons")] Person persons, 
IBinder binder)
{
var blobAttrib = new BlobAttribute("persons/" + persons.Name + "BlobIBinder");
var writer = binder.Bind<TextWriter>(blobAttrib);
writer.Write("Hello " + persons.Name);
}

最新更新