It’s easy enough to send messages to an Event Hub using the EventHubs NuGet package as specified here, but if you want to test it by making you’re own HTTP POST requests to the endpoint then it becomes a bit trickier.
There’s some Microsoft documentation here about manually sending HTTP requests to the Event Hub but what it doesn’t mention is how to generate the SharedAccessSignature needed for the Authorization header.
After a bit of investigation I discovered this post about generating the signature but unfortunately it uses the NuGet package Microsoft.ServiceBus which is .NET Framework only and the new Microsoft.Azure.ServiceBus package doesn’t contain all the same methods.
Happily, after a bit of work I was able to create an EventHubSASToken static class for use in generating the required Shared Access Signature. I created a web interface for demonstrating this here and the code for it is here.
EventHubSASToken.cs
public static class EventHubSASToken
{
private static TokenProvider CreateTokenProvider(string senderKeyName, string senderKey, TimeSpan tokenTimeToLive)
{
return TokenProvider.CreateSharedAccessSignatureTokenProvider(senderKeyName, senderKey, tokenTimeToLive);
}
private static Uri CreateServiceUri(string serviceNamespace, string hubName, string publisherName)
{
if (string.IsNullOrEmpty(publisherName))
{
return CreateServiceUri(serviceNamespace, hubName);
}
else
{
return new Uri(string.Format("https://{0}.servicebus.windows.net/{1}/publishers/{2}/messages", serviceNamespace, hubName, publisherName));
}
}
private static Uri CreateServiceUri(string serviceNamespace, string hubName)
{
return new Uri(string.Format("https://{0}.servicebus.windows.net/{1}/messages", serviceNamespace, hubName));
}
public static SecurityToken CreateSecurityToken(string senderKeyName, string senderKey, string serviceNamespace, string hubName, string publisherName, TimeSpan tokenTimeToLive)
{
TokenProvider tokenProvider = CreateTokenProvider(senderKeyName, senderKey, tokenTimeToLive);
Uri serviceUri = CreateServiceUri(serviceNamespace, hubName, publisherName);
return tokenProvider.GetTokenAsync(serviceUri.ToString(), tokenTimeToLive).Result;
}
public static SecurityToken CreateSecurityToken(string senderKeyName, string senderKey, string serviceNamespace, string hubName, TimeSpan tokenTimeToLive)
{
TokenProvider tokenProvider = CreateTokenProvider(senderKeyName, senderKey, tokenTimeToLive);
Uri serviceUri = CreateServiceUri(serviceNamespace, hubName);
return tokenProvider.GetTokenAsync(serviceUri.ToString(), tokenTimeToLive).Result;
}
public static async Task<SecurityToken> CreateSecurityTokenAsync(string senderKeyName, string senderKey, string serviceNamespace, string hubName, string publisherName, TimeSpan tokenTimeToLive)
{
TokenProvider tokenProvider = CreateTokenProvider(senderKeyName, senderKey, tokenTimeToLive);
Uri serviceUri = CreateServiceUri(serviceNamespace, hubName, publisherName);
return await tokenProvider.GetTokenAsync(serviceUri.ToString(), tokenTimeToLive);
}
public static async Task<SecurityToken> CreateSecurityTokenAsync(string senderKeyName, string senderKey, string serviceNamespace, string hubName, TimeSpan tokenTimeToLive)
{
TokenProvider tokenProvider = CreateTokenProvider(senderKeyName, senderKey, tokenTimeToLive);
Uri serviceUri = CreateServiceUri(serviceNamespace, hubName);
return await tokenProvider.GetTokenAsync(serviceUri.ToString(), tokenTimeToLive);
}
}
0 Comments