One of the nice new features in C# 10 is file scoped namespaces. It clears some of the cruft and provides more horizontal space for code instead of an extra indent. If you are using Visual Studio 2022 or later you have a couple of nice options to apply this change at a page or project level.
At a page level, you can simply add a semi-colon at the end of your traditional using statement:
VS will auto refactor this to
and remove the curly braces and indentation.
If you want to apply this to your entire project, follow these steps:
- Create a new file in your project root called .editorconfig (or use the existing file)
- Under Code Style search for namespace and select File Scoped
- Go to one of your traditional namespace declarations, right-click it and select "Quick Actions and Refactorings". You will see a list of all refactoring actions available, and one of them will be "Convert to file-scoped namespace". At the bottom, select Project and you’ll see a preview of the changes to be made. Then click Apply.
- Build your project and ensure there are no errors.
Need a developer, architect or manager? I am available - email me at [email protected]
If you install Visual Studio 2010 SP1 and then you find that IIS will not start, you may have a port conflict issue. SP1 includes a new service, the Web Deployment Agent Service. This new service runs on port 80, the same default port that IIS uses for HTTP traffic. If the Web Deployment Agent Service is running, IIS will not start if you are using port 80 in IIS. On option is to modify the web deployment agent service. For me, I stopped and disabled since I do not plan to use. You may be able to change the port is uses by default. Additional info can be found at the link below
If you want to debug a WCF service you may encounter the following error in Visual Studio:
IIS specified authentication schemes 'IntegratedWindowsAuthentication, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used.. ---> System.InvalidOperationException: IIS specified authentication schemes 'IntegratedWindowsAuthentication, Anonymous', but the binding only supports specification of exactly one authentication scheme. Valid authentication schemes are Digest, Negotiate, NTLM, Basic, or Anonymous. Change the IIS settings so that only a single authentication scheme is used
This can occur if the virtual directory hosting the service in IIS has both the Intergrated Windows Authentication and Anonymous authentication schemes selected. In order to get around this so that you can debug your service, you can do the following:
- In IIS, uncheck the Annonymous scheme
- In IIS, leave Integrated Windows Authentication checked - this is required to run the VS Debugger
- Start the debugger from VS
- Before you make a request to your service, go back into IIS and check the Annonymous scheme
- Make a request to your service and your break point will be hit
I recently passed exam 70-523 to earn my MCPD .NET Framework 4 certification. The exam covers a fairly wide range of topics and the following provides an overview of the sections to expect on the exam.
There are 5 seperate tests that you will take during the exam and this includes one real world scenario statement follwed by 6 questions on the requirement statement.
The tests break down into the following:
- WCF with .NET 4 - approximately 20 questions
- Web Application development with .NET Framework 4 - approximately 20 - 30 questions including topics on MVC and Web Forms
- Accessing Data with Microsoft .NET framework 4 - approximately 20 questions including topics on EF, LINQ, and DataTables
- Designing and Developing Web apps - 20 questions + the real world scenario statement & quesitons
The exam was moderatly difficult but if you have about 1 years experience in .NET 4 including MVC, EF, LINQ, and WCF, and jQuery you should do OK and no need to get a vce or brain dump or anything like that.
Some additional resources I found helpful:
If you use ASP.NET (webforms or MVC) you may want to use MySQL as your database. One nice feature of .NET is the Membership Provider which takes care of authentication and authorization. You can use MySQL to store the necessary informaion for the membership and role providers. The below article details how to do this:
If you run into issues, make sure you copy the MySQLMembershipProvider and MySQLProfileProvider sections from the machine.config to your local web.config. Once you do this, you should be all set.
If you do development on your localhost and you want to test email, you can update your web.config as per below. Note that you will need a directory of D:\Maildrop on your machine. Doing this will result in emaills being delivered to the directory you specify.
You may receive Unable to relay for... errors and if you do, you can specify a from in the smtp element.
<mailSettings> <smtp deliveryMethod="SpecifiedPickupDirectory" from="local@localhost">
<specifiedPickupDirectory pickupDirectoryLocation="D:\Maildrop"/> </smtp></mailSettings>
Often I find the need to dynamically specify the order by for a LINQ to SQL query. I came across a very nice example of this here.
I needed to use this technique with a CSLA class, so I needed to call it in a sligthly different manner than the way explained in the article. I was grouping information and needed to then dynamically set the order by and finally place the results into a CSLA read only list.
I accomplished this as follows:
- First I specified my LINQ query and selected the results into an annonymous type and placed into a variable (var data = ...)
- Next, called my OrderBy extension method on the LINQ query created in #1
var ordered = data.OrderBy(criteria.OrderByProperty, criteria.OrderByDesc);
The criteria object is the typical pattern used in CSLA to pass data to your data access methods such as fetch.
- Finally, I selected the data elements into my CSLA list item, and add to the parent list object
var selected = ordered.Select(a => MyCslaListItem.GetMyCslaListItem(a.Id, a.Name, a.Email, ...));
I understand I could do this in fewer steps, but am leaving as it to explicitly outline the approach taken.
If you are using the .NET Rss20FeedFormatter and SyndicationFeed classes to generate an RSS feed, you may run into an issue of needing to include a link attribute to the channel element. If you validate the default RSS generated the validation will fail since the channel element does not contain a link attribute.
You can test this by going to http://beta.feedvalidator.org and entering your feed Url. If you recieve the error
SyndicationFeed syndicationFeed = new SyndicationFeed(synicationItems);
syndicationFeed.AttributeExtensions.Add(new XmlQualifiedName("link"), "http://yoursite.com");
This will produce a valid RSS feed with a channel that contains a link attribute.
I was struggling with this for a while...how to set the background of a bitmap. I read a few suggestions, but none worked for me.
My need was to set the background of the image to white, crop it, with the end result that after cropping, the background be set to white.
To do this, I used the Clear method on the Graphics class...essentially this sets the background.
Graphics grPhoto = Graphics.FromImage(bmpImage);
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
new Rectangle(destX, destY, destWidth, destHeight),
new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),