简体   繁体   English

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

[英]Silverlight exception (faults) handling from wcf service

I want get exeption code from wcf method but i always get NotFound error. 我想从wcf方法获取exeption代码,但我总是得到NotFound错误。

Client Side: 客户端:

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???
        }
    }

Server side: 服务器端:

[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 set to True): 配置(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>

I need to get BaseFault type on my client side. 我需要在客户端获得BaseFault类型。 How to do that? 怎么做?

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

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

Evgeny, how have you created your client proxy? Evgeny,您是如何创建客户端代理的? Does your client have access to BaseFault type? 您的客户端是否可以访问BaseFault类型? What kind of error do you get (type not found, page not found, file not found)? 你得到什么样的错误(未找到类型,找不到页面,找不到文件)?

Evgeny, 叶夫根尼,

The problem here is that you are getting error 404. This is at the level above WCF service and is handled and returned by the IIS so your request never hits your WCF service. 这里的问题是你得到错误404.这是在WCF服务之上的级别,由IIS处理和返回,因此您的请求永远不会到达您的WCF服务。 You need to check the endpoint URL of your service and the same on your .svc file/IIS and make sure they are the same. 您需要检查服务的端点URL以及.svc文件/ IIS上的相同URL,并确保它们是相同的。 I would actually try browsing to the endpoint URL using a browser and see what I get. 我实际上会尝试使用浏览器浏览到端点URL,看看我得到了什么。

As your link explains, you need to have the code to be able to cast to fault and I assume you are already doing that. 正如您的链接所解释的那样,您需要让代码能够转换为错误,并且我假设您已经这样做了。

Hope this helps. 希望这可以帮助。

Found easy solution for me: 为我找到简单的解决方案:

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

just add this line of code and it works without configuration. 只需添加此行代码即可,无需配置即可运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM