WCF 中异常发送的编程配置

发布于 2024-08-05 19:09:12 字数 1535 浏览 2 评论 0原文

我希望我的 Silverlight 客户端能够显示 WCF 调用期间服务器上发生的异常。

给定我当前创建 WCF 通道的代码(在客户端上):

// create the binding elements
BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };

// add the binding elements into a Custom Binding
CustomBinding customBinding = new CustomBinding(binaryMessageEncoding, httpTransport);

// create the Endpoint URL 
EndpointAddress endpointAddress = new EndpointAddress(serviceUrl);

            // create an interface for the WCF service
ChannelFactory<TWcfApiEndPoint> channelFactory=new ChannelFactory<TWcfApiEndPoint>(customBinding, endpointAddress);
channelFactory.Faulted += new EventHandler(channelFactory_Faulted);         
TWcfApiEndPoint client = channelFactory.CreateChannel();

return client;

当发生异常时,我只是得到一个“NotFound”异常,这显然没有用。如何获取异常信息?

我使用此代码来使用上面返回的客户端对象:

try
{
// customFieldsBroker is the client returned above
        customFieldsBroker.BeginCreateCustomField(DataTypeID, newCustomField, (result) =>
        {
            var response = ((ICustomFieldsBroker)result.AsyncState).EndCreateCustomField(result);

    }, customFieldsBroker);
}
catch (Exception ex)
{
    // would like to handle exception here
}

将 Begin/End 调用包装在 try { } catch { } 块中似乎甚至没有跳转到 catch { } 块。

如果重要的话,我在客户端使用 Silverlight 3。

I would like my Silverlight client to be able to display exceptions that have happened at the server during a WCF call.

Given my current code to create a WCF Channel (on the client):

// create the binding elements
BinaryMessageEncodingBindingElement binaryMessageEncoding = new BinaryMessageEncodingBindingElement();
HttpTransportBindingElement httpTransport = new HttpTransportBindingElement() { MaxBufferSize = int.MaxValue, MaxReceivedMessageSize = int.MaxValue };

// add the binding elements into a Custom Binding
CustomBinding customBinding = new CustomBinding(binaryMessageEncoding, httpTransport);

// create the Endpoint URL 
EndpointAddress endpointAddress = new EndpointAddress(serviceUrl);

            // create an interface for the WCF service
ChannelFactory<TWcfApiEndPoint> channelFactory=new ChannelFactory<TWcfApiEndPoint>(customBinding, endpointAddress);
channelFactory.Faulted += new EventHandler(channelFactory_Faulted);         
TWcfApiEndPoint client = channelFactory.CreateChannel();

return client;

When an exception occurs, I just get a "NotFound" exception, which is obviously of no use. How can I get the exception information?

I use this code to use the client object returned above:

try
{
// customFieldsBroker is the client returned above
        customFieldsBroker.BeginCreateCustomField(DataTypeID, newCustomField, (result) =>
        {
            var response = ((ICustomFieldsBroker)result.AsyncState).EndCreateCustomField(result);

    }, customFieldsBroker);
}
catch (Exception ex)
{
    // would like to handle exception here
}

Wrapping the Begin/End calls in a try { } catch { } block doesn't seem to even jump into the catch { } block.

If it matters, I'm using Silverlight 3 at the client.

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

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

发布评论

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

评论(2

辞别 2024-08-12 19:09:12

由于浏览器沙箱的安全限制,silverlight 无法看到服务器错误的正文(状态代码 500)。要使其正常工作,您需要对服务器端进行更改,以更改其向浏览器返回错误的方式。有一篇 MSDN 文章 详细描述了它。

Due to security limitations in the browser sandbox, silverlight can't see the body of server errors (status code 500). To get this working you need to make a change to the server side, to change the way it returns faults to the browser. There's an MSDN article that describes it in detail.

浮云落日 2024-08-12 19:09:12

您需要做两件事:

  • 将错误异常声明为合约的一部分
  • 将异常作为错误异常抛出

    [运营合同]

    [FaultContract(typeof(ArithmeticFault))]

    公共 int 计算(操作 op,int a,int b)

    {
    // ...
    }

    抛出新的FaultException();

You need to do two things:

  • declare the Fault exception as part of the contract
  • throw the exception as a fault exception

    [OperationContract]

    [FaultContract(typeof(ArithmeticFault))]

    public int Calculate(Operation op, int a, int b)

    {
    // ...
    }

    throw new FaultException();

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