Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting CultureInfo on wcf service calls?

I have a WCF service running that needs to parse some data. It turns out that data (points, sizes) gets converted differently in different CultureInfo's and the parsing is spread out in a lot of classes and methods. Since all the parsing is done without passing any CultureInfo the success of the parsing is dependant of the threads culture.

Since there is no programmatic setting of CultureInfo the service picks the current cultureinfo off the machine somehow. I have no idea where it gets this, since changes to the Regional and Language Options doesn't seem to have any effect on the cultureinfo of the wcf service. Also changes to the web.config (yes the service is hosted in iis) doesn't seem to work either.

Am I really left with only one option? Setting the CultureInfo programmaticly? I could find all the conversion calls and pass in a CultureInfo or i could set it on the Thread.CurrentThread.CurrentCulture. Is there no way i can set the CultureInfo once and for all - having effect on all the exposed wcf methods?

like image 657
Per Hornshøj-Schierbeck Avatar asked Apr 28 '09 10:04

Per Hornshøj-Schierbeck


2 Answers

The answer about using tag in web.config only works if Asp.net compatibility mode is enabled. You also need the following inside :

<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

Without Asp.Net compatibility mode, the http modules are not used and the tag is ignored.

like image 182
Spacebar Avatar answered Sep 19 '22 19:09

Spacebar


You should check out this blog post...

http://blogs.msdn.com/drnick/archive/2008/02/26/using-call-context-initializers-for-culture.aspx

... which shows how to define a behaviour for setting the culture.

HOWEVER, web.config should be your friend here. You should be able to set up the "default" culture that your service works with from here.

The globalization elemenent...

http://msdn.microsoft.com/en-us/library/hy4kkhe0.aspx

... should allow you to set the Culture and UICulture...

<globalization
    enableClientBasedCulture="true|false"
    requestEncoding="any valid encoding string"
    responseEncoding="any valid encoding string"
    fileEncoding="any valid encoding string"

    responseHeaderEncoding = "any valid encoding string" 
    resourceProviderFactoryType = string
    enableBestFitResponseEncoding = "true|false"

    culture="any valid culture string"
    uiCulture="any valid culture string"/>
like image 30
Martin Peck Avatar answered Sep 18 '22 19:09

Martin Peck