来自 WCF 服务的 Silverlight 异常(故障)处理

发布于 2024-09-16 19:52:29 字数 2104 浏览 9 评论 0原文

我想从 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 类型。怎么做呢?

I want get exeption code from wcf method but i always get NotFound error.

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):

<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. How to do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

逆流 2024-09-23 19:52:29

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

Evgeny, how have you created your client proxy? Does your client have access to BaseFault type? What kind of error do you get (type not found, page not found, file not found)?

逆夏时光 2024-09-23 19:52:29

Evgeny,

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

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

希望这有帮助。

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. 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. I would actually try browsing to the endpoint URL using a browser and see what I get.

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.

耀眼的星火 2024-09-23 19:52:29

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

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

只需添加这行代码,无需配置即可工作。

Found easy solution for me:

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

just add this line of code and it works without configuration.

无名指的心愿 2024-09-23 19:52:29

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

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

Add the following in the Application_Startup event handler of your Sliverlight app:

    bool registerResult = WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文