Common advice against contracting bacteria or viruses is to wash your hands because soap dissolves the viruses' shell. Does any organism exploit this effect to protect against infection, e.g. by having detergents in their mucous membrane? If not, why not?
To answer your question, consider why soap is effective against bacteria and viruses. The chemistry of detergents allows them to interrupt the lipid layer that surrounds cells and some viruses. Viral envelopes, in particular, tend to be composed of host-derived phospholipids and proteins. So, if an organism were to synthesize a detergent to fight off a pathogen, that detergent would pose a threat to the producing organism, as well.
Edit - most air-breathing animals produce a pulmonary surfactant in their alveolar cells, though the purpose of these molecules is lung function and cell size regulation, not immunity.
I note that the body of your question refers to bacteria as well as viruses.
The following review discusses the antimicrobial effects of amphipathic bile salts, including a detergent-like mechanism:
Bile acids are surface active, amphipathic molecules, and their detergent activity damages cell membranes. Not surprisingly, many bile-sensitive mutants of both Gram-negative and Gram-positive bacteria carry mutations that impair membrane integrity. Likewise, electron microscopy studies have described a shrunken phenotype in Propionibacterium freudenreichii cells exposed to bile. Enzymatic assays in E. coli, Clostridium perfringens, and Lactobacillus acidophilus have shown that bile causes leakage of intracellular material. Factors that influence the severity of membrane disruption are as follows:
(i) Concentration of bile, high concentrations will dissolve membrane lipids, causing leakage and cell death. Low concentrations of bile may have more subtle effects on membrane fluidity and permeability by altering membrane-bound proteins or increasing trans-membrane divalent cation flux. Low levels of bile can also alter the hydrophobicity of the cell surface.
I haven't looked into this deeply, so I'm not sure how pronounced these effects are in vivo. There are other mechanism by which bile salts exhibit antimicrobial effects (also discussed in the review). Another review suggests that bile salts may have a similar, detergent-like effect on enveloped viruses:
Attention should be called to the possibility that the detergent effect of bile acids as a physico-chemical defense mechanism may not be confined to protection against bacterial endotoxins. Theiler has noted that yellow fever virus and other arthropod-borne viruses are inactivated by mixing them with bile or sodium deoxycholate. However, the viruses of poliomyelitis, murine encephalomyocarditis, and Coxsackie virus are resistant to bile acid treatment. These observations have become a significant means of virus classification; viruses have been divided into two large groups, one being sensitive and the other resistant to sodium deoxycholate. The potential pathogenic and prophylactic significance of these observations has not been suggested heretofore. It is concluded that only those viruses with lipoprotein coating (peplon) are sensitive to this detergent action under in vitro conditions. Those with no such covering are resistant. The examination of the effect of bile acid on Aujeszky virus of the herpes group has demonstrated in vivo that bile acids do have the capacity to protect the organism from such viruses.
There is also a model for the mechanism of amphipathic antimicrobial peptides that involves a detergent-like action:
Whereas detergents at very low detergent-to-lipid ratios can have a neutral effect on model membranes, or can even result in their stabilization, openings might form temporarily at intermediate concentrations, and membrane disintegration becomes apparent at higher peptide-to-lipid ratios. Disintegration of the bilayer takes into account the loss of the membrane barrier, dissipation of the transmembrane electrochemical gradient, loss of cytoplasmic constituents and concomitantly interference with the energy metabolism of living cells, all being observed also with antimicrobial peptides.