Skip to main content
 首页 » 编程设计

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

2024年02月24日26zlslch

我想从 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 类型?您遇到了什么样的错误(找不到类型、找不到页面、找不到文件)?