저는 C#으로 시작 했으므로 여기에 조금 머물러 있습니다.WCF http 서비스에서 사용자 지정 오류 메시지를 반환합니다.
사용자가 특정 리소스에 액세스 할 수없는 경우 사용자 지정 오류 메시지를 반환해야합니다. 서비스에 대한
인터페이스 클래스 :
[ServiceContract]
public interface IPatientDemographics
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "GetPatientDemographics/{strHospId}/{strView=mobile}",
ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
[return: MessageParameter(Name = "PatientDemographicsResultSet")]
List<PatientDemographics> GetPatientDemographics(string strHospId, string strView);
[OperationContract]
[FaultContract(typeof(AccessError))]
[return: MessageParameter(Name = "Error")]
AccessError ReturnAccessError();
}
[DataContract]
public class AccessError
{
[DataMember]
public bool Error { get; set; }
[DataMember]
public string Message { get; set; }
}
서비스 클래스 : 사용자가 액세스 권한이없는 경우 순간
[ServiceBehavior(AddressFilterMode=AddressFilterMode.Any)]
public class PatientDemographics : IPatientDemographics
{
public List<PatientDemographics> GetPatientDemographics(string strHospId, string strView)
{
AuthenticationDAO authenticationDAO = new AuthenticationDAO();
string userName = GetUserNameFromHeader();
if (authenticationDAO.CheckPatientSealAccess(strHospId, userName, "-1", "N"))
{
PatientDAO patientDAO = new PatientDAO();
patientDAO.AuditPatient(strHospId, userName);
return patientDAO.GetPatientDemographics(strHospId);
}
else
{
AccessError serviceData = new AccessError();
serviceData.Error = true;
serviceData.Message = "User does not have access to patient record";
throw new FaultException<AccessError>(serviceData, "User does not have access to patient record");
}
}
}
, 이것은 단지 null 객체를 반환합니다. 나는 FaultException을 던지려고했지만 행운이 없었다. 이상적으로는이 같은 JSON 형식 뭔가를 반환 할 :
{
error: "User does not have access to patient record"
}
".NET 웹 서비스"가 아닙니다. WCF입니다. 질문에보다 정확한 태그를 달아야합니다 : D –
오류 예외를 throw하는 데 사용 된 코드를 표시 할 수 있습니까? –
시도한 코드에서 편집했습니다. –