Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WCF http服务中返回自定义错误消息_C#_.net_Wcf_Http_Custom Errors - Fatal编程技术网

C# 在WCF http服务中返回自定义错误消息

C# 在WCF http服务中返回自定义错误消息,c#,.net,wcf,http,custom-errors,C#,.net,Wcf,Http,Custom Errors,我刚开始学C,我有点迷恋这个 当用户无法访问特定资源时,我需要返回一条自定义错误消息 服务的接口类: [ServiceContract] public interface IPatientDemographics { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "GetPatientDemographics/{strHospId}/{strView=mobile}", ResponseFo

我刚开始学C,我有点迷恋这个

当用户无法访问特定资源时,我需要返回一条自定义错误消息

服务的接口类:

[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; }
}

您必须使用Channel Dispatcher添加错误处理程序并定义自定义故障协定

WCF3.5中有一篇关于错误处理的好文章。这是很长的一段路来描述你们所需要的,因为我因为发布URL而受到惩罚。我建议您在代码项目中查找 WCF错误处理和故障转换。萨沙·戈尔茨坦写的一篇好文章。
希望这有帮助。

这不是.NET web服务,而是WCF。您需要更精确地标记您的问题:d您能显示用于引发错误异常的代码吗?我已经编辑了我尝试的代码,您不会因为发布链接而受到惩罚。如果你解释了一种可能的方法/想法,并添加了一个链接作为参考,这比你在回答中提供的概念性链接要好:PI最近给出了方法并添加了url,但我还是失去了3分。谢谢。这很可悲。有很多下流的妓女,但我的印象是这样很有效,你不必永远担心下流选民……我同意,但我想获得50分,让评论继续下去。因此,我的积分要求。不管怎样,我没有参与任何游戏。谢谢你的精神这是一个强有力的论点关于评论的事情
[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");
        }
    }
}
{
    error: "User does not have access to patient record"
}