ASP.NET 5 Dependency Injection

ASP.NET 5 now has a built in default DI container. Here’s a simple example of using this that isn’t in a Controller class (all the examples I’ve seen so far are how to inject objects into a controller) …

Mapping our interfaces

First we need to map our interfaces to the implementations we want the app to use in Startup.ConfigureServices()

This code tells the DI container that when IHelloMessage is requested, return a new instance of HelloMessage.

The code for IHelloMessage and HelloMessage is below:

Requesting the implementation

Controller classes do this for us but we use IApplicationBuilder.ApplicationServices.GetRequiredService() to get the implementation for other classes.

The code below injects IHelloMessage into ResponseWriter during each http request.

The code for ResponseWriter is below:

If we run the app, we get something like below and everytime you refresh the browser, the time within the message should change.

run services.AddTransient

Other mapping options

If we wanted to only use a single instance when IHelloMessage is requested, we could have used:

Now when we refresh the browser, the time should stay static.

If we wanted a specific instance to be used, we could have used:

Now when the run the app, we should see “My instance” in the web page.

Connect with me:RSSGitHubTwitterLinkedIn

12 Tips for a Better Daily Standup

In my post a few months ago on Scrum pain, I criticised the daily stand up. However, they can be very valuable – they can help the team synchronise their efforts and get the right focus for the day ahead.

Here are some tips that I’ve found useful ..

  • Meet close to work desks. Some of the content of the meeting is about progress and plans on work items and there will be many memory triggers for this at our work desks, perhaps even a kanban board
  • Start the meeting first thing in the morning, at same time every day. This will help everyone focus for the day.
  • Always start on time. Start, even if members haven’t made it on time, including scrum master. This ensures that this event is a minimum interuption to everyones day
  • Stand up. This helps the meeting stay focused – physical discomfort will indicate that the meeting is going on for too long
  • Rotate the person driving the meeting. This encourages team autonomy
  • Avoid problem solving. It is natural for developers to problem solve but this isn’t the focus of this meeting – take the problem solving offline
  • Get commitment for today’s plan. Watch out for “I think …”, “I hope …”, “… as soon as possible” – the team member isn’t really making a good commitment using these words. We want to hear “I will ” with a specific timings
  • Do not to commit to things beyond our control. For example “I will fix 5 bugs today” has a good chance of failure if we don’t know anything about the 5 bugs. The problem with failure is that the next time that person gives a commitment, it will likely have an inflated timescale. It’s much better if we say something like “I’ve looked into bug X, I know the root cause and will have the fix checked in by lunch time. I will then start looking at bug Y and if I get that done, bug Z”.
  • Get commitment from the Scrum master. Make sure the Scrum master is specific about the timing of the next step in removing any impediments that come up. If the Scrum master is developing, then get their commitments for the day, just like the other developers on the team
  • If you are the Scrum master, break eye contact. This encourages team members to address the team which in tern encourages automony
  • Time the meetings. Share the results. This will reduce the risk of meetings dragging on
  • Finish positively. “Enjoy your morning everyone!”
Connect with me:RSSGitHubTwitterLinkedIn

ASP.NET 5 HttpHandlers? / HttpModules?

In ASP.NET 5, both HttpHandlers and HttpModules are no longer needed because of the ability to now plugin to the request pipeline. This post details how to plugin into the ASP.NET 5 request pipeline and handle requests for a given URL.

Pluging in to the pipeline

Startup.Configure() lets you plug middleware into the request pipeline using IApplicationBuilder.Use()

The parameter in IApplicationBuilder.Use() is a delegate that in tern has parameters giving the HttpContext and a delegate to the next piece of middleware.

The code below is an example of how to plug some code into the request pipeline. In this example, MyHttpHandler.ProcessRequest() is called during every request.

HttpContext is different

One thing to note is that the HttpContext class is a little different …

For example HttpContext.Request.Path is of type StringPath rather than String and HttpContext.Request.HttpMethod is now HttpContext.Request.Method.

Handling the request

The following code is the implementation of MyHttpHandler from the earlier example. It should output “This is my HttpHandler …” for the path /MyHttpHandler.

If we try this and browse to /MyHttpHandler you should get the following:

ASP.NET 5 HttpHandler

Connect with me:RSSGitHubTwitterLinkedIn