ErickTreetops
ErickTreetops

Reputation: 3385

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:

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?

Upvotes: 11

Views: 5894

Answers (3)

ferhrosa
ferhrosa

Reputation: 1793

You can do it in the Global.asax.cs file, in the Application_Start file:

using System.Threading;
using System.Globalization;

public class Global : HttpApplication
{
    protected void Application_Start(object sender, EventArgs e)
    {
        CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-AU");
    }
}

Upvotes: 0

Diana
Diana

Reputation: 2226

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

Upvotes: 7

Teddy Bo
Teddy Bo

Reputation: 679

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.

Upvotes: 2

Related Questions