Preheat oven to 350 degrees. Spray a 2-quart (approximately) baking dish with cooking spray. Set aside.
Prepare an ice bath. Bring a large saucepan of salted water to a boil. Add the beans to the boiling water and cook for 3-4 minutes, or until tender. (The green beans heat through when baked, so crisp-tender beans will stay fairly crispy. If you like softer beans, cook them longer.)
Remove and place in the ice bath to stop the cooking process. Drain, discard the water and pat dry. Transfer the beans to the prepared baking dish.
Heat the butter over medium heat in the saucepan you used to cook the beans.
Add the shallot and cook for 2-3 minutes or until softened, being careful not to burn it. Add the garlic and cook briefly (10 seconds) until fragrant.
Whisk in the flour and cook for 2 minutes, being careful not to brown the flour.
Add the wine (or beer), whisking constantly then slowly pour in the chicken broth and half-and-half. Bring the sauce to a boil, whisking until smooth and thickened.
Remove the sauce from the heat. Let the sauce rest for 1 minute, then add the shredded Cheddar by the handful, whisking after each addition until the cheese melts and the sauce is smooth.
Season the sauce with salt, black pepper and cayenne pepper if desired.
Pour the sauce over the green beans. MAKE AHEAD: Can be made two days ahead of time to this point and refrigerated. Keep the topping off until ready to bake.
Sprinkle with the pecans and the fried onions.
Bake for 25 to 30 minutes or until heated through. Serve immediately.