Quick Django Foreign-key Gotcha

June 14th, 2012

When your Django app poops out with:

'RelatedManager' object is not callable

The problem might be that you’re trying to call up related fields via a foreign key field type’s auto-generated reverse-lookup attribute — but mistakenly trying to call it as a method instead of as an attribute that holds a query set. In code terms, you put:

tracks = cd.track_set()

What you meant to do was:

tracks = cd.track_set.all()

China 2008 Reflections

May 6th, 2012

Observations on China
August 2008
Randall Lucas

This blog post, adapted from a letter to friends and colleagues, summarizes my observations from a recent trip to Beijing, Shanghai, and some smaller cities in between, during three weeks in August, 2008 (coinciding with, but not focused on, the Summer Olympics).

While no short trip or short document could hope to capture much of this vast country and ancient civilization, I certainly felt I learned quite a lot.  My target audience is the Western thinker who has not (recently) visited China; old hands at Sinology are asked to indulge me some basic explanations in parts.

Originally composed at Seattle, Wash., 25 August 2008.

I am dividing my observations into the following broad categories:

– Labor
– Sharp Edges
– Maintenance
– Content
– Health
– Sex
– Law and Government
– Intellectual Property
– Video
– Service
– Salesmanship
– Cargo Cult

– Labor

Every Western observation of China must begin with the sheer quantity of humanity.  In fact, this great number is not apparent in the way one might find most obvious (on the streets of the cities).  Nowhere in Beijing were the streets anywhere near as crowded as, say, a New York shopping street in December.  However, this may be because of the government’s Olympic preprations, which, rumor holds, caused all of the construction workers, laid-off and gypsie cabbies, and “aunties” (older cleaning maids) to be sent out of town for the duration.

Rather, the way the relative scarcity or abundance of people is made visible is through the labor market.  And the shocking discovery is that /labor is essentially free in China./  The cost of hiring a full-time employee is less than what we would consider, in the West, the mere “overhead” of keeping an employee (taxes, insurance, etc.).  Consider the following examples:

A tourist-targeted tailor’s shop offers to add French cuffs to a shirt or working buttonholes to a suit coat for no extra cost.  It’s as though the labor involved in sewing is just a loss leader to get the fabric sold.

Each floor of a hotel, each car of a train, and damn near each table at a restaurant has a fuwuyuan, or attendant, serving just that floor or car or table.  This is in addition to the normal complement of staff that you would expect (bellmen, clerks, conductors, waiters, etc.).

Foot massage is a ubiquitous creature comfort, and is 40-100 RMB ($6-15 US) for an hour (likely including tea and other consumables) even in the large cities.

Gas costs over 6 RMB a liter, almost $3.40 a gallon, and yet the taxi fare in Beijing is less than 50c per mile.  The cabs are either old shitboxes or, surprisingly, many newer Jettas and Hyundais.  When you consider that gas and capital costs are nearly the same as for a U.S. cab, the cost of labor almost disappears in the numbers.

Instead of coinboxes, buses have a ticket-seller on board to make change and shepherd passengers.

A result of having such a huge number of people is how you relate to them.  My take on the “sea of humanity” effect is that it leads to a binary view of others.  You are either Somebody (a friend, a colleague, a customer), or Nobody At All.  If you are Somebody (and you can become Somebody through just the briefest exchange or introduction), you will be extended every courtesy.  If you are Nobody At All, then expect to be ignored, cut in front of (in line), dismissed with an indecipherable grunt, and almost literally spat upon.  It seems rude, but it’s a fairly effective way to run a civilization of a billion and a half souls.

Finally, it is worth noting that even with all these people, on the ground, China still operates at human scale.  For example, we bumped into a (Chinese) girl at one tourist site two days after we’d briefly spoken at another, and recognized each other.  There are not so many people that these sorts of things can’t happen.

– Sharp Edges

Things in China have sharp edges.  Don’t want to fall off the edge of that stone stairway (sans handrail)?  Well, don’t walk next to the edge.  Don’t want to break an axle driving down a main urban street?  Well, don’t drive over that open manhole.

2- and 3-star hotel rooms may or may not have a smoke detector, but will definitely have burn marks all over every tabletop and counter surface from where forgotten cigarettes burned themselves out, thankfully without incident.

I still haven’t figured out what it is the taxi drivers all say when you start to reach for the nonexistent or nonfunctional seat belts in back, but it’s on the order of “give it up.”  (Tip: the front passenger “suicide seat” might be a better deal, as its belt more often works.)  Beltless cabs comprise 80-90% of the total.

Seatbelts: a fantasia

Traffic itself is a royal flying three-ring clusterfuck with crossed palms.  No, it’s nowhere near as interesting as videos you may have seen of bike and motorbike armadas playing chicken with trucks in faraway Asian cities.  There do exist controlled intersections, although I’ve seen every category of vehicle, from rusty trike to posh bus, blast full speed through red lights.  And the number of truly outrageous specimens, donkey-drawn carts or rickshaws piled high with crates of live chickens, etc., is minimal.  But shenanigans like steadily pushing through a left turn  against four lanes of oncoming traffic, straddling lanes on the major ring roads, or just plain driving into oncoming traffic for a brief while, are all to be expected from cabbies if not everyone.  I recommend dosing your anxiolytic of choice before your first day of cab rides.

Amazingly, I saw only one wrecked car the whole time, although I was close enough to hear and smell the rubber of some squealing near-misses.

Lots of folks wear flip-flops everywhere, despite the ubiquitous presence of shit (by which I mean not junk, but actual turds of unknown provenance).  Nails and such are fairly common, although broken glass gets swept up quickly by ubiquitous attendants (see “Labor” above).

If you eat enough, and interesting enough, food, you will eventually be served things that you might choke on (bones, shells, inedible bits, etc.).

The glib, and not a little racist, take on all of this is that with so many people, individuals’ lives and safety count for less.  Whether or not this is true, I don’t think it has explanatory power.

My theory is that this state of affairs is the result of limited civil remedies against businesses and the sovereign.  If you effectively can’t be sued or voted from office, then why fix, clean up, or make safe your restaurant, shop, or city?

Safety first!

– Maintenance

Maintenance of capital assets is simply not a natural follow-on to their acquisition in the way we think of as a best practice in the West.

Of course, things are created and built in China very competently and often on a grandiose scale — think of the Bird’s Nest stadium or the Water Cube for the 2008 Olympics.  But I believe that within five years, these gems, if still standing, will have fallen into a shameful disrepair (beyond the norm for disused trophies of expos and games long past, like that weird World’s Fair thingy in Knoxville).  This is because nothing, it seems, gets maintained in China.

My first ten nights were spent in a posh apartment in a high-rise built in the last several years by a Hong Kong developer (your hint of this is when the internal signage is all in traditional characters).  Covered garage under a fountain and lawn; legions of starched-shirt, polite security guards; marble, steel, flat-screen TVs and all that.  We towered above the neighboring tenements.  But despite all of this, there were subtle hints that something was amiss.  A sheet-metal sign near the private tennis courts was bent at the corner into a menacing protrusion.  Some of the flagstones were broken, others wobbly.  The elevators were intermittently functional.

The residents of this high-rise were expats (one gal working for the U.S.  Army was dropping $4000 of your tax dollars monthly on a two-bedroom) or Chinese upper-class folks: surgeons, bankers, and the children of the wealthy.  So, while this building was hardly the very most upscale in Beijing, it was no slouch even by U.S. (cost) standards.  Yet the maintenance of the facility lagged well below what Americans might expect from, say, a well-run Holiday Inn franchise.

Paradoxically, it felt as though we were in a place where capital (initial building cost) was “cheap” while operating expense (mainly labor) was “expensive.”  Of course (see “Labor” above), labor opex is nearly free, while capital (though the issue is complex and nonmonotonic) is constrained.  This makes me think that the problem is probably a mix of management culture and accounting practice.

Other examples of maintenance issues are visible at hotels which have been open more than a few years.  As the Lonely Planet “China” guide opined, many hotels are “manifestly” one star lower than their ostensible rating after having been open a while.

Grand Hotel, Petite Slippers

Finally, I want to make a couple of observations about maintenance of historical sites.  Specifically, on Tai Shan, the holiest mountain of Daoism, there are numerous shrines and temples up and down the slope.  They are often in ill-repair from an infrastructure-maintenance point of view, but this is not because they are in any way being historically preserved for “authenticity.”  Many of them bear plaques indicating their age, but a curious pattern emerges.  The typical plaque says something like:

“The Temple of the Holy She-Wolf has been here since antiquity, but has been rebuilt many times since 1650.”

Is that the same temple that was there in 1650?  Or the same as was there in antiquity?  The writer of the plaque seems to imply that it is the same, merely rebuilt.  Or perhaps it doesn’t matter that it is no longer the same; perhaps there is an Eastern philosophical outlook at play that suggests we overlook the artificiality of the new vs. old distinction.

But if that sounds hokey to you, it does to me, as well.  I think a more likely suggestion is that since some parts of China are so very old, and since history gets measured there in dynasties instead of decades, the “oldness” of a 1970-rebuilt temple vs. an 1890-rebuilt temple vs. a 1770 rebuilt temple vs.  a 1650 rebuilt one is all about the same — on the order of “not that old.”  I also suggest that a sense of permanence accompanies this idea of longevity, so that any urgency is removed from the need to keep things just so.  Combine that with a lackadaisical approach to routine maintenance, and it starts to make sense to just rebuild the Temple of the Holy She-Wolf every several decades after it’s crumbled past some unusable point.

– Content

China is dying for content.  To a first approximation, all content in China sucks.

Television is horrendous.  CCTV (state television) controls several channels, and apart from heavily censored news (with Mao’s portrait over the Forbidden City behind the anchors) and Olympic sports, the content was:

Kung-Fu Movies
Soap Operas
Bland Music Shows

But mostly, Kung-Fu.  I know it sounds like a bad joke, but it’s not.  At any given time you can turn on the TV in Beijing and find at least two channels with some guy in a ponytail kicking some guy in pajamas.  And we’re not talking Jackie Chan or Bruce Lee, export quality stuff — this is mainland Chinese knockoff, crappy effects, no athleticism, martial arts mush.

I managed to flip through the 20 or so channels available about once a day.  The only imported Western content I saw was an overdubbed episode of “Growing Pains,” the 1980s sitcom.

(In non-broadcast media [e.g. DVD or VCD] you can get dubiously licit copies of Hong Kong movies, which are usually still pretty high on the Kung-Fu and the violence but have something approaching plot, cinematography, and acting to bolster it.  And, of course, pirated Western movies are around on DVD, although the Olympic cleanup effort swept most of this into back alley shops.)

In print media, I am told you can get a copy of the NY Times or the Wall Street Journal in a five-star hotel.  However, I never once saw one.  Not in a hotel, not in a newsstand, and not even in the largest foreign-language bookstore on Beijing’s main shopping drag.

China wants content.  Adores content.  Needs content.  And by “content,” I mean both the longer-form stuff — albums, movies, TV shows, songs — that we typically think of, but also the short-form, non-media content that is embedded in products and the built environment, like brands and design.

Look at kids walking down the street.  They want content on their shirt so badly that they wear nonsensical, misspelled knockoffs of Western logos, slogans, and graphics.  Half the kids in the Internet cafes (wang-bas) are watching video-on-demand.

But there are two totalitarian forces standing in the way of the Chinese getting content: the Chinese government, and Rightsholders.

The Chinese government does not want a populace aroused (in either sense; see “Sex” below) by uncontrolled foreign content.  Make no mistake: this is naked fear on the part of the government.  But, they control the outlets and the press, so in a move of simple genius, they just don’t allow any uncensored content.

Rightsholders, those rent-seekers who find themselves quasi-legally entitled, in the West at least, to a monopoly on the use of certain information, are just as scared.  Their fear is just as blind and just as destructive; for more, see “Intellectual Property” below.

Maybe China has spurious DMCA abuse, too?

– Health

China is a ticking time bomb of public health.  Bird flu or SARS are red herrings.  The dominating effects over the next 20 years will be chronic lifestyle issues.

Smoking in China is where it was in the West at midcentury, acceptable (and actually done) literally everywhere I went except for inside airplanes and train cars.  Restaurants, taxis, Internet wang-bas, you name it.  This would not be such a shocker, I admit, if the entire First World hadn’t made a jihad against tobacco its major accomplishment over the last ten years (with even iconically fumigaed French restaurants and Irish bars banning smoking).  But it seems very unlikely that this trend will catch on in China (although trends are propagated in odd ways; see “Law and Government” below).

Deep fried everything is more ubiquitous, in some cuisines, than in a Deep Southern lunch counter.  In those cuisines that don’t subscribe to the fry-everything model, another lurking danger is the taste for pure animal fat.  The costlier a piece of pork is, the more likely it is that it’s got a half-inch thick fat-cap slow cooked and carmelized on top of it.  (The first three such morsels you eat will overwhelm you with goodness, melting like a cross between fine smoked slab bacon and cotton-candy in your mouth.  But then you realize that there remain twenty more bites on your plate, and the thought of eating a half-pound of suet sobers you up quickly.)

American bobos and self-anointed gourmands who get high on “authenticity” (see also “Intellectual Property” below) will be glad to hear that yes, your local “Jade Garden Pagoda” (or whatever) take-out place is doing it all wrong, and that all that soy and oyster sauce, must not be Truly How It Is Done.  But foodies, hippies, and cardiologists alike would be aghast at how much salt, sugar, MSG, and God Knows What Else does “authentically” make it into the food.  Drenched in sugary sauces, salted to the extreme, and systematically selected for fattiness describes just about everything besides staples and vegetables.

People (except those who eat only staples and vegetables) are getting fat in China, and it’s going to get worse.  The standard Beijing or Shanghai adult male summer uniform of short-sleeve shirt tucked into navy blue polyester pants with some kind of plastic loafers, is not complete without a modest (by American standards) but growing paunch and a phlegm-hacking cough.

Booze flows freely everywhere, and beer is the beverage of choice to accompany meals.  (Good damn luck getting a nice glass of red wine outside of a truly upscale restaurant!)

In Beijing and Shanghai at least, the bicycle is no longer ubiquitous.  I saw more bicycles (parked) than electric bikes, but the majority of vehicles actually being ridden around were powered (electrical or tiny motors).

Bikes and mopeds: you tell me how folks are getting around

There exist gyms and health clubs, but in nothing like a proportional amount to the population.

– Sex

Maybe the health and wellness industry is stunted because one of its key drivers is completely squelched.  Chinese aren’t yet whipped into a neurotic frenzy over body image and attractiveness, because there is essentially no sex in the media in China.

By no sex, I mean at any level.  TV and movie content may be romantic but is largely chaste.  The only “bikini” level body exposure in any medium I saw was during Olympic beach volleyball broadcasts.  And rap music video-style booty shaking, Cosmo-style cleavage-showing magazine covers, or outright porno?  Forget it.  It’s just not there.

That’s not to say that there’s no sex industry in China.  It’s just run in a complete mirror image of the way an American might expect: prostitution is ubiquitous and rampant, but harmless looking is completely impossible.  (Gentle reader, trust that I had only your curiosity in mind during my research; suffice to say my conscience remains clean, yea though my mind be incorrigibly dirty.)

There exist “sex shops” in the major cities, but, curiously, they sell only “appliances” and not pornography.

My Shanghai (a more Westernized and racier city by far) hotel ostensibly had adult video-on-demand on the TVs in the room.

Now those are some enticing titles

Massage parlors are commonplace and, with obvious hookerific exceptions, completely legitimate (or rather, I should say it is possible to get a completely legitimate foot rub there).  45 minutes into the hour long foot massage, though, the inquiries begin: do you have a taitai or a nupengyou (a wife or a girlfriend)?  Locals confirm that it is nearly always possible to negotiate extra services, even at these “legitimate” establishments.

Also fairly blatant is the pimp’s street come-on, common on tourist streets after dark: “Hello hello my friend, you like lady?”

Despite the fact that you’re never a kilometer from a negotiably naughty massage joint, there are no nudie or strip clubs at all.  A Beijing Hooter’s franchise is as close as you can get.

I find it hard to believe that market demand is what shapes this skewed-to-extremes adult entertainment industry; after all, one would expect that increasing social opporobrium on the scale from Hooter’s to hookers would suppress incidence.  The opposite is true of market offerings in China.  Which is almost certainly the result of government policy.

– Law and Government

The law and the government (very nearly one and the same) in China use a bizarre mix of laissez-faire and complete thuggery to distort the hell out of various markets at the whim of the relevant officials.

Most of you have probably heard about the dramatic measures the government took to clean the air in time for the Olympics: unilateral shutdown of nearby factories, car rationing by license number, and even cloud-seeding to induce cleansing rains.  Did you also know that the leftmost lane on every expressway around Beijing was repainted with Olympic rings, for the exclusive use of credentialed Olympic vehicles?  Or that every single taxi driver in Beijing was spruced up with a collared shirt and tie, at risk of a 200 RMB fine, for the games’ duration?

These measures sound slightly wacky, and, at least to you collectivist Seattle eco-hippie readers unconcerned with factories’ positive contributions or cabbies’ sartorial discretion, mostly harmless.  (The anti-spitting propaganda campaign, for example, was definitely a plus.)

But an example of something more troubling is the clean-up approach to the crime on Beijing’s premier bar street, the Sanlitun district.  Like any nightlife area, it had an underground economy, and as is common everywhere, foreign (in this case, North African) gangs had mastered the distribution chain for the relevant goods.  The rumor we heard in hushed tones from Sanlitun regulars, as they pointed out the absence of nearly all black folks from the district, was that two weeks before the Olympics, the police simply gave the bum’s rush to any blacks they found in Sanlitun.  The cops logic was brutal but effective, in the Chinese style: some North Africans are known to be dealing in Sanlitun, so let’s drive anyone who looks vaugely like that out of the district.  Problem solved, sort of, and quite messily, in that way that the Chinese government solves problems.

Another “solution” is ubiquitous surveillance.  Tourists enjoying the Summer Palace (which, by the way, puts Versailles to shame both in grandeur and in tastelessness) will see staring back at them, among the odd-numbered gargoyles atop the pagoda roofs, closed-circuit TV cameras in steel housings.  (In a weird echo of Orwell’s telescreen, the name of the Chinese state TV broadcasting monopoly mirrors back the name of the surveillance cameras: CCTV.)

Cops are everywhere in China, though they kind of look like rejects from the third string of the little league minor team — skinny kids in ill-fitting green uniforms — or else slightly more swaggering versions of the universal Chinese adult male (avec paunch, emphysematic hack, and navy blue polyester).  Nobody seems to have a gun, except for the occasional hardcore ninja team that sweeps through transit hubs with machine guns and full armor.

The operation of the cops on the ground is a sort of mirror of the operation of the government at large.  In Qingdao, on the boardwalk to the little pagoda in the harbor (see your nearest Tsingtao beer bottle label for an illustration), hawkers selling seashells, carved bone combs, and similar items do a brisk business selling to the (Chinese) tourist throng.

Qingdao vendors (crouched/seated on the dock) before the cops.

As I walked back toward the shore from the end of the pier, though, I noticed the vendors getting all a-twitter and standing up from their crouches.  A cop car was slowly rolling down the pier.  As the car got about 10 meters away from each vendor (camouflaged by the crowds), each vendor rolled up his seashells and tossed them into the sea, some jumping to follow their package into the water.  Once the cops had made their way down to the end, they stopped, turned around, and headed back — and just exactly opposite the way they disappeared, the frogman vendors and their goods reappeared back in their original places, a bit damp but back to business.  There was no secret as to exactly what each party was doing, but as long as the cops didn’t see the vendors actually in flagrante delicto, everything was kosher.

Where'd they go? (Hint: into the drink)

(Alas, I was too scaredy-cat to photograph the cops themselves.)  The Qingdao seashell sellers exemplify the approach of small entrepreneurs to the law — scrupulous compliance (or at least invisibility) when the eye of the authorities is turned, and business as usual when it looks away.

The government does not always rely on the formula of muscle plus surveillance, however.  Ingenious little incentives are sometimes used, too.

In restaurants (here as everywhere), there is a strong incentive not to ring up cash-paying customers properly, in order to skim tax-free into either employees’, managers’, or owners’ pockets.  This problem is compounded by the fact that everyone pays cash and restaurants rarely use computerized tills.  In order to get restaurants properly to account for charges, though, the tax authorities turn to the customers for help.  Asking for a fa piao (receipt) with a government inventory control number on it (and a carbon copy to the revenuer) gives you, the diner, the chance to win 10 RMB by scratching off a lottery ticket-like box.  (As far as we could tell, this is the only “gambling” permitted north of Macau!)  Your waiter will grumble and pretend not to understand you, but if you repeat “fa piao” enough, he’ll eventually bring you the ticket, fulfilling the tax authority’s purpose.

– Intellectual “Property”

Here in the West we have an authority that has very successfully corrupted our lawmakers into granting it “taxation” rights over the public: the Walt Disney Corporation.  Although our Constitution specifically and unambiguously requires that copyrights be for “limited Times,” Disney and its friends among the copyright holders (the Rightsholders) have managed to turn it into an effectively perpetual rent.  This is, of course, so remarkably lucrative that it inspires phenomenal amounts of both fear and greed.

When the Rightsholders think of any place, medium, or system in which they lack the ability to strangle free speech with their ill-gotten legal cudgel, their immediate conclusion is: “If we let our increasingly digitized content into [place], our rights will be violated and our content will be instantly uploaded with bitwise fidelity into a giant cloud of sharing!  Therefore, blockade and stonewall, and protect our precious rents!”

Think of the RIAA and MPAA reacting to VCRs, Napster, just about every home computer peripheral that can write data, Internet music distribution, etc.  and you can see this pattern repeated over and over.

I am fairly certain that the Rightsholders have this view toward China, as well.  (For much of Hollywood’s sex-drenched pulp, the blockade is unnecessary, as the government would censor it anyhow; see “Sex” above.) This is not due merely to fear that the content might be redistributed back to their home markets digitally, but it is also due to greed at the prospect of eventually selling billions of copies of “Steamboat Willie” or “Eagles Greatest Hits Volume 2” to a pacified middle kingdom.

The more nuanced or subtle (but still conservative business-friendly) view of Intellectual “Property” in the West is that the culture of respect for IP is important to enable R&D and content-producing businesses.  This view makes respect for IP part of a climate that permits innovation and entrepreneurship, and ultimately, economic growth.

This binary view of a country’s attitude toward IP — respected or not respected, rights consistently enforced or not enforced — is completely inadequate, however, to understand how IP is viewed and treated in China.

For starters, I hold that IP in the form of branding is hugely respected in China — in the sense of a recognition that branding holds great power and that it is valuable.  Look at the rising Chinese-native athletic wear brand, Li Ning.  The logo is a clear rip-off of the Nike “swoosh.”  The English-language slogan, “anything is possible,” comes straight from Adidas’ Olympic promotional phrase, “impossible is nothing.”

Does that "swoosh" motif look familiar?

Tidy little syllogisms: if "Impossible is Nothing," then ... ?

The power of a brand is clearly respected in China — why else would they want so badly to rip one (or two) off?  It is only the ownership and exclusivity that is not respected.  The thirst for content is there (see “Content” above), but the willingness to play by the Rightsholders’ rules isn’t.

None of this means, however, that IP has no value.  Quite the opposite.  The value might be different (including “less”) or it might merely be unlocked or monetized differently.  But people are willing to pay for branded goods and content in China.

Two brand- and IP-heavy companies from the West have very clearly figured out the Chinese IP code, in my opinion.  The two are very different, though what they’ve learned is largely the same, so I split them apart here.

Microsoft has the perfect approach to China.  They know full well that the high-priced software they sell in the West isn’t economical for the vast majority of consumers or even businesses.  So, they let sharing (“piracy”) flourish.  As a result, Microsoft is the de facto standard for operating systems, despite the government’s nationalist efforts at an Open Source “Red Flag” GNU/Linux distribution.  As businesses mature and interoperate with the West, their default choice of Microsoft as a platform, originally free (pirated), will result in later paid installs and flourishing of the proprietary Microsoft ecosystem.

Audi, too, has cracked the code, to some extent.  The black Audi A6 is the rough equivalent of the Lincoln town car or the Mercedes S500 sedan in North America and Europe, respectively — the “executive” car or choice.  These Audis, however, are by and large not the newest A6s, with the styling one sees in a posh parking lot in the US, nor are they large-engine (4.2L or larger) powerhouses.  And perhaps most tellingly, they are almost never Quattro (all-time 4-wheel drive) — the sine qua non of Audi’s branding work in the West (see e.g. “nur fuer Audi” mountain roads).  Audi has thrown away the key elements of its Western branding — newest styling, large engine displacement and/or turbo, and Quattro all-wheel drive — in favor of pushing (probably through a shrewd licensing deal) lots of black A6s into the Chinese market.  And the result is very favorable; they are the de facto standard in Beijing (especially for the Olympics) and Shanghai for chaufferred luxury sedans.

– Video

My observations on Video are almost an add-on to “Intellectual Property” (above), and certainly share some common themes.  Video entertainment and communications are treated much differently in China than in the West, and I think the key difference is in “quality of service.”

In China, video is available.  Notice, I don’t say “available to those with 6.0 Mbps download speed” or “available on certain models” or “available for a fee.”  I say “available.”  And what that means is that, by hook or by crook, video is going to find its way down your uplink.

Asia is home to the Video CD (VCD) format(s), the (crappy) VHS-equivalent digital video smorgasbord.  China specifically enjoys a plurality of standards that will squeeze down onto whatever pipe or medium is available.  Hotel VOD (Video on Demand) often appears to be VCD or worse.  Broadcast and cable TV ranges from the shiniest high-bitrate product (main CCTV) to seriously degraded, distorted and discolored signal from local stations.

In any wang-ba of size, you will see numerous folks plugged in and watching video streaming over the Web.  It’s never YouTube, and it’s often washed-out colors with bad quality, but it’s ubiquitous.  Somehow (and my vocabulary was insufficient to the task fo figuring out what codecs and methods these were), the Chinese have figured out how to make video a ubiquitous part of their digital experience without handwringing over HD-DVD, bickering over Blue-Ray, or dicking around with DRM.

This parallels the Intellectual Property observations to some extent: worse is better, and getting your stuff out there is the key thing to do.  How you do it, and how well you do it, takes a back seat to just getting ‘er done (more or less).

– Service

This eminently practical attitude of just “getting ‘er done (more or less)” has a dark side, however.  Doing the bare minimum to get something done is, without a doubt, the default method by which work in China gets accomplished.  The notion of “customer service,” or the phrase “above and beyond,” are unknown here.

Ask a ticket agent for a ticket to Qingdao.  “Mei you.”  (“Don’t have.”) Don’t give up — you asked (implicitly) for a standard fare.  What about a sleeper car?  “Mei you.”  What about hard-class (bench)?  “Mei you.”  What about first-class sleeper?  “San bai yuan.”  (“That’ll be 300 bucks, please.”) Ok, thanks, I’ll take that ticket, and fuck you very much for checking the other classes for me.

Unfortunately, that sort of automaton-like response to your requests is very common, especially where the job is more rote, the customers more numerous, or the employee closer to being in the government’s service.  This is different, admittedly, only in degree and not in kind, from what you can expect from such employees here in the States, but it is very pronounced in China.

Restaurants, from the slightly-above-average on down (except fast food) will not give you napkins, unless you ask, and those napkins are about the same consistency of high-end toilet paper.

Cab drivers are practically speaking incapable (though more likely, unwilling) of reading, or even orienting themselves on, the most basic of maps (even those marked in Chinese characters).  They show remarkable creativity in finding places to drive, but very little initiative in figuring out where you want to go.

This general “I-don’t-give-a-shit” attitude (see “Labor” above) is pervasive but probably explainable with some fairly straightforward analysis.  Tipping is unheard of in China, except for those with some Western experience who will try to hustle newly-arrived laowais into leaving them the change from a big bill.  Quality Control is simply not something that service industries bother with yet.  So without either a bottoms-up or tops-down incentive for improving service, why should anyone care?

– Salesmanship

A little bit more curious is the lack of salesmanship talent in China.  Here, incentives should be rather obvious — commission for sales.  Indeed, the motivation is quite apparent among hawkers at tourist-facing retail outlets.  But everything here gets sold as a commodity — bargained on price — and the notion of selling value to the customer is unknown.

I realize that it’s a bit much to hope a retail clerk to understand “unique selling proposition” or “differentiation” in translation.  But the concepts themselves, I am convinced, are not understood by sellers.  Conversations with Western-educated businesspeople confirmed that even up the value chain, and for larger and larger deals, the same holds true.

My personal theory is that, in China’s dealings with the West for the last 30 years, the price (labor cost) advantage has been so decisive that the competitive pressures that would otherwise lead to differentiation have been absent.  In a sense, the “free money” that the labor cost arbitrage has left on the table has made Chinese business lazy and uncreative about creating value (rather than just capturing the labor cost delta).  As a result, skills at differentiating product, selling value, and articulating a selling proposition are still in development.

It should be noted, however, that the concept (or at least the empty word) of “quality” is certainly known and much bandied-about.  However, it’s recited in almost a caricature of how you might imagine a Chinese merchant — “very good quality!” excitedly repeated about every object, no matter its obvious shabbiness.

– Cargo Cult

The vendors know that saying something is “quality” is supposed to make somebody buy, or pay a higher price for, an item.  But they seem to have no concept about what it is that underlies this notion of “quality.”  In effect, they seem to be cargo-culting the notion of quality.

An aside on the idea of a Cargo Cult: briefly, after World War II there remained certain isolated islands that were once used as airstrips but were thereafter abandoned by the militaries.  The native inhabitants were found many years later to have created religions and ceremonies mimicking the use of runways, radios, control towers, and ground crews, in hopes of eliciting the “magic” cargo from the skies (and doubtless the high-calorie treats and other Western goods the GI’s doled out).  These (possibly apocryphal) natives were said to have created “Cargo Cults,” and the name sticks to this day, especially in the tech industry, as meaning any rote repetition of an action in search of its characteristic response without any understanding of the underlying mechanism of the original process.

Although it may seem to smack of Western arrogance, I can’t shake the feeling that a lot of what goes on in China, and especially a lot of what I have written about above, ends up being reducible to the notion of cargo-culting.  Yes, having a clean sheet over the back seat of a taxi is, standing alone, probably a sign of higher quality.  But it’s of lower quality when that sheet blocks the seat belts from working.  Yes, it’s generally good to have lots of “volunteers” to guide foreigners when you host the Olympics.  But putting a tent up and putting polo shirts on the same four old men who would otherwise be squatting on the same street corner, without any training or maps or information or value-add, completely misses the point of having volunteers.

A less culturally-charged way of saying this might be that the Chinese “go through the motions” for a lot of things.  But the things I noticed are ones imported from the West, which makes them seem cargo-cult-like.

– Conclusion

Much of the above comes across as critical, which reflects the frustration I felt as my third week in-country approached and I sketched out these notes.  I acknowledge that this emotional content would be a weakness of an objective account; however, this is meant to be a thoughtful but subjective and opinionated piece.

I left China without either a great apprehension of the rising hegemon, nor with any sense that our Western advantages will hold past the horizon we today can see.  But my perpsective /was/ changed and honed.  The measures that one might naively use to measure’s China’s great catch-up — % GDP growth, number of universities per capita, degrees awarded, power plants constructed, durable goods orders — these are all meaningful in their own fashions, but are red herrings when it comes to commensurability with the West.  Things like civil institutions, the culture of business, salesmanship, and service, IP regimes, and cultural mores will ultimately mean more than these easily quantifiable (and easily falsifiable) figures.

The experience also has helped highlight for me those cultural and institutional parts of American culture that might otherwise be taken for granted.  We must not discount the role of things like R&D, value-based pricing, the emphasis on industrial standards, and individual initiative within large organizations, for the effect they have on our collective wealth and national effectiveness.

All that said, gentle reader, I have only scratched the surface and can’t wait to get back to learn and experience more of the country that is destined to be our greatest trading partner, our greatest military rival, and our greatest source of learning during the next century.  This is my account; now, please correct, challenge, and contradict me at will.

The other Lighter Capital

August 10th, 2011

Today, the team at Lighter Capital (formerly RevenueLoan) kicked off a promotion, where we commit to investing $500k in a company that applies online by 31 August 2011 (that’s 21 days and counting).

We did lots of the usual things, plus some unusual things, to get the word out — press releases, blogger outreach, videos, tweets, etc. We’re pleased to trumpet our “Lighter” name, as indicating both a lighter-weight process and a lighter attitude than the traditional banks.

But in our research we discovered: we’re not the only “lighter capital” out there.

In fact, the industrial city of Wenzhou, south of Shanghai on China’s eastern coast, claims the title of “lighter capital of the world.” To wit:

In Wenzhou, there are more than 500 lighter manufacturers that produce 5,000 kinds totaling 500 million lighters each year. Among that, 80 percent are exported abroad. Lighters from Wenzhou make up 70 percent of the world’s market for lighters with metal shells, and 80 percent of the European market.

From the China Daily, http://www.china.org.cn/english/2002/Apr/31597.htm

So, to our “lighter” brethren across the big pond — 你好, 我们爱你的名字!

Force a reference to System.Core in Visual Studio 2010

April 9th, 2011

There are reasons why you might need to add a reference to “System.Core” to your Microsoft Visual Studio project. (For example, if you wish to compile/build both inside the IDE and from the command-line with MSBuild.exe.)

However, if you try to do this through the IDE, it will barf at you: “A reference to ‘System.Core’ could not be added. This component is already automatically referenced by the build system.”

Alas, that’s a big fucking lie. It’s referenced by the IDE when it invokes the build system, but not by MSBuild itself. So sometimes, you indeed must add such a reference, but you can’t do it from IDE-land. So close VS2010 and fire up vim. Add a line to the .csproj file in question, telling it to:

<Reference Include="System.Core" />

Hat tip to Ashby at StackOverflow:
http://stackoverflow.com/questions/1302488/the-type-or-namespace-name-linq-does-not-exist-in-the-namespace-system-data/4331322#4331322

I got to this point when recompiling something for .NET 3.5 that had originally been coded/built for .NET 4.0; it wouldn’t import the LINQ namespace without being told to import System.Linq, but then couldn’t find it without the System.Core reference being made explicit. Arrgh.

Fukushima-type reactors in the USA

March 22nd, 2011

I was moving a bookshelf around my house this weekend when I found a copy of “Nuclear Power Reactors in the World,” an April 2000 publication by the IAEA.  (Don’t ask why I have this kind of crap lying around …) It struck me that people would want to know what reactors are “like” the Fukushima Daiichi reactors in Japan which have been causing all sorts of problems lately. Well, here goes:

View BWR 1970s reactors in the USA in a full screen map

The March 2011 earthquake / tsunami / reactor emergency brought to the public eye the dangers of “active safety” in engineered systems.  Elements of reactor design and operation which may have seemed appropriate in the 1960s (when these reactors were designed) now seem like “what were they thinking??” anachronisms to concerned laypersons.

Specifically, I’m talking about the need for electrical pumps to be in continuous operation to prevent reactor core overheating; use of water (hydrolysible into 2H2 and O2, explosive and reactive gasses) as coolant; use of cladding and fuel alloys that are subject to fire risk and enhanced toxicity (zirconium and MOX); and storage of spent fuel rods in top-floor containment pools subject to sloshing and evaporation and requiring electrical pumping. Keep in mind that all of the bad shit at Fukushima started happening after the earthquake and tsunami had passed, during a period where the active safety systems relatively slowly stopped working.

(Lots of folks don’t realize it, but you can build stable, passively safe, high tech systems, to a degree. Simple airplanes are built to fly themselves. If you’re up in the air piloting a Cessna in level flight at, say, 5000 feet, you could probably take a 10 minute nap and live to tell about it. Yes, there’s gravity involved, but the aerodynamics involved let the plane stay up there either flying (engine on) gliding (engine off) for quite a while with no requirement for constant input and management. Contrast this with something like the Joint Strike Fighter, where the plane is intentionally aerodynamically unstable and, without the constant inputs of a high-speed computer, would fall out of the air like a brick. We want reactors that are boring and Cessna-like, not delicate JSF divas that literally melt down without enough attention.)

(Non-geek version: the Fukushima-type reactors are like delicate plates spinning on top of poles.  You can’t just leave them be without expecting to break a lot of shit.  And they don’t tend to revert to safe or stable states when they break.)

The above map names the US-based reactors with BWR type (boiling water; arguably the most dangerous type still in service), manufacture by GE (GE, Toshiba, and Hitachi were the suppliers at Fukushima), and construction dates that include the 1970s (1969 in the case of Nine Mile Point).  This does not mean that you should freak out if you live near these plants.  But it does mean that, in the broadest sense, these types of reactors are subject to the same types of risks as the Fukushima reactors.  (Keeping in mind that even Fukushima was fine for 30+ years until a 9.0 earthquake.)

If you want to do something positive about nuclear power in general, don’t freak out or ask for all nukes to be banned. Instead, the nuke-minded citizen should:

  • …push for greater research on safer alternatives like pebble bed reactors.
  • …push your Congressional representatives to get off their asses and open up a real, centralized, better-than-inaction interim solution for the nation’s nuclear waste. (This gets rid of fuel rods sitting in ponds at the very place where they can do the most incremental harm when things go wrong…)
  • …pay, pay, pay. [Good] nuclear power will not be cheap. But it can be vastly improved from the Fukushima state of affairs. However, it will take enormous amounts of money for research, and the political will to eschew interim half-assed solutions (like putting cheap BWRs into service well into the 1970s, when other approaches were already either viable or in progress).

Why is Port 21 apparently open on my firewall?

February 28th, 2011

Scenario: You set up a server somewhere on the public Internet.  You lock down its ports to the minimal subset you need using firewall(s).  Yet, somehow and for some strange reason, nmap reports that port 21 (FTP) is open on your server!  Sure enough, you do a “telnet myhost.cxm 21” and it connects!  Shit-damn, what’s going on??

Don’t bang your head against your iptables or pf or PIX or ASA config.  First, check to make sure that the environment you’re checking from behaves right.  Do a “telnet google.com 21” and see if it connects.

Some NAT setups in offices apparently try to do some stateful inspection of outbound active FTP in order to rewrite the addresses/ports involved, and these can intercept outbound requests on port 21, making it seem like any host is picking up on that port.

Try nmap’ing or telnet’ing from an outside host directly connected to the public internet.  And make sure that your subsequent security scans/checks come from such a host.

Capital As A Service: A Manifesto

February 24th, 2011

We have been misled.  We, the entrepreneurs, early employees, and investors who power the world of technology startups, are told that “everything has changed.”  Everything is now “agile,” “lightweight,” and “flexible,” and it’s all going to be available to us “as a service.”  We create user-friendly experiences, using elegant free open source frameworks, running just as many EC2 instances in the “cloud” as we need at any given moment.  We coordinate the world’s information by mashing-up APIs from across the Web, and we coordinate our dev teams with daily Scrum standups and free collaboration tools.  A book can tell you how to have an “epiphany” and learn all this for yourself, and a thriving startup culture on the coasts, in the Rockies and Chicago, and elsewhere will support you.

But everything has not changed.  Just try raising capital.

Software is now a service.  Hardware, indeed, infrastructure is now a service.  Need an office?  Use a Web service to pick a short-term coworking space.  Outsourcing?  Sure — there’s a service to manage those service providers.  Hell, the boys at OnCompare now have a service to help you select the services.  Everything you need is discoverable, trialable, and available 24/7, online, with a few clicks and a credit card, right?

But the moment you start to feel the rhythm, decide you want to dance to the music, and try to roll up funding to grow one of these agile new businesses, the record screeches to a stop.  Needle scratch, and silence: a disco full of folks staring at you.  Are you crazy?  You want what on demand? Capital as a service?

Yes.  You want just enough of it, just when you want it, conveniently and as automated as possible.  You want to try it out in 10 minutes, understand it, trust it, and, if you like it, use its APIs to integrate with your business processes.  This is how you advertise for clicks, how you get a new logo designed, and how you provision servers.

So what is money’s major malfunction?  When you can get 100,000 virtual server instances started for you in a minute, why does it take days and weeks (or worse) to get $100,000 in working capital?

Something’s wrong with this picture.  And it’s about to be fixed.

Find a money-man and ask him about “efficient markets.”  He will give you a sparkling smile and tell you the MBA answer, that markets tend to squeeze out transaction costs, and costly middlemen, and price gaps.  Then ask him about the cost of doing transactions with him.  Or ask if he’s a costly middleman.  Or ask what “price” he pays his investors to use the money.  Is he still smiling the MBA smile?

We like having coffee with our investors.  But you shouldn’t have to savor a fine latte from Cherry Street or Coupa to get funding.  Sand Hill Road is a lot more fun to travel on a bicycle carrying a picnic, than in a rental car carrying a pitch deck.  And banks are more fun once they’re turned into bars.

Money, especially the “buy side,” loathes change.  From where Money sits, everything is fine: by definition, wherever the Money is, folks are feeling pretty flush.  And so the Money will resist change, it will cling to its prerogatives.  Bankers would still be on the golf course by 3 PM if ATMs hadn’t revolutionized their customers’ expectations.  For heaven’s sake, it’s the year 2011, and the New York Stock Exchange still closes at 4 PM.  The barbarians are at the gates: it’s about time for the “buy side” to get a little less comfortable.

About to go public on the Big Board?  Toying with a half-billion M&A offer?  Trying to pioneer commercial space travel?  Building nuclear submarines?  OK, you want old-school money from old-school money-men, with old-school suits and rich mahogany, Corinthian leather, and white-shoe lawyers.  But rolling up the cash to take your SaaS company from $2 M to $4 M next year?  It it worth 12 weeks of pitching and partner meetings and Purell between uncounted firm handshakes, with no guarantee of success?

We think not.  For a specter is hanging over the Internet: the specter of capitalism.  And we believe that capitalism should and will deliver its promise as a service.  We at RevenueLoan are not the only ones who see this; we may not even be the ones who ultimately realize it.  But make no mistake: capital for growing technology companies is going to be available on-demand, as a service, and players in this market who ignore this trend do so at their peril.

secedit for setting security policy in windows server 2008 r2 server core

January 15th, 2011

Concepts:

There is a policy running on the system. There may be one or more databases in .sdb files which are files representing a possible policy that could be run. These are stored in c:\windows\security\database\. One may “export” a policy out to a configuration .ini file (the docs for secedit say .inf, but it is clearly the venerable .ini format), which policy comes either from a database .sdb file, or from the current running system policy (if no policy is specified when running secedit.exe on the command line).  One may edit this configuration .ini file (the docs ambiguously call it a “security template” as well, but the command line options all say “cfg”).  You then create a new security database .sdb file with the “import” syntax.  Contrary to a lot of stuff on the Web, you don’t need to put it into some particular magic database (but see below for path gotchas) like the original secedit.sdb; put it in a new one.  Once you have a new, valid, legit database .sdb file, you only then use “configure” to apply the database file to the current system.

Lots of gotchas here.

Sometimes when secedit.exe fails, it is silent, like a good UNIX program, but it will return an %ERRORLEVEL% so check that or you will be bamboozled.  It is noisy when it succeeds and sometimes even noisier when it fails (except when it’s silent).

Secedit silently failed in many confusing ways if either the security database .sdb or the configuration .ini was located on my z: drive, which in this case was a VMware shared folder on a Mac OS X system.  Move stuff to a C: temp dir, then clean up afterwards, because hey, writing xcopy lines in batch files is fun.

The configuration .ini files are in full on, utf-16 format.  Two fucking bytes per character.  Nice.

If you try to create a brand-new configuration .ini file without reference to anything, you do not get a listing of the default settings, but rather an unhelpful, nearly-empty file that informs you that Description=Default Security Settings. (Windows Server).

If you try to look up the values for the various sections, you just plain can’t get them anywhere.  If you go to this horrible javascript monstrosity of a reference site and click Technical Reference (for Windows Server Group Policy), you can get a giagantic, unstructured spreadsheet that has relatively lengthy (but non-technical) prose about the various settings.  But it won’t tell you which sections they go under in the .ini file, nor the requirements for each field, some of which are registry settings and some of which are not.  If you want to see something that was apparently done as a class project by a charity course taught for retarded non-native English speaking remedial computer science students by the generous and distinguished engineers of Microsoft (who are clearly working on better and more interesting problems than, oh, say, making sure the OS’s core security API is sensical and internally consistent), you can look at this horrifying jumble of shittiness, which can’t search for shit but mollifies you with funny Engrish when it fails (I am not kidding: “We probably hit search limit.  Try to redefine your search string.”) and as a bonus demonstrates the sloth of Azure serving AJAX (because, you know, actually putting the documents into an html page where anyone’s browser “find” function could speedily search for it would lack the pedagogical value to the retard-children-programmers).

You can maybe stay sane if you learn that the registry values are prepended by an integer and a comma, where that int seems to specify the data type of the reg value (4=integer, 7=text, 1=some other kind of text).

Specifically in the [Event Audit] section, there are values that are not registry values.  They are ints that appear to be bitmask math fanciness.  (Remember setting options on visual basic windows back in the ’90s, where you got to add up powers of 2? ).  It so happens that they all have two bits, the first one being “log successes” and the second being “log failures.”  So 0 is neither, 1 is successes, 2 is failures, 3 is both.  But this isn’t, as far as I can tell, anywhere on MSFT’s site and it’s sure as fuck not in the giant unstructured spreadsheet reference.

Much of the configuration .ini file can be omitted (so you can just overlay the parts you want).  But you MUST include the [Version] and [Unicode] sections or it will barf.  Use secedit /validate to check it.  However, “validate” does not mean that it will actually round-trip and work right; it doesn’t check the security identifiers in the [Privilege Rights] section so it’s quite possible to have a valid cock-up (see round-trip gotcha below).

Biggest one: secedit CANNOT ROUND-TRIP. The security policy “export” may will (and does for me) produce an output with entries in the [Privilege Rights] section that refer to “Classic .NET AppPool” among others.  If you try to import and configure with this, you’ll get “No mapping between account names and security IDs was done” in the error log.  Turns out you have to manually fix this by adding “IIS AppPool\” before the names of these AppPool entities.  (Hat tip)  If you want to actually find out whether that, or some other hackery, fixes it to something that can be mapped to an SID, find yourself the PSTools download and test the name with PSGetSID.exe.  Awesome.

The “configure” option only really needs a .sdb database specified by /db.  If you give it an additional /cfg parameter, it will muddy up the .sdb with the contents of the specified config ini.  There is no benefit to using this, ever, other than skipping a step that could result in you keeping a sanity-preserving intermediate state backup.

The “overwrite” option doesn’t do what you think it does.  Especially with “configure.”  Just don’t use it, unless you are planning on destroying what is in your .sdb file(s).  The .ini configuration already wins in a tie.

The “configure” option is NOT ATOMIC, and it will happily set your system’s security policy partially to be what was in the file you indicated, and partially not (for example, with the broken round-tripping of IIS AppPool names).  There’s no way to find out whether or not the configuration will succeed, except to “suck it and see.”  And once it does partially, non-atomically make a goulash out of the then-current and database-specified settings, there’s no way to tell what succeeded or failed, except by reading the log, which is formatted in an unparseable mess.  Fantastic.

Sun Beams, Snow Banks, and Small Businesses

November 23rd, 2010

Seattle was inundated yesterday by a steady snowfall during which it was cold, then warmer, then colder again: AKA, a recipe for icy road disaster (at least in a city of 142 square miles with 26 snowplows).

Today’s morning news and communications, then, were dominated by transportation-related issues.  “Schools are closed!”  “Courts closed!” “Clinics closed!”  “Stay home by all means!”

That wasn’t an option today at RevenueLoan — we had a closed deal to paper and two more to work on.  So I put on the hiking boots and the bubblegoose, and navigated foot-mobile through the mostly-closed Seattle streets.

And, while abnormality was the order of the day — 30 year old Seattleites sledding and sliding around like giddy 5th graders, hilly streets barricaded, and creep-crawling traffic throughout the day on main highways — what struck me most was the acute normality of the day for the small businesses I passed.

Local printing company — “open” for business.  Our caffeinated home-away-from home at Moka’s Cafe — check.  Tiny bookstore slinging used paperbacks and tacky tourist t-shirts — you bet.

The office windows across the street from mine, usually packed until 6 or 6:30 with yuppies at an Anonymous Top Online Retailer, are empty at 4:30.  A Major Local Operating System Vendor was beseeching people to stay home.  And, that’s fine — arguably most of the people out there driving today were assholes endangering themselves and others (at least if my anecdotal observations can be extrapolated).

But there’s something heartening about seeing that neon “Open” sign lit up — not the giant custom one in the corporate standard font, but the red-and-blue one you can buy at Costco when you’re first hanging a shingle.  Something heartening about the proud real-estate guy giving the walking tour to prospective tenants on pavement he just scraped and salted.  Something heartening about the beer distributor’s careful truck maneuvers as he pulls up to the corner bar to restock it for happy hour.

OK, fine, cynics: I know that these people are responding to the iron economic law that governs small business, and the simple reality that fixed costs don’t go away.  There is no East-coast failover data center for the guy who makes sandwiches on the corner, and there’s no corporate balance sheet to pay him his take-home if he no-shows.

But I think it’s more than that.  People doing their work because the work itself is valuable.  Yes, bonds have value.  Yes, big corporate edifices have power.  But imagine a world of 6 billion bondholder rentiers.  Who makes a tasty Jambo sandwich?  Who actually prints up your annual reports?  The work we do, cumulatively, is what makes humanity wealthy (and human, for that matter).

And those small businesses are the individual loci of non-abstractable human work.  The smallest functional unit that can deliver the value they do.  And they show up despite the snow.

So maybe it was the bright sun in the sky, reflecting off of the snowpack, and giving a blessed vitamin-D-blast on a Seattle winter’s day.  But something about the walk this morning, and the “open for [small] business!” that came to me from shop windows and storefronts, gave me a cheer.

Web.config and App.config file gotchas

November 19th, 2010

If you try to use idiomatic .NET, and you have even modest configurability architecture requirements, you will almost certainly want to use the *.config system (App.config or Web.config). According to old hands at Win32 programming, this is quite a step forward from *.ini files or registry manipulation. Perhaps so.

However, the *.config regime is extraordinarily fragile and surprise-prone once you start trying to do more than just add name/value pairs to the <appSettings> section. The following are some gotchas that I hope you can avoid if you have to deal with this.

.EXE assembies get App.config, and Web DLLs get Web.config, but non-Web DLLs (e.g. tests) get App.config.

.EXE assemblies look for filename.exe.config, which is in App.config format.  Normally, DLLs do NOT get a config file; rather, they inherit / acquire whatever config is in place in their runtime environment.  But there are two important exceptions.  Web services / sites get built as DLLs.  Their execution environment (presumably IIS or the dev server) looks for Web.config and its format.  Test projects (of the MS type that Visual Studio 2010 makes by default) get built as DLLs, as well, but they get run by (mstest? vstudio?) an execution environment that looks for an App.config file.

So, to sum:

  • .EXE => App.config
  • .DLL Web project => Web.config, via its server runtime
  • .DLL Test project => App.config, via the test / IDE runtime
  • .DLL other => none, inherits runtime environment

Sections such as <appSettings> can be externalized into other files, but there are two subtlely different and incompatible ways to do so.

Specifically, you can add a “file” or “configSource” attribute to your appSettings section.  If you use “file,” that file will be read and will override default values that are set in that section in the .config main file.  If you use “configSource,” however, you must not set any values in your .config main file, and instead must entirely scope out that section (and that section alone, save for the XML declaration) in the file whose name you specify.

Frustratingly, “file” and “configSource” have different rules for what may be included (relative / absolute paths, parent directories, etc.).  Especially restrictive are the rules for Web.config, I believe, though I don’t have them straight.  Effectively what this means is that if you have several Web projects that require a shared configuration section, you cannot put your customSection.config in a parent directory and have your projects pull it in (thereby keeping a Single Point of Truth); rather, you have to propagate multiple copies out to all of the sub-Projects (ick).

For more on this: re: configSource, re: file from MSDN.

Web.config settings are mostly inherited from machine.config down through a hierarchy, but confusingly stop being inherited at the sub-directory level in IIS.

Sometimes, or at least most of the time by default, Web.config settings for a given directory are merged with those of parent directories, and are merged with machine-level config as well.  This can lead to somewhat unfortunate results if you have an app in a subdirectory of another app with divergent configruation requirements.  This fellow seems to have figured out how to resolve this.

Be alert, though, because not all settings *do* propagate properly.  First, a parent Web.config can indicate that its settings should not be inherited.  Second, collection settings are merged together, not replaced, by child specifications.  Third, some settings, just seem stubbornly not to propagate (see this MSDN article which suggests that “anonymousIdentification” does not propagate because it is a secret never-properly-set default magical element).  Finally, the above-quoted MSDN article raises the good point that Web.config only applies to ASP.NET stuff, and that there is an entirely different regime for static content and plain old ASP files.  So watch yourself, there, Tex.