Culture changing inexplicably during ASP.NET web a

2019-05-10 06:40发布

问题:

I'm having a weird issue when where the date format of from the date fields changes from 4/16/2009 12:00:00 AM to 16/04/2009 00:00:00. I set the application to write out each stored proc that fired with the same corresponding date field if it exists. Here's what came out. You'll notice that the format switch inexplicable half way through.

EXECUTE uspContent_SelectOne '132'
4/16/2009 12:00:00 AM
EXECUTE uspContent_SelectOne '127'
4/16/2009 12:00:00 AM
EXECUTE uspContent_SelectOne '133'
4/16/2009 12:00:00 AM
EXECUTE uspContent_SelectOne '131'
4/16/2009 12:00:00 AM
EXECUTE uspAttachment_SelectAll
EXECUTE uspArticleAuthors_SelectAll_ArticleId '3'
EXECUTE uspArticles_SelectOne '3'
EXECUTE uspAuthors_Letters
EXECUTE uspAuthors_Letters
EXECUTE uspAuthors_Letters
EXECUTE uspAuthors_SelectAll_Letter_LastName 'A'
EXECUTE uspFiles_SelectAll_NoFileData
EXECUTE uspArticles_SelectOne '3'
EXECUTE uspArticleTypes_SelectAll
EXECUTE uspFiles_SelectAll_NoFileData
EXECUTE uspAuthors_SelectOne '0'
EXECUTE uspArticleAttachments_SelectAll_ArticleId '3'
EXECUTE uspArticleAttachments_SelectOne_ArticleId_AttachmentId '3','4'
EXECUTE uspAttachment_SelectOne '4'
EXECUTE uspContent_SelectOne '132'
16/04/2009 00:00:00
EXECUTE uspContent_SelectOne '127'
16/04/2009 00:00:00
EXECUTE uspFiles_SelectOne_NoFileData '60'
EXECUTE uspArticleAttachments_SelectOne_ArticleId_AttachmentId '3','3'
EXECUTE uspAttachment_SelectOne '3'
EXECUTE uspContent_SelectOne '133'
16/04/2009 00:00:00
EXECUTE uspContent_SelectOne '131'
16/04/2009 00:00:00
EXECUTE uspAttachment_SelectAll
EXECUTE uspArticleAuthors_SelectAll_ArticleId '3'
EXECUTE uspAuthors_SelectAll_Letter_LastName 'A'
EXECUTE uspContent_SelectOne '129'
18/09/2008 00:00:00
EXECUTE uspContent_SelectOne '7'
18/09/2008 00:00:00
EXECUTE uspContent_SelectOne '8'
18/09/2008 00:00:00
EXECUTE uspContent_SelectOne '9'
18/09/2008 00:00:00
EXECUTE uspContent_SelectOne '10'
18/09/2008 00:00:00
EXECUTE uspContent_SelectOne '11'
18/09/2008 00:00:00
EXECUTE uspFiles_SelectAll_NoFileData 

I can't figure out why. I'm using the ASP.NET/C#, SQL Server 2005, and the MS Enterprise Library 4.1.

UPDATE 1

I checked the culture settings of the app and got this. For every date field query I checked the culture. In the screen shot below you can see how the culture changes from US to UK:

image of cuture changing http://www.craigmoliver.com/cultureissue.jpg

The setting in the web.config it's set as follows:

<globalization culture="en-US" uiCulture="en-US" requestEncoding="utf-8" responseEncoding="utf-8" enableClientBasedCulture="false" />

回答1:

Are the requests coming from different browsers? Are you picking up the Culture/UICulture from the Accept-Language header in the HTTP Request? If so, it is possible that you have one browser that has it's preferred language set to en-US and another set to en-GB?



回答2:

The suggestions to check the current culture are promising.

One additional possibility to consider: Is your application running in a load-balanced server farm and are the web servers properly configured identically?



回答3:

Create a breakpoint that triggers whenever Thread.CurrentThread.CurrentCulture changes. That should at least point you in the right direction.



回答4:

If you are using DataSets generated from XSD schemas (DataSet.ReadXmlSchema method), and the XSD contains an msdata:Locale Attribute, culture-dependent operations on this DataSet might use the locale from the schema, ignoring web.config settings. Explicitly setting it for the current thread then overrides the locale from the XSD again.

I lost a few hours on strange locale-switching-related problems once and this turned out to be the reason.

Maybe something similar could also hide in the Enterprise Library config files. I would also recommend searching all project-related files for the offending locale string (en-UK). Maybe something interesting comes up.



回答5:

In the methods where I performed a "Convert.ToDate" I put the following code in:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");

Still don't know WHY it's doing it. The default culture on my machine and the development server is en-US.

This is maddening!



回答6:

Is this happening during the life cycle of a single page, or across several pages? If it's across several pages, you should also check the @Page directives in the markup for each page in your app - you can explicitly set the culture and UI culture there. Maybe you have one rogue page with the culture set improperly.



回答7:

You could also check in your procedures for use of the SET DATEFORMAT statement. In SQL Server it is possible to change the intepretation of date values within the context of a procedure so that the database parses dates differently. The following code

SET DATEFORMAT 'DMY';
SELECT CAST('1-4-2009' AS date);
SET DATEFORMAT 'MDY';
SELECT CAST('1-4-2009' AS date);

Returns the following results on my PC

2009-04-01
2009-01-04

SQL Server will therefore interpret the string values as dates differently