Correlated Content

Replace Text in a Stream: StreamReader and StreamWriter

The problem with the previous methods is that we need to read the entire request into memory and convert it to string before we can do our search and replace. This explains the high memory usage.

A better way would be to work on just a part the request and send it to the response as soon as possible, which should reduce the memory footprint significantly. This is what is called a circular buffer. In that pattern, you read a part of the original stream called a buffer. You do your work in the buffer and send it to the output stream. Only then you read the next part of the input stream, overwriting the buffer. You only keep the buffer in memory and not the whole input stream.

Read the rest of " Replace Text in a Stream: StreamReader and StreamWriter"

Replace Text in a Stream: Introduction

What is the best way to replace a string in a stream?

I was working with request and response body transforms in YARP when I read the following in the documentation:

The below example uses simple, inefficient buffering to transform requests. A more efficient implementation would wrap and replace HttpContext.Request.Body with a stream that performed the needed modifications as data was proxied from client to server. That would also require removing the Content-Length header since the final length would not be known in advance. (source)

This tickled my curiosity and I went on a quest for that more efficient implementation. During that quest I learned about Span<T> and Pipes and the surprising power of regex. I got some help from friends and used new tools that helped me to find the most efficient way of replacing a string in a stream.

Read the rest of " Replace Text in a Stream: Introduction"

Aspect Oriented Programming style Caching with Castle Windsor

We were doing some work on caching at work the other day, sprinkling calls to cache values here and there in our methods. I couldn’t shake the feeling that this was creating a spaghetti-mess where what a method should do got dilluted with the caching logic. I then remembered that this was exactly the problem Aspect Oriented Programming set out to solve. Wouldn’t it be nice if you could decorate a method with an attribute and all the caching logic would magically be applied to the output of the method. Like this:

public string SomeMethod(int someArgument)
	//some logic
	return "someValue";

Challenge Accepted!

Read the rest of " Aspect Oriented Programming style Caching with Castle Windsor"
Loading comments...