客户端的WCF数据服务异常处理

时间:2013-04-19 10:51:55

标签: c# .net wcf exception-handling wcf-data-services

我目前正尝试在wpf客户端上进行干净的wcf数据服务异常处理。我有一个简单的数据服务,包括ef4模型和sql数据库以及一个wpf4客户端。

我已经阅读了一些关于wcf数据服务异常处理的文章,并提出了一个解决方案,我解析收到的异常消息,这是一个格式良好的xml。

文章:ADO.NET Data Services : Efficient Error Handling across Application Tiers

问题是,我的DataServiceQueryException消息包含浏览器中错误页面的HTML而不是XML。因此,解析器无法解析XDocument并只返回原始异常

在服务器端我抛出

throw new DataServiceException(
    (int)HttpStatusCode.Unauthorized, 
    ExceptionMessages.ErrorAuthCommon);

哪里出错?

提前致谢

修改

以下是一些代码段

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyDataService : DataService<MyEntities>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
         config.UseVerboseErrors = true;

异常会在CustomAuthProvider.Authenticate(application.Context)

中抛出
public class BasicAuthenticationModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.AuthenticateRequest
           += new EventHandler(context_AuthenticateRequest);
    }
    void context_AuthenticateRequest(object sender, EventArgs e)
    {
        HttpApplication application = (HttpApplication)sender;
        if (!CustomAuthProvider.Authenticate(application.Context))
        {
            application.Context.Response.Status = "401 Unauthorized";
            application.Context.Response.StatusCode = 401;
            application.Context.Response.AddHeader("WWW-Authenticate", "Basic");
            application.CompleteRequest();
        }
    }
    public void Dispose() { }
}

在这里,我注册了我的IHttpModule

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <add name="CustomAuthenticationModule" type="DataService.BasicAuthenticationModule">
    </modules>
</system.webServer

0 个答案:

没有答案
相关问题