从wcf服务处理Silverlight异常(故障)

时间:2010-09-02 07:37:21

标签: c# silverlight wcf custom-exceptions

我想从wcf方法获取exeption代码,但我总是得到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; }
    }

Config(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类型。怎么做?

4 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

叶夫,

此处的问题是您收到错误404.这是在WCF服务之上的级别,由IIS处理和返回,因此您的请求永远不会到达您的WCF服务。您需要检查服务的端点URL以及.svc文件/ IIS上的相同URL,并确保它们是相同的。我实际上会尝试使用浏览器浏览到端点URL,看看我得到了什么。

正如您的链接所解释的那样,您需要让代码能够转换为错误,并且我假设您已经这样做了。

希望这有帮助。

答案 2 :(得分:1)

为我找到了简单的解决方案:

        bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

只需添加此行代码即可,无需配置即可使用。

答案 3 :(得分:1)

在Silverlight应用的Application_Startup事件处理程序中添加以下内容:

    bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
相关问题