Exception in Silverlight calling WCF/Rest

Source: Internet
Author: User

Create a Rest service interface:

[ServiceContract]
Public interface IService1
{
[OperationContract]
String GetData (int value );
} Create a new service implementation class:

Public class Service1: IService1
{
Public string GetData (int value)
{
Int I = 0;
Int j = 5/I;
Return string. Format ("You entered: {0}", value );
}
}

Here, Let Service1 throw "divided by zero exception :"

<System. serviceModel>
<Behaviors>
<ServiceBehaviors>
<Behavior name = "ServiceBehavior">
<ServiceDebug includeExceptionDetailInFaults = "true"/>
<ServiceMetadata httpGetEnabled = "true"/>
</Behavior>
</ServiceBehaviors>
</Behaviors>
<Services>
<Service behaviorConfiguration = "ServiceBehavior" name = "WcfService1.Service1">
</Service>
</Services>
</System. serviceModel>

Note <serviceDebug includeExceptionDetailInFaults = "true"/>

 

Add a service reference on the Silverlight client, named ServiceReference1.

Add a button on the page. The Click Event code of the button is as follows:

Private void Button_Click (object sender, RoutedEventArgs e)
{
Service1Client client = new ServiceReference1.Service1Client ();

Client. GetDataCompleted + = new EventHandler <GetDataCompletedEventArgs> (client_GetDataCompleted );
Client. GetDataAsync (35); // Try GetData
}

Void client_GetDataCompleted (object sender, ServiceReference1.GetDataCompletedEventArgs e)
{
}

The result is as follows:

 

The actual exception is "try to divide by 0", but due to browser restrictions, all exceptions are NotFound.

 

There are two ways to solve this problem on msdn,

The simplest is to use RegisterPrefix in the App. xaml. cs code to use the backup client HTTP stack.

Public App ()
{
Bool bRegisterPrefix = WebRequest. RegisterPrefix (http: // localhost: 9541/, WebRequestCreator. ClientHttp );
// Other Code
}

Run the code again:


This is how SL calls the WCF Service to handle exceptions. So what about calling the Rest service?

 

First, modify the serviceModel under the Web. config node to support Rest.

<System. serviceModel>

<Behaviors>

<EndpointBehaviors>
<Behavior name = "EndpointBehavior">
<WebHttp helpEnabled = "true" defaultOutgoingResponseFormat = "Json"
FaultExceptionEnabled = "true"/>
</Behavior>
</EndpointBehaviors>

<ServiceBehaviors>
<Behavior name = "ServiceBehavior">
<ServiceDebug includeExceptionDetailInFaults = "true"/>
<ServiceMetadata httpGetEnabled = "true"/>
</Behavior>
</ServiceBehaviors>

</Behaviors>

<Services>
<Service behaviorConfiguration = "ServiceBehavior" name = "WcfService1.Service1">
<Endpoint behaviorConfiguration = "EndpointBehavior" binding = "webHttpBinding"
BindingConfiguration = "" name = "Rest" contract = "WcfService1.IService1"/>
</Service>
</Services>

</System. serviceModel>

 

Set faultExceptionEnabled = true for the webHttp node and set includeExceptionDetailInFaults to true for serviceDebug.

OK. The Web. config file of the service has been configured. Next, add the WebGet feature for the GetData method.

Public class Service1: IService1
{
[WebGet ()]
Public string GetData (int value)
{
Int I = 0;
Int j = 5/I;

Return string. Format ("You entered: {0}", value );
}
}

Run:

 

 

The error message is displayed.

 

Note: Do not forget to add the cross-origin and authorization files crossdomain. xml and clientaccesspolicy. xml to the website root directory.

 

Similarly, on the SL client page, add a Button. The Code event of the button is:

Private void btnRest_Click (object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient ();

Wc. DownloadStringCompleted + = new DownloadStringCompletedEventHandler (wc_DownloadStringCompleted );
}

Void wc_DownloadStringCompleted (object sender, DownloadStringCompletedEventArgs e)
{
& N

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.