2014-09-17 3 views
0

저는 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" 
} 
+0

".NET 웹 서비스"가 아닙니다. WCF입니다. 질문에보다 정확한 태그를 달아야합니다 : D –

+0

오류 예외를 throw하는 데 사용 된 코드를 표시 할 수 있습니까? –

+0

시도한 코드에서 편집했습니다. –

답변

0

당신은 계약을 잘못 사용자 지정 오류 처리기를 추가 할 채널 Dispatcher를 사용하여 정의해야합니다.

WCF 3.5에서 오류 처리에 대한 좋은 기사가 있습니다. URL을 게시 할 때 불이익을 당했을 때 필요한 것을 설명하는 것은 먼 길입니다. "WCF 오류 처리 및 오류 변환"코드 프로젝트를 살펴 보시기 바랍니다. Sasha Goldshtein이 쓴 훌륭한 기사. 희망이 도움이됩니다.

+0

링크 게시에 대한 불이익을받지 않습니다. 가능한 접근법/아이디어를 설명하고 링크를 추가하면 참고 문헌이 답안에 제공된 것과 같은 개념적 링크보다 낫습니다. P –

+0

최근 접근 방식을 사용했고 URL을 추가해도 여전히 3 점이 느슨합니다. – meetkichu

+0

이것은 슬프다. * dowvote * whor *가 많이 있지만 내 인상은 SO가 작동하고 영원히 downvoters에 대해 걱정할 필요가 없다는 것입니다. –

관련 문제