WCF DataContract枚举错误消息返回给用户

时间:2015-07-29 10:33:15

标签: c# wcf enums

我有一个WCF Web服务,我用这个装饰了数据成员枚举数据类型(使用DataAnnotations)..

public enum MaritalStatuses
{
    Married = 1,
    Cohabiting = 2,
    Single = 3,
    Divorced = 4,
    Seperated = 5,
    Widowed = 6
}

来自内部枚举。

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <s:Fault>
         <faultcode xmlns:a="http://schemas.microsoft.com/net/2005/12/windowscommunicationfoundation/dispatcher">a:InternalServiceFault</faultcode>
         <faultstring xml:lang="en-GB">The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the &lt;serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework SDK documentation and inspect the server trace logs.</faultstring>
      </s:Fault>
   </s:Body>
</s:Envelope>

我试图尽可能多地向请求者提供信息,但问题是如果请求包含除上述文本选项之一以外的任何内容(已婚,离婚等),则服务会抛出错误,但不会给出任何迹象表明什么是错的。像这样......

[DataContract]
[ServiceKnownType(typeof(DrivingLicenceTypes))]
[ServiceKnownType(typeof(MaritalStatuses))]
[ServiceKnownType(typeof(ResidentialStatuses))]
[ServiceKnownType(typeof(EmploymentStatuses))]
public class Applicant
{
    [DataMember]
    [EnumDataType(typeof(MaritalStatuses), ErrorMessage = "MaritalStatus - must be a valid value.")]
    [Required]
    public MaritalStatuses MaritalStatus { get; set; }

所有其他数据类型的数据注释按预期工作,并将相应的消息返回给调用者,但是使用枚举,我无法获得良好的消息。它几乎就像DataContract不允许它获得DataAnnotation检查(或DataAnnotation检查错误)。

以下是Enum所在的类的一部分

$password = isset($_SERVER['PHP_AUTH_PW']) ? $_SERVER['PHP_AUTH_PW'] : '';
$user = isset($_SERVER['PHP_AUTH_USER']) ? $_SERVER['PHP_AUTH_USER'] : '';

function authenticate($user, $password) {

    $AuthUserFile = file(".htpasswd");
    $AuthGroupFile = file(".htgroup");
    $group = "Members";

        if(!preg_grep("/$group: $user$/", $AuthGroupFile)) {
            if(!($authUserLine = array_shift(preg_grep("/$user:.*$/", $AuthUserFile)))) {
                preg_match("/$user:((..).*)$/", $authUserLine, $matches);
                $authPW = $matches[1];
                $salt = $matches[2];

                $submittedPW = crypt($password, $salt);
                if($submittedPW != $authPW) { return 0; } else { return 1; };
            } else { return 0; };
        }else { return 0; };

}

0 个答案:

没有答案