Skip to content

Instantly share code, notes, and snippets.

View alexjamesbrown's full-sized avatar

Alex Brown alexjamesbrown

View GitHub Profile
@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 / 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 / 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 / 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);
}
//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);
var customer = new Customer();
customer.FirstName = firstName;
customer.LastName = firstName; //uh oh!
//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");
public interface ICustomerService
{
int Save(Customer customer);
}
public class CustomerController : Controller
{
private ICustomerService _customerService;
public CustomerController(ICustomerService customerService)
{
_customerService = customerService;
}
public IActionResult Post(string firstName, string lastName)
@alexjamesbrown
alexjamesbrown / info.aspx
Created June 24, 2015 15:10
aspx page displaying the server name
<script language="c#" runat="server">
public void Page_Load(object sender, EventArgs e)
{
Response.Write("<h1>"+System.Environment.MachineName+"</h1>");
}
</script>