Monday, August 17, 2009

Implementing WCF Service Part 6 – Using Fault @ Client

Ok, so you have developed a nice WCF Service with proper implementation of custom fault exception handling, now you want to call this service in a web application or other client application and catch the fault exception in your client code. For simplicity I am using a web page, the following code is self explanatory.

  1: Protected Sub btnGetCustomers_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGetCustomers.Click  
  2:   
  3:         Using wcfSrv As CustomerWcfService.CustomerServiceClient = New CustomerWcfService.CustomerServiceClient("WSHttpBinding_ICustomerService")  
  4:             Try  
  5:                 Dim custArr() As CustomerWcfService.Customer  
  6:                 custArr = wcfSrv.GetCustomers()  
  7:                 For Each cust As CustomerWcfService.Customer In custArr  
  8:                     Response.Write("ID: " & cust.ID.ToString & " Name: " & cust.Name & "<br/>")  
  9:                 Next  
 10:                 lblStatus.Text = "No of Customer Records found: " & custArr.Length.ToString  
 11:   
 12:                 'Custom Fault Exception Response from Service  
 13:             Catch ex As ServiceModel.FaultException(Of CustomerWcfService.ErrorResponse)  
 14:                 lblStatus.Text = "Code:" & ex.Detail.Code.ToString() & " Messgage:" & ex.Detail.Message & " Details:" & ex.Detail.Details  
 15:                 wcfSrv.Abort()  
 16:   
 17:                 'General Fault Exception  
 18:             Catch ex As ServiceModel.FaultException  
 19:                 lblStatus.Text = ex.ToString()  
 20:                 wcfSrv.Abort()   
 21:   
 22:                 'General Exception  
 23:             Catch exp As System.Exception  
 24:                 lblStatus.Text = "Error:" & exp.ToString()  
 25:                 wcfSrv.Abort()  
 26:   
 27:             Finally  
 28:                 wcfSrv.Close()  
 29:             End Try  
 30:   
 31:         End Using  
 32:   
 33:     End Sub  
 34: 

Sunday, August 09, 2009

Convert Array to Generic List

You can happily use .NET Generics in WCF Services. But when you will get the proxy of the respective service, these generics will get converted into Array, even you are using .NET to develop the client application. So now the question is how I should get back from Array to Generic List again.

VB.NET

Simple Example

  Dim someArray() As Integer = {1, 2, 3, 4, 5}

        Dim genericList As New List(Of Integer)

        genericList.AddRange(someArray)

 

Web Service Example

  Dim resp As New MyLookupService.GetCountriesResponse

        Dim reqs As New MyLookupService.GetCountriesRequest

        Dim listData As New List(Of MyLookupService.CountryData)

        Dim arrData As Array

 

        reqs.LoginInfo = loginReq

        resp = srvObj.GetCountries(reqs)

        arrData = resp.CountriesInfo

        listData.AddRange(arrData)

 

        Me.gvwCountry.DataSource = listData

        Me.gvwCountry.DataBind()

 

Alternate Option

        When you Add Service Reference, click on "Advanced..." button on bottom left and Choose Collection Type from Dropdown as "System.Collections.Generic.List" and you are all set. No need to use array.

 

Monday, August 03, 2009

WCF Service Error: The maximum message size quota ...

If you are using a WCF (Web) Service in client application and dealing with large data, at some point you may come across an error message:

{"The maximum message size quota for incoming messages (65536) has been exceeded. To increase the quota, use the MaxReceivedMessageSize property on the appropriate binding element."}

 

Solutions:

Open your web.config or App.Config file at client and change the values in your bindings. Change the value of following configurations from 65536 (default) to 2147483647. That means it will allow up to 2GB.

<wsHttpBinding>  
    <binding name="WSHttpBinding_IMyService" closeTimeout="00:01:00"  
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"  
     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"  
     maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text"  
     textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">  
     <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"  
      maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />  
     <reliableSession ordered="true" inactivityTimeout="00:10:00"  
      enabled="false" />  
     <security mode="Message">  
      <transport clientCredentialType="Windows" proxyCredentialType="None"  
       realm="" />  
      <message clientCredentialType="Windows" negotiateServiceCredential="true"  
       algorithmSuite="Default" establishSecurityContext="true" />  
     </security>  
    </binding>  
   </wsHttpBinding>