C# 10 File Scoped Namespaces - How to Migrate Your Project

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:

namespace ApiKeyAuthentication
{
    ...
}

VS will auto refactor this to

namespace ApiKeyAuthentication;

and remove the curly braces and indentation.

If you want to apply this to your entire project, follow these steps:

  1. Create a new file in your project root called .editorconfig (or use the existing file)
  2. Under Code Style search for namespace and select File Scoped
  3. 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.
  4. Build your project and ensure there are no errors.

Need a developer, architect or manager? I am available - email me at [email protected]

Using Transloadit for file uploading & processing on your website

Over the years I have used a variety of custom solutions to resize images, encode video, etc.  Transloadit provides a really nice cloud based solution that you can use with any technology stack.  From rails, to php, to JavaScript, you can interface with their API to upload images and videos.  They offer some easy to use plugins to help get you started https://transloadit.com/docs#jquery-plugin

Visual Studio 2010 SP1 IIS will not start

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

http://superuser.com/questions/282519/change-the-port-of-iis-7-5-express

Debug WCF service IIS specified authentication schemes 'IntegratedWindowsAuthentication, Anonymous', but the binding only supports

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:

  1. In IIS, uncheck the Annonymous scheme
  2. In IIS, leave Integrated Windows Authentication checked - this is required to run the VS Debugger
  3. Start the debugger from VS
  4. Before you make a request to your service, go back into IIS and check the Annonymous scheme
  5. Make a request to your service and your break point will be hit

Passing Exam 70-523 Upgrade: Transition Your MCPD .NET Framework 3.5 Web Developer Skills to MCPD .NET Framework 4 Web Developer

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:

  1. WCF with .NET 4 - approximately 20 questions
  2. Web Application development with .NET Framework 4 - approximately 20 - 30 questions including topics on MVC and Web Forms
  3. Accessing Data with Microsoft .NET framework 4 - approximately 20 questions including topics on EF, LINQ, and DataTables
  4. 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:

http://gregorsuttie.wordpress.com/2011/01/01/net-upgrade-exam-70-523/

http://www.microsoft.com/learning/en/us/exam.aspx?ID=70-523#tab2

 Good luck!

AspNetMembershipProvider for MySQL

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.

IIS localhost System.Net.Mail.SmtpFailedRecipientsException Unable to relay for

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>

LINQ to SQL dynamically set order by

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:

  1. First I specified my LINQ query and selected the results into an annonymous type and placed into a variable (var data = ...)
  2. 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.
  3. 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, ...));
    this.AddRange(selected);

I understand I could do this in fewer steps, but am leaving as it to explicitly outline the approach taken.

 

 

Generating a valid RSS feed using SyndicationFeed and Rss20FeedFormatter

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

Missing channel element: link

you can modify your code as follows:

 

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.

.NET how to set the background color of a bitmap

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.Clear(Color.White);

grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;

grPhoto.DrawImage(imgPhoto,
                new Rectangle(destX, destY, destWidth, destHeight),
                new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

            grPhoto.Dispose();