Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug fix for stream blob triggers, more docs
- Loading branch information
1 parent
47a4610
commit 8982f25
Showing
10 changed files
with
168 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="us-ascii"?> | ||
<Message xmlns="http://schemas.microsoft.com/servicebusexplorer"> | ||
<Date>05 July 2018 18:11:24</Date> | ||
<Content><![CDATA[<?xml version="1.0" encoding="utf-8"?> | ||
<message>Hi mate, how are you?</message>]]></Content> | ||
</Message> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="us-ascii"?> | ||
<Properties xmlns="http://schemas.microsoft.com/servicebusexplorer"> | ||
<Property Key="MachineName" Type="String" Value="DESKTOP-JB77CF0" /> | ||
<Property Key="UserName" Type="String" Value="JamesRandall" /> | ||
</Properties> |
9 changes: 5 additions & 4 deletions
9
Samples/Scratch/SwaggerBuildOut/Commands/HelloWorldCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,14 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.IO; | ||
using AzureFromTheTrenches.Commanding.Abstractions; | ||
using FunctionMonkey.Commanding.Abstractions; | ||
using SwaggerBuildOut.Commands.Responses; | ||
|
||
namespace SwaggerBuildOut.Commands | ||
{ | ||
public class HelloWorldCommand : ICommand<Message> | ||
public class HelloWorldCommand : ICommand<Message>, IStreamCommand | ||
{ | ||
public Stream Stream { get; set; } | ||
|
||
public string Name { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,134 @@ | ||
# Blobs | ||
|
||
Docs coming soon | ||
Function Monkey supports two approaches for responding to blob storage events: | ||
|
||
1. Deserializing directly into a command from JSON - useful for small blobs who's format is known in advance | ||
2. Assigning a stream to a command that can be picked up in the handler - useful for blobs who's format is not known or that are very large and require careful processing | ||
|
||
## Deserializing Directly Into a Command | ||
|
||
First begin by creating an empty Azure Functions v2 project and then install the core nuget packages for Function Monkey: | ||
|
||
Install-Package FunctionMonkey -pre | ||
Install-Package FunctionMonkey.Compiler -pre | ||
|
||
Now create a folder in the solution called commands and create a class called HelloWorldCommand: | ||
|
||
public class HelloWorldCommand : ICommand | ||
{ | ||
public string UserName { get; set; } | ||
} | ||
|
||
Next create a folder in the solution called Handlers and create a class called HelloWorldCommandHandler: | ||
|
||
internal class HelloWorldCommandHandler : ICommandHandler<HelloWorldCommand> | ||
{ | ||
public Task ExecuteAsync(HelloWorldCommand command) | ||
{ | ||
Debug.WriteLine($"Hello {command.UserName}"); | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
And now we'll create our function app configuration in the root of the project that registers the command handler and registers the command with a storage account and blob container: | ||
|
||
public class FunctionAppConfiguration : IFunctionAppConfiguration | ||
{ | ||
private const string ContainerName = "myblobcontainer"; | ||
private const string StorageConnectionName = "storageConnection"; | ||
|
||
public void Build(IFunctionHostBuilder builder) | ||
{ | ||
builder | ||
.Setup((serviceCollection, commandRegistry) => | ||
commandRegistry.Register<HelloWorldCommandHandler>() | ||
) | ||
.Functions(functions => functions | ||
.Storage(StorageConnectionName, storage => storage | ||
.BlobFunction<HelloWorldCommand>($"{ContainerName}/{name}") | ||
) | ||
); | ||
} | ||
} | ||
|
||
Finally we need to create an entry in local.settings.json for the Azure Storage connection string: | ||
|
||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"AzureWebJobsStorage": "UseDevelopmentStorage=true", | ||
"AzureWebJobsDashboard": "UseDevelopmentStorage=true", | ||
"storageConnection": "<connection string>" | ||
} | ||
} | ||
|
||
And that's it! If you run this sample you should find that adding a blob to the container triggers the function. Note that Azure Functions blob triggers do not work with the storage emulator, you need to use a real storage account. | ||
|
||
## Using Streams | ||
|
||
First begin by creating an empty Azure Functions v2 project and then install the core nuget packages for Function Monkey: | ||
|
||
Install-Package FunctionMonkey -pre | ||
Install-Package FunctionMonkey.Compiler -pre | ||
|
||
Now create a folder in the solution called commands and create a class called HelloWorldCommand - in addition to implementing the _ICommand_ interface we also must implement the _IStreamCommand_: | ||
|
||
public class HelloWorldCommand : ICommand, IStreamCommand | ||
{ | ||
public Stream Stream { get; set; } | ||
|
||
public string Name { get; set; } | ||
} | ||
|
||
Next create a folder in the solution called Handlers and create a class called HelloWorldCommandHandler: | ||
|
||
internal class HelloWorldCommandHandler : ICommandHandler<HelloWorldCommand, Message> | ||
{ | ||
public Task<Message> ExecuteAsync(HelloWorldCommand command, Message previousResult) | ||
{ | ||
using (StreamReader reader = new StreamReader(command.Stream)) | ||
{ | ||
string json = reader.ReadToEnd(); | ||
return Task.FromResult(new Message | ||
{ | ||
Text = $"JSON payload\n{json}" | ||
}); | ||
} | ||
} | ||
} | ||
|
||
Our handler will be provided an open stream to the blob in the _Stream_ property and the name of the blob in the _Name_ property of the command. | ||
|
||
And now we'll create our function app configuration in the root of the project that registers the command handler and registers the command with a storage account and blob container: | ||
|
||
public class FunctionAppConfiguration : IFunctionAppConfiguration | ||
{ | ||
private const string ContainerName = "myblobcontainer"; | ||
private const string StorageConnectionName = "storageConnection"; | ||
|
||
public void Build(IFunctionHostBuilder builder) | ||
{ | ||
builder | ||
.Setup((serviceCollection, commandRegistry) => | ||
commandRegistry.Register<HelloWorldCommandHandler>() | ||
) | ||
.Functions(functions => functions | ||
.Storage(StorageConnectionName, storage => storage | ||
.BlobFunction<HelloWorldCommand>($"{ContainerName}/{name}") | ||
) | ||
); | ||
} | ||
} | ||
|
||
Finally we need to create an entry in local.settings.json for the Azure Storage connection string: | ||
|
||
{ | ||
"IsEncrypted": false, | ||
"Values": { | ||
"AzureWebJobsStorage": "UseDevelopmentStorage=true", | ||
"AzureWebJobsDashboard": "UseDevelopmentStorage=true", | ||
"storageConnection": "<connection string>" | ||
} | ||
} | ||
|
||
And that's it! If you run this sample you should find that adding a blob to the container triggers the function and you're given an open stream. Note that Azure Functions blob triggers do not work with the storage emulator, you need to use a real storage account. |