I just reread Connie Willis's essay "Learning to Write Comedy or Why It's Impossible and How to Do It" from Writing Science Fiction & Fantasy, and while I love all her examples--Shakespeare, Wodehouse, Jerome K. Jerome, Twain--her overall point is that comedy is about turning things on their head, setting up and knocking over assumptions, ending up someplace different than expected.
So we have a camel eating Twain's overcoat, described as a several course meal; we have Jerome's Three Men in a Boat trying to open a tin can that ends up nearly breaking all of the them, as well as a tea cup; and we have a salesman in Heinlein describing soap as made of finer ingredients, improving your chance of Heaven, and refusing to take the Fifth Amendment.
Which is all very well and good, since you can describe things in a silly manner when the reader knows what you're talking about. You can leave things out (such as any joke that ends with "that's not my leg" never tells us what it is); or add things in (Wooster talking about his aunt's chef as the most important person in the world is only funny because we know how narrow Wooster's worldview is)--as long as you have a shared frame of reference.
But what if you're on a spaceship dealing with aliens that are sentient clouds? (As is inevitable.) How do you get a shared frame of reference so that you can make something unexpected into something funny?
It seems that a lot of science fiction/fantasy humor relies on the frame of reference provided by other sf/fantasy stories. Example: Howard Waldrop's "Night of the Cooters" is a version of Wells's War of the Worlds if some Martians landed in Texas, playing on the shared references we have for alien invasion stories and for Texans.
There's also, I would argue, a larger shared frame of reference for the logical and the absurd. For instance, Douglas Adams's discussion of the Babel Fish in Hitchhiker's Guide to the Universe largely relies on piling absurdity on absurdity: fish don't go in the ear; a telepathic fish used as a communication device is a little odd; and the inset story of how Man uses the Babel Fish to disprove God's existence by proving God's existence caps the absurd proposition of the Babel Fish with a discussion of logical absurdities. But still, we have to share some frame of "common sense" and "logic" for that to be funny.
I sort of hate to discuss humor in so serious a manner (oh, who am I kidding, I love it), but it seems that every comic story has to set up its assumption in order to frustrate them by going somewhere unexpected. Still, it seems the writer of sf/f comedy faces the double whammy of explaining the situation (alien worlds, aliens, quests!) and adding the punchline, without the punchline accidentally being taken for a serious statement.