Skip to content

Instantly share code, notes, and snippets.

View alexjamesbrown's full-sized avatar

Alex Brown alexjamesbrown

View GitHub Profile
public interface ICustomerService
{
int Save(Customer customer);
}
//Arrange
var mockCustomerService = new Mock();
var customerController = new CustomerController(mock.Object);
mockCustomerService.Setup(x=> x.Save(It.IsAny())
.Returns(1);
//Act
customerController.Post("Alex", "Brown");
var customer = new Customer();
customer.FirstName = firstName;
customer.LastName = firstName; //uh oh!
//Arrange
var mockCustomerService = new Mock();
var customerController = new CustomerController(mockCustomerService.Object);
Customer customerServiceSaveArg = null;
mockCustomerService
.Setup(x => x.Save(It.IsAny()))
.Returns(1)
.Callback(c => customerServiceSaveArg = c);
@alexjamesbrown
alexjamesbrown / TopicProcessor.cs
Last active February 23, 2019 22:26
Simple ServiceBusTrigger Function
[FunctionName("TopicProcessor")]
public static async Task Run(
[ServiceBusTrigger("%topic-name%", "%subscription-name%", AccessRights.Manage, Connection = "connection")] BrokeredMessage msg,
ILogger log)
{
var thing = msg.GetBody<Thing>();
var apiClient = new ApiClient();
await apiClient.PostAsync(thing);
}
@alexjamesbrown
alexjamesbrown / TopicProcessorDeadletter.cs
Created February 23, 2019 22:31
Try deadletter message on exception
[FunctionName("TopicProcessor")]
public static async Task Run(
[ServiceBusTrigger("%topic-name%", "%subscription-name%", AccessRights.Manage, Connection = "connection")] BrokeredMessage msg,
ILogger log)
{
var thing = msg.GetBody<Thing>();
var apiClient = new ApiClient();
try {
await apiClient.PostAsync(thing);
}
@alexjamesbrown
alexjamesbrown / ServiceBusTriggerExample.cs
Created February 23, 2019 22:32
Example of ServiceBusTrigger function runtime
var message = ReceiveMessageFromTopic();
try {
//run the code within your function, passing the message as a parameter
RunFunctionCode(message);
}
catch() {
message.Abandon();
}
message.Complete();
@alexjamesbrown
alexjamesbrown / TopicProcessorWithRetry.cs
Created February 23, 2019 22:35
Example of retrying messages by re-queuing
[FunctionName("TopicProcessor")]
public static async Task Run(
[ServiceBusTrigger("%topic-name%", "%subscription-name%", AccessRights.Manage, Connection = "connection")] BrokeredMessage msg,
[ServiceBus("%topic-name%", AccessRights.Manage, Connection = "connection", EntityType = EntityType.Topic)] IAsyncCollector<BrokeredMessage> outputTopic,
ILogger log)
{
var thing = msg.GetBody<Thing>();
try {
var apiClient = new ApiClient();
@alexjamesbrown
alexjamesbrown / applicationConfig.xdt
Created March 14, 2019 12:04
disable urlCompression for Azure Functions via xdt transform
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.webServer>
<urlCompression xdt:Transform="Remove" />
<urlCompression doStaticCompression="false" doDynamicCompression="false" xdt:Transform="InsertIfMissing" />
</system.webServer>
</configuration>
@alexjamesbrown
alexjamesbrown / MattMarshEventsApi-GoogleSheet-Slugify.js
Last active June 30, 2019 21:57
Slugify function for Google Sheet
function Slugify(title, date) {
if (!title)
return "";
if(!date)
return "";
slug = '';