Itching for that lil' utility to have your XML massaged a bit? This program will modify your XML by removing the whitespace and/or comments.
http://mathfactcafe.com/util/xmlstrip.aspx
Just some random development ramblings mostly related to the Microsoft .NET platform.
Thursday, February 11, 2010
Friday, February 5, 2010
WCF: appSettings values not seen in nested web.config files
I recently ran into an issue with an WCF application that had services in sub-folders AND each one had it's own web.config file. When the app pool was reloaded after a period of time, appSettings would appear to have vanished when using the standard ConfigurationManager to retrieve them.
The WCF services were derived from a common base class that used some appSettings values. I had to place each service in sub-folders so each service could have their own copy of the appSettings values used by the base class. There are other known problems with WCF and web.config files; these are resolved by adding the aspNetCompatibilityEnabled element to the ServiceModel section of the web.config. I had already done this to fix earlier issues.
This new problem seemed to occur after the app pool was reloaded after being idle. It is like the service didn't even see the web.config in the sub-folder.
To fix the issue I had to create a separate Configuration object and manually point it to use the web.config from the virtual path of the web service in the sub-folder. I created a tiny helper class to read the appSettings for the web services:
Then I can then use the above class like:
The WCF services were derived from a common base class that used some appSettings values. I had to place each service in sub-folders so each service could have their own copy of the appSettings values used by the base class. There are other known problems with WCF and web.config files; these are resolved by adding the aspNetCompatibilityEnabled element to the ServiceModel section of the web.config. I had already done this to fix earlier issues.
This new problem seemed to occur after the app pool was reloaded after being idle. It is like the service didn't even see the web.config in the sub-folder.
To fix the issue I had to create a separate Configuration object and manually point it to use the web.config from the virtual path of the web service in the sub-folder. I created a tiny helper class to read the appSettings for the web services:
/// <summary>
/// This class is used to retreive appSettings values from a configuration file. Special
/// support is provided when nested web.config files are used since pre .NET 4.0 are broken
/// in terms of nested web.config settings.
/// </summary>
public class WcfAppSettings
{
private readonly Configuration _config;
public WcfAppSettings()
{
VirtualPathExtension extension = OperationContext.Current.Host.Extensions.Find<VirtualPathExtension>();
_config = WebConfigurationManager.OpenWebConfiguration(extension.VirtualPath);
}
public string this[string key]
{
get
{
return _config.AppSettings.Settings[key].Value;
}
}
}
Then I can then use the above class like:
WcfAppSettings appSettings = new WcfAppSettings();
string x = appSettings["MySettingKey"];
This appears to have fixed the issue. This is supposed to be fixed in .NET 4.0.
Wednesday, January 13, 2010
Word 2007 footnotes appearing on wrong page
I like to create a nice looking document when I'm tasked to do so. This includes using footnotes in Word 2007. I recently found that Word sometimes puts a footnote on the wrong page (especially if I have more than one footnote on a page). You can adjust the line spacing of the footnote to correct this (once I complete a document, I go back and modify all the footnotes even if they are on the correct page).
- Put your cursor in the footnote
- Modify the paragraph
- Set the Line Spacing to "Exactly" and "At" 10.5 pts which is a bit larger than the default footnote font size of 10 pts.
Friday, January 1, 2010
How do you handle configuration differences for environment (dev, QA, prod) using Visual Studio?
When executing a software application, there are always differences in configuration, depending on the environment. These configuration changes may include references to a database server, contact email addresses, or how exceptions are handled. I was recently doing some research on how Visual Studio 2010 handles this.
The question I would like to pose to folks is:
"What is your thoughts and/or model for building and deployment of the software bits? How do you use Visual Studio or other tools to build and deploy a "kit" to an environment?"
The question I would like to pose to folks is:
"What is your thoughts and/or model for building and deployment of the software bits? How do you use Visual Studio or other tools to build and deploy a "kit" to an environment?"
Tuesday, December 29, 2009
Nested web.config files and connectionStrings (entry has already been added)
When using the Entity Framework (EF) recently, I ran into a situation where I received the following error message:
I have a reusable EF DAL (data access layer) that is used by several web services on a single web site. Several of these web services are in sub-folders with their own web.config files. This allows me to have custom settings for each service as well as being able to easily xcopy an entire directory and know I have all the correct settings. For the EF I have a connectionStrings key in the config files. In addition to the nested web.config files, I have the top-level config which also has the EF connectionStrings key.
When I tried to access a web service I would get the error above because .NET was merging the web.config files and found duplicate connectionString keys. To fix this I just had to add the following element within the connectionStrings section, directly before my key:
The entry 'myConnectionstring' has already been added...web.config line: 47
After investigating I found out this was due to my use of web.config files in nested directories. Here is the situation...I have a reusable EF DAL (data access layer) that is used by several web services on a single web site. Several of these web services are in sub-folders with their own web.config files. This allows me to have custom settings for each service as well as being able to easily xcopy an entire directory and know I have all the correct settings. For the EF I have a connectionStrings key in the config files. In addition to the nested web.config files, I have the top-level config which also has the EF connectionStrings key.
When I tried to access a web service I would get the error above because .NET was merging the web.config files and found duplicate connectionString keys. To fix this I just had to add the following element within the connectionStrings section, directly before my key:
<remove name="myConnectionString" />
To facilitate knowing I can easily xcopy any web service, it is good practice to add this to all the web.config files, even the top-level one.
Sunday, December 27, 2009
XDocument / XElement - Save using a StringWriter as UTF-8 rather than UTF-16
One thing I find myself having to do on frequent projects is to save XML to a text file. I tend to use the .NET XDocument or XElement classes and use the Save() method. Once and a while I need to call Save() using a TextWriter. By default, when I do this the XML is written using a UTF-16 processing instruction as:
More often than not you want UTF-8; .NET XElement won't even read the UTF-16 contents back in using the Load() method (and things like IE won't be able to view it in the browser). To work around this problem I derive a new StringWriter and override the Encoding method:
Now just use the new class and the XElement.Save() method to write as UTF-8:
<?xml version="1.0" encoding="utf-16"?>
More often than not you want UTF-8; .NET XElement won't even read the UTF-16 contents back in using the Load() method (and things like IE won't be able to view it in the browser). To work around this problem I derive a new StringWriter and override the Encoding method:
public class StringWriterWithEncoding : StringWriter
{
private Encoding mEncoding;
public StringWriterWithEncoding(Encoding encoding)
{
mEncoding = encoding;
}
public override Encoding Encoding
{
get { return mEncoding; }
}
}
Now just use the new class and the XElement.Save() method to write as UTF-8:
XElement xml = XElement.Parse("<root/>");
using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
{
xml.Save(writer);
Console.WriteLine(writer.ToString());
}
Wednesday, December 16, 2009
The row value(s) updated or deleted either do not make the row unique or they alter multiple rows
I was working with a Microsoft SQL database today (not mine thank goodness) that had duplicate rows; the table had no primary key defined. When I went to go delete the duplicate row (or even change it), I got the error:
The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(2 rows)
It was somewhat hard to figure out how to delete the duplicate row. In the end, I was able to delete it by doing:
SET ROWCOUNT 1
DELETE FROM myTable WHERE statmentToSelectTheDuplicateRow
Another reason why to always define primary keys so you don't even get into this situation.
The row value(s) updated or deleted either do not make the row unique or they alter multiple rows(2 rows)
It was somewhat hard to figure out how to delete the duplicate row. In the end, I was able to delete it by doing:
SET ROWCOUNT 1
DELETE FROM myTable WHERE statmentToSelectTheDuplicateRow
Another reason why to always define primary keys so you don't even get into this situation.
Saturday, October 24, 2009
Remote Desktop using NLA from XP to Server 2008 R2
I recently had to connect to my Server 2008 R2 computer (only allowing Network Level Authentication (NLA) for RD) from an older XP client running SP3. No luck at first. Turns out that to use NLA from XP SP3, I had to make two registry changes.
I found out how to do this on https://support.soundenterprises.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=221, but I also included that information below just in case the source link disappeared one day.
Enabling Network Level Authentication on Windows XP Service Pack 3 for access to Server 2008 via Remote Desktop
When connecting to a Windows 2008 Server using remote desktop from a Windows XP client running service pack 2 or earlier, you get the following error message:
"The remote computer requires Network Level Authentication, which your computer does not support."
To enable NLA in XP machines; first install XP SP3, then edit the registry settings on the XP client machine to allow NLA.
Next, configure XP for NLA as follows:
1. Click Start, click Run, type regedit, and then press ENTER.
2. In the navigation pane, locate and then click the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
3. In the details pane, right-click Security Packages, and then click Modify.
4. In the Value data box, type tspkg. Leave any data that is specific to other SSPs, and then click OK.
5. In the navigation pane, locate and then click the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders
6. In the details pane, right-click SecurityProviders, and then click Modify.
7. In the Value data box, type credssp.dll. Leave any data that is specific to other SSPs, and then click OK.
8. Exit Registry Editor.
9. Restart the computer.
I found out how to do this on https://support.soundenterprises.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=221, but I also included that information below just in case the source link disappeared one day.
Enabling Network Level Authentication on Windows XP Service Pack 3 for access to Server 2008 via Remote Desktop
When connecting to a Windows 2008 Server using remote desktop from a Windows XP client running service pack 2 or earlier, you get the following error message:
"The remote computer requires Network Level Authentication, which your computer does not support."
To enable NLA in XP machines; first install XP SP3, then edit the registry settings on the XP client machine to allow NLA.
Next, configure XP for NLA as follows:
1. Click Start, click Run, type regedit, and then press ENTER.
2. In the navigation pane, locate and then click the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa
3. In the details pane, right-click Security Packages, and then click Modify.
4. In the Value data box, type tspkg. Leave any data that is specific to other SSPs, and then click OK.
5. In the navigation pane, locate and then click the following registry subkey: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders
6. In the details pane, right-click SecurityProviders, and then click Modify.
7. In the Value data box, type credssp.dll. Leave any data that is specific to other SSPs, and then click OK.
8. Exit Registry Editor.
9. Restart the computer.
Thursday, October 1, 2009
Server 2008 R2 / ApplicationPoolIdentity / Databases
I recently set up a new website on a Windows Server 2008 R2 computer running IIS 7.5 and SQL Server 2008. The new site would not run...wouldn't even start (.NET security issus, database login error). After some painful investigation on the issues, I encapsulated the steps to correctly use IIS 7.5's new ApplicationPoolIdentity support and allow access to a SQL Server 2008 database.
Microsoft will continue to cramp down on security and the use of ApplicationPoolIdentity is the default settings for a new website (so I'm blogging this to help others and to remind myself).
To the best of my knowledge, here are the steps to get your site up and running on 2008 R2 (if you have any corrections or feedback, please comment):
Whew!
Microsoft will continue to cramp down on security and the use of ApplicationPoolIdentity is the default settings for a new website (so I'm blogging this to help others and to remind myself).
To the best of my knowledge, here are the steps to get your site up and running on 2008 R2 (if you have any corrections or feedback, please comment):
1) Using IIS, select “Application Pools”. Right-click the pool to modify and
choose “Advanced Settings”. In the “Process Model” section:
a. Verify “Identify” is set to ApplicationPoolIdentity.
b. Verify “Load User Profile” is set to true.
2) Using IIS, select the site to modify. In the IIS section, double-click
“Authentication”. Next “Edit” the “Anonymous Authentication” name and verify
“Application pool identity” is chosen.
3) Using SQL Studio Management Studio, select “Security”, then “Logins”. Add
“New Login” using:
a. Set “Login name” to “IIS AppPool\yourIISSiteName”.
b. Select “Windows authentication” (don’t worry that this doesn’t resemble a
real account on the computer; click OK).
4) Optional for security flexibility on the site files:
a. Using Windows Explorer, right-click the site directory and choose
“Properties”. Select the “Security” tab and choose “Add”.
b. In the “Enter the object names select” box, enter
“IIS AppPool\yourIISSiteName”. Click OK.
c. Before leaving the “Permissions” dialog, customize the permissions for the
new account (read, write, execute, etc.).
Whew!
Monday, September 14, 2009
Simulated Annealing - Poker Solitaire and C#
I recently revisited the Simulated Annealing heuristic algorithm using C#. This algorithm is traditionally the choice for solving the Traveling Salesman problem, but I wanted to tackle another large combinatorial problem, Poker Solitaire.
For a description of the problem, visit Dr. Dobbs original article at:
http://www.ddj.com/184408203?pgno=16
How do you calculate a result with a solution set of 25 factorial (15,511,210,043,330,983,907,819,520)?
The Visual Studio 2008 solution (see below) provides a console application that repeatable executes a single annealing process, each time trying to optimize a 5x5 matrix of playing cards. The algorithm attempts to lay the cards out to optimize 12 poker hands (5 rows, 5 columns, 2 diagonals).
You can execute the console application using the syntax below, passing it the name of a text file that represents 25 cards for the 5x5 matrix.
C:> SimulatedSolitaire.Console.exe TestData\BestCards.txt
Download the Visual Studio 2008 solution by clicking here.
For a description of the problem, visit Dr. Dobbs original article at:
http://www.ddj.com/184408203?pgno=16
How do you calculate a result with a solution set of 25 factorial (15,511,210,043,330,983,907,819,520)?
The Visual Studio 2008 solution (see below) provides a console application that repeatable executes a single annealing process, each time trying to optimize a 5x5 matrix of playing cards. The algorithm attempts to lay the cards out to optimize 12 poker hands (5 rows, 5 columns, 2 diagonals).
You can execute the console application using the syntax below, passing it the name of a text file that represents 25 cards for the 5x5 matrix.
C:> SimulatedSolitaire.Console.exe TestData\BestCards.txt
Download the Visual Studio 2008 solution by clicking here.
Wednesday, September 2, 2009
Is "simple" better?
Just thought I'd pass along a lesson that I continue to learn.
A couple of years ago I had the need to know my ever-changing home IP address. Granted there are some free tools like Dynamic DNS and others that do this, but I had some special requirements that the IP had to be placed on another server for special reasons.
When I wrote the code, I created a service that ran on my home server. I used a C# interface so that I could "one day" move other types of data from my home PC to a server. I also had to have Visual Studio create the service installer and manually install the service. I created my Visual Studio solution with nicely decoupoled projects (assemblies). I was a genius.
Recently I had to change some stuff in that piece of code. The more I thought about it (and tried to remember all the code parts), I realized had I just taken a pure TDD (test-driven development) approach I could have done it much simpler. I then sat down and wrote a simple console app main() with about 4 lines of code to call a web page with a magical query parameter that did everything I needed. A quick job creation in Windows Task Manager and things were now running nicely.
Much less code and more importantly, much easier to maintain and make sense of. I know we all hear often (and I preach it as well) that we are to develop "extensible" code, but the reality of it is that probably 90% of the code we write will never even need to be extended. Given that, a TDD attribute is warranted in many cases (if not all of them).
I'm still learning that "simple is better" a lot of the time.
A couple of years ago I had the need to know my ever-changing home IP address. Granted there are some free tools like Dynamic DNS and others that do this, but I had some special requirements that the IP had to be placed on another server for special reasons.
When I wrote the code, I created a service that ran on my home server. I used a C# interface so that I could "one day" move other types of data from my home PC to a server. I also had to have Visual Studio create the service installer and manually install the service. I created my Visual Studio solution with nicely decoupoled projects (assemblies). I was a genius.
Recently I had to change some stuff in that piece of code. The more I thought about it (and tried to remember all the code parts), I realized had I just taken a pure TDD (test-driven development) approach I could have done it much simpler. I then sat down and wrote a simple console app main() with about 4 lines of code to call a web page with a magical query parameter that did everything I needed. A quick job creation in Windows Task Manager and things were now running nicely.
Much less code and more importantly, much easier to maintain and make sense of. I know we all hear often (and I preach it as well) that we are to develop "extensible" code, but the reality of it is that probably 90% of the code we write will never even need to be extended. Given that, a TDD attribute is warranted in many cases (if not all of them).
I'm still learning that "simple is better" a lot of the time.
Subscribe to:
Posts (Atom)
Can't RDP? How to enable / disable virtual machine firewall for Azure VM
Oh no! I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore. I did ...
-
Don't want Office 2007 installed on your web server to access Excel 2007 content? Here is a Visual Studio C# solution that demonstrates ...
-
Here is a full test program that demonstrates how to use SharpZipLib to zip an XElement into a byte array. This allows you to transfer larg...