【问题标题】:Can Silverlight WCF client read exceptions from an ASMX web service?Silverlight WCF 客户端可以从 ASMX Web 服务读取异常吗?
【发布时间】:2009-12-15 22:33:05
【问题描述】:

我认为没有必要将我的服务升级到 WCF,但我一直在使用 WCF 客户端从 .NET 3.5 ASP.NET 访问 ASMX 服务。我认为最终我会在这种不匹配中碰壁,而我只是这样做了 - 但使用的是 Silverlight。

当使用 Silverlight 访问 ASMX Web 服务时,我在弹出窗口中收到如下错误:

期间发生异常 操作,使结果无效。 检查 InnerException 是否有异常 详情。

如果我正在调试,我会收到此错误:

 The remote server returned an error: NotFound.

如果我在 Fiddler 中查看异常/故障就可以了:

 <?xml version="1.0" encoding="utf-8"?>
 <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 <soap:Body><soap:Fault>
 <faultcode>soap:Server</faultcode>
 <faultstring>Server was unable to process request. ---&gt; ID does not match</faultstring>
 <detail /></soap:Fault></soap:Body></soap:Envelope>

如何在 Silverlight 客户端中实际处理此异常。

我需要在运行时使用 no fiddlerno debugger 访问错误。

在 web.config 中有一个属性 includeexceptiondetailinfaults 属于 &lt;behaviors&gt; - 但据我所知,这仅适用于服务器端。

我是否正确假设我需要将我的 asmx 转换为 svc 才能在 silverlight 客户端中获得实际的异常详细信息?

【问题讨论】:

    标签: wcf silverlight asmx


    【解决方案1】:

    如果您愿意将 asmx SOAP 请求包装在您自己的 IHttpHandler 中,您可以在 System.Web.Script.Services.ScriptHandlerFactory 工作后强制输入 Response.StatusCode = 200。这是一个示例;

    static void ProcessService(HttpContext context)
    {
        //
        // I'm also using this to fake/hide the path of my asmx so that 
        // domain.com/xml becomes the service end-point..
        //
        string asmx = "/Services/Some.Service.asmx";
        string method = context.Request.Path.Substring("/xml".Length);
    
        //
        // ScriptHandlerFactory and friends are sealed so have to use reflection..
        //
        IHttpHandlerFactory fact = (IHttpHandlerFactory)Activator.CreateInstance(Type.GetType("System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions"));
        Type vpt = Type.GetType("System.Web.VirtualPath, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a");
        System.Reflection.MethodInfo mi = vpt.GetMethod("Create", new Type[] { typeof(string) });
        object vp = mi.Invoke(null, new object[] { context.Request.Path });
        System.Reflection.FieldInfo fi = context.Request.GetType().GetField("_pathInfo", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        System.Reflection.FieldInfo _virtualPath = vpt.GetField("_virtualPath", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
        _virtualPath.SetValue(vp, method);
        fi.SetValue(context.Request, vp);
        IHttpHandler handler = fact.GetHandler(context, context.Request.RequestType, asmx, context.Server.MapPath(asmx));
    
        try
        {
            // This will trap your asmx Exception and output 500 status and soap fault
            handler.ProcessRequest(context); 
    
            // force 200 status for Silverlight to receive fault code
            context.Response.StatusCode = 200;
    
            context.ApplicationInstance.CompleteRequest();
        }
        finally
        {
            fact.ReleaseHandler(handler);
        }
    }
    

    【讨论】:

    • 我认为更容易切换到真正的 WCF!感谢处理程序
    【解决方案2】:

    没有客户端从 Web 服务中获得异常。 Web 服务不发送异常 - 它们发送错误。

    故障的详细信息包含在故障消息的&lt;detail/&gt; 元素中。某些平台(包括 WCF)会解析此信息,以便将故障转换为特定于平台的异常。

    由于&lt;detail/&gt; 元素中没有信息,因此可能不会发生翻译。

    【讨论】:

    • 我确实说过异常/故障 :-) 所以我必须转换为 .svc 才能获取故障详细信息?
    猜你喜欢
    • 2010-10-31
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多