Two New York City police officers were recovering from gunshot wounds on Friday after a man opened fire on them inside a public housing project before fatally shooting himself, police said.

The shooting occurred at about 8 p.m. on Thursday in an apartment building in the Bronx while three officers were conducting routine patrols.

During the search, the officers encountered two men in a sixth-floor stairwell. One of the men abruptly pulled out a gun and shot at the officers, grazing one on the face and hitting another in the abdomen, police said.

The suspect, identified as Malik Chavis, 23, fled into an apartment on the seventh floor. When officers gained access to the apartment, they found Chavis dead of an apparent self-inflicted gunshot wound to the head. A semiautomatic firearm and a shotgun were found in the unit.

RELATED: Two NYPD officers shot, one in face

Several people who where inside in the apartment at the time were being questioned by police. The second man who was in the stairwell when the gunfire erupted was taken into custody.

The wounded officers, identified as Patrick Espeut, 29, and Diara Cruz, 24, were being treated at a local hospital and are in good condition, authorities said.

"(This) is another example of what our officers confront every single day, keeping us safe not only on the streets of New York City, but in the stairwells and the hallways of our public housing developments," Mayor Bill de Blasio told a news conference.

The incident coincides with the trial of New York Officer Peter Liang, who was charged with manslaughter in the fatal shooting of an unarmed black man named Akai Gurley in a stairwell in a Brooklyn public housing project in 2014.

Liang, as well as Espeut and Cruz, are part of a city program that assigns officers, many of them rookies, to so-called "vertical patrols" of stairwells in high-crime public housing.

The practice has come under fire since Gurley's death by critics who say it violates privacy and puts officers and residents at undue risk.

Liang is expected to take the stand on Monday at his manslaughter trial.