Skip to content

Instantly share code, notes, and snippets.

View tugberkugurlu's full-sized avatar
:shipit:
💥 shakalaka

Tugberk Ugurlu tugberkugurlu

:shipit:
💥 shakalaka
View GitHub Profile
[Test]
public void Should_be_able_to_consume()
{
var autoResetEvent = new AutoResetEvent(false);
using (var channel = connection.OpenChannel())
{
channel.Declare(queue);
var settings = new ConsumerSettings(queue)
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace TDFDemo
{
class Program
{
@tugberkugurlu
tugberkugurlu / PerfTracer.cs
Created December 31, 2012 00:19
ASP.NET Web API Performance Tracing
// The Web API pipeline writes traces at the beginning and end of an operation.
// The TraceRecord class includes a TimeStamp property, so you can use this information
// to see how long various operations take.
// Traces written at the start of an operation have the Kind property set to TraceKind.Begin,
// and traces at the end of an operation have Kind set to TraceKind.End. To get the total
// execution time, match Begin and End traces. The following code shows a trace writer
// that collects traces and matches them when GetPerformanceRecords is called. You could even
// write an API controller that returns the performance data from this trace writer.
public interface IPeopleContext : IDisposable, IEntitiesContext {
IDbSet<Person> People { get; set; }
IDbSet<Book> Books { get; set; }
}
public interface IEntitiesContext : IDisposable {
DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
IDbSet<TEntity> Set<TEntity>() where TEntity : class;
@tugberkugurlu
tugberkugurlu / FizzBuzz.cs
Last active October 12, 2022 20:14
FizzBuzz problem solution with C#.
// reference: http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html
static class Program {
static void Main(string[] args) {
for (int i = 1; i <= 100; i++) {
bool canBeMultipliedByFive = i.CanBeMultipliedBy(5);
bool canBeMultipliedByThree = i.CanBeMultipliedBy(3);
private static void RegisterRepositories(ContainerBuilder builder) {
Type baseEntityType = typeof(BaseEntity);
Assembly assembly = baseEntityType.Assembly;
IEnumerable<Type> entityTypes = assembly.GetTypes().Where(x => x.IsSubclassOf(baseEntityType));
foreach (Type type in entityTypes) {
builder.RegisterType(typeof(EntityRepository<>).MakeGenericType(type)).As(typeof(IEntityRepository<>).MakeGenericType(type)).InstancePerApiRequest();
}
}
/// <summary>
/// Base generic HttpClient class for the more specified clients.
/// </summary>
/// <typeparam name="TResult">Type of the result type which is expected.</typeparam>
/// <typeparam name="TId">Type of the id parameter for this client.</typeparam>
public abstract class HttpApiClient<TResult, TId> where TResult : IDto {
// Thoughts:
// 1- _httpClient which is passed here is fully configured HttpClient.
// No further alteration is required for headers, etc.
ServerSettings = new MongoServerSettings {
Servers = new List<MongoServerAddress> {
new MongoServerAddress("unittest")
}
};
Server = new MongoServer(ServerSettings);
DatabaseSettings = new MongoDatabaseSettings("databaseName", new MongoCredentials("", ""), GuidRepresentation.Standard, ReadPreference.Primary, WriteConcern.Acknowledged);
Database = new MongoDatabase(Server, DatabaseSettings);
public class CorrelationHandler : DelegatingHandler {
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) {
var response = await base.SendAsync(request, cancellationToken);
response.Headers.Add("X-CorrelationId", request.GetCorrelationId().ToString());
return response;
}
}
// Before
public async Task DoWork() {
await Task.Delay(2000).ConfigureAwait(false);
Console.Writeline("Foo...");
}
// After
public async Task DoWork() {