无法将ServiceBusTrigger BrokeredMessage成员绑定到Azure WebJobs SDK中的Blob blobpath

时间:2016-12-30 16:13:26

标签: c# azure-storage-blobs azure-webjobs azure-webjobssdk azure-servicebus-queues

我无法使用Azure WebJobs SDK添加Service Bus BrokeredMessage的任何成员的动态绑定。

不工作

// 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}\r\nBlob {json}");
            Console.WriteLine($"Content(Id) {q.Label}\r\nBlob {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}\r\nBlob {json}");
            Console.WriteLine($"Content(Id) {q.JsonFilename}\r\nBlob {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}\r\nBlob {json}");
            Console.WriteLine($"Content(Id) {q.JsonFilename}\r\nBlob {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();

1 个答案:

答案 0 :(得分:0)

正如您所发现的,在绑定到定义这些成员的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);
    }