c# - 来自 wcf 服务的 Silverlight 异常(故障)处理

标签 c# silverlight wcf custom-exceptions

我想从 wcf 方法中获取异常代码,但我总是收到 NotFound 错误。

客户端:

public MainPage()
    {
        InitializeComponent();
        client.TestCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(TestCompleted);
    }

    void TestCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        if(e.Error!=null)
        {
            //HOW to get here my class BaseFault???
        }
    }

服务器端:

[ServiceContract]
public interface IService1
{
    [OperationContract]
    [FaultContract(typeof(BaseFault))]
    void Test(int id);
}

  public void Test(int id)
  {
            try
            {
                if (id == -1)
                    ThrowEx(new BaseFault() { ErrorCode = ProcessErrorsCode.InvalidArgument });
                else
                    throw new NullReferenceException("some server error with null value");
            }
            catch
            {
                ThrowEx(new BaseFault() { ErrorCode = ProcessErrorsCode.InternalServerError });
            }
   }


 public void ThrowEx(BaseFault fault)
 {
    throw new FaultException<BaseFault>(fault);
 }



    [DataContract]
    public class BaseFault
    {
        [DataMember]
        public ProcessErrorsCode ErrorCode { get; set; }
    }

配置(includeExceptionDetailInFaults 设置为 True):

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>

    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="">

                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="True" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    </system.serviceModel>
</configuration>

我需要在客户端获取 BaseFault 类型。如何做到这一点?

最佳答案

Evgeny,您是如何创建客户端代理的?您的客户端是否可以访问 BaseFault 类型?您遇到了什么样的错误(找不到类型、找不到页面、找不到文件)?

关于c# - 来自 wcf 服务的 Silverlight 异常(故障)处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3624688/

相关文章:

silverlight - 使用不受信任的证书制作 WP7 HttpWebRequest POST?

c# - C# 中的 XML 模式 1.1 断言

c# - Silverlight/C# web 应用程序 - 无需打开即可将 PDF 发送到客户的打印机

c# - WP7 + 在继续之前等待操作完成

.net - 就 Web 服务而言,何时使用 SOAP 而不是 REST?

android - 从 Android 应用程序使用 WCF/Rest

c# - 使用自签名证书从 Winforms 客户端对 SSL 保护的 WCF 服务进行身份验证

c# - 如何制作 'always-on-bottom' -window

c# - 使用 C#,LINQ 如何一次又一次地在标记之间选择项目?

c# - 来自 Windows Phone 8 的 Http GET 请求