Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to change Culture in WCF

I'm running on a windows 2008 server. I have one Web service which calls a wcf service. Within the WCF service it attempts to cast a date 20/08/2010 which fails because it thinks it in US format not Austrlaian.

So far I have:

  • On control panel change the region to English Australian under format
  • Under the Administrative tab I have also set system local to English (Austrlian)
  • within IIS7 at the default web site level I have changed Culture and UI culture under the .Net globalization.
  • I've also done this at the Web service and WCF Nodes

I have added the following to the Web service and WCF apps web.config file

<globalization requestEncoding="utf-8" 
               responseEncoding="utf-8"  
               culture="en-AU" 
               uiCulture="en-AU" />

This finally changed the culture in the Web service but the WCF service remains US culture.

Can anyone tell me what else I can try?

like image 499
ErickTreetops Avatar asked May 24 '12 00:05

ErickTreetops


2 Answers

The WCF will ignore your globalization configuration if you do not set aspNet compatibility:

<system.serviceModel>    
   <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
...

To use that mode your service class must have the attribute AspNetCompatibilityRequirements set to Allowed or Required:

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class ServiceClass
{
...
}

This could work if you want to apply the Culture and CultureUI from config file.

Or you could either try to force the Culture in your WCF service code, if you are sure that it will not change dynamically. For instance, in your service class constructor. Note that this is not a best practice, perhaps you should use a Context initializer, but this one is quite simple.

public ServiceClass()
{
    ...
    System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-AU");
    System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-AU");
}

More info:

setting-cultureinfo-on-wcf-service-calls

using-call-context-initializers-for-culture

like image 83
Diana Avatar answered Sep 28 '22 06:09

Diana


The problem is in the culture that is set for a user used in the application pool.

I found the following way to resolve this issue:

  1. If the application pools uses ApplicationPoolIdentity change it to NETWORKSERVICE (unfortunatly I didn't found how to set regional settings for ApplicationPoolIdentity)
  2. Set regional settings you need (en-AU) on the current user and than copy them for the system accounts as described here.
like image 21
Teddy Bo Avatar answered Sep 28 '22 08:09

Teddy Bo