Stocks, broadly speaking, have been hammered in 2022. At this point, only two S&P 500 sectors are trading in the green year to date: energy and utilities. But the big gains for many energy sector players are unlikely to persist in the long run because of the ongoing shift away from fossil fuels, and utility stocks are considered defensive -- they don't typically generate the high growth returns many investors are looking for. 

Those types of gains can often be found in the technology sector, but only a handful of companies in that group can be considered safe at times like this -- among them, Microsoft (MSFT 0.41%) and Apple (AAPL 1.27%). They may not be immune to stock market turmoil, but they have time-tested business models with decades worth of success under their belts. That means when the economy bounces back, these companies will probably be among the first to recover to new highs. Not only might they help investors weather the present volatility, these two stocks also look like solid long-term bets for any portfolio. 

1. Microsoft serves both consumers and businesses

Most people know Microsoft for its Windows operating system for computers and its Office 365 digital document suite. After all, billions of people worldwide use those products in both personal and business settings. But the company has expanded far beyond its roots and into areas its early backers probably never expected, amassing a $1.7 trillion market valuation in the process.

Having diverse revenue streams is extremely beneficial for a company during an economic downturn. At the moment, consumers are tightening their belts on discretionary spending due to high inflation and rising interest rates, so Microsoft is experiencing softer demand for hardware like its Surface laptops and Xbox gaming consoles. But its intelligent cloud segment is picking up the slack in a big way, and it now contributes the largest share of the company's revenue.

It's driven by the Azure platform, which helps businesses operate in the cloud. It offers solutions like data storage, virtual machines, and even cybersecurity. With more of the corporate world adopting this technology, the cloud is on track to become a $1.5 trillion annual opportunity as soon as 2030, according to one estimate by Grand View Research.

In Microsoft's fiscal 2022 (which ended June 30), Azure's revenue grew by an estimated 45% (based on a calculated average of reported quarterly growth reports because Microsoft doesn't release Azure's actual revenue) while the rest of its business expanded by just 18%. 

But still, even though Azure is helping Microsoft weather the current unsteady economic conditions, growth from its other segments will likely kick into gear once interest rates level off. For that reason, it's important to zoom out and focus on the big picture because, as the below chart suggests, Microsoft has been a fantastic long-term investment.

A chart of Microsoft's growing annual revenue.

With Microsoft stock currently down 30.6% from its all-time high, this might be a great chance to buy ahead of its next potential wave of growth. 

2. Apple continues to innovate and diversify

Apple is the largest public company in the world with a valuation of $2.4 trillion, and it just launched its latest smartphone, the iPhone 14. As exciting as that is, it does highlight one of the company's (minor) weak spots. As a manufacturer of premium-priced consumer electronics, Apple is very exposed to the health of the economy. But it has been diversifying its revenue base over the last few years by offering a portfolio of services, and that segment of its business is carrying the company's growth at the moment.

Those services include Apple Pay, Apple TV+, Apple News, and Apple Music, to name just a few on an expanding list. The key benefit for investors is that the services segment delivers a gross profit margin of 71% compared to 52% for Apple's hardware products. Put simply, it's more profitable to deliver subscription-based services to customers than it is to sell them devices, and recurring revenue makes it easier to build scale.

In the company's fiscal 2022 third quarter (which ended June 25), its services segment accounted for 23.6% of the company's $82.9 billion in total revenue. During the prior-year period, it accounted for 21.4%, so it's gradually becoming a larger part of the overall business. In fiscal Q3, services grew by 12% compared to a 1% contraction in hardware revenue.

That said, the release of products like the iPhone 14 and the new, rugged Apple Watch Ultra will likely boost sales into the Christmas season. Both devices come with new feature sets. Of particular note, Apple did a major internal redesign on the iPhone 14 that made it easier for technicians outside the Apple ecosystem to repair them, a cost-saving option that could give the devices even greater appeal to consumers. 

With Apple stock down 17.4% from its all-time high, this could be the chance some investors have been waiting for to buy shares at a discount.