How to correctly call a web service in .NET

I came across a problem with calling web services that I had never heard about before. I was creating the client with a Using statement like so:

       Dim xml As XDocument = TestXml 

        Dim actual() As Byte 

        Using proxy As New MyWebService.MyServiceClient 
            actual = proxy.GenerateForm(xml.Root) 
        End Using 

        Assert.IsNotNull(actual) 
This is apparently a no no, as it can mask the real error.  For me I was getting this error:

"System.ServiceModel.ChannelFactory`1[MyLibrary.PNetTServices.PNetTServicesSoap],  cannot be used for communication because it is in the Faulted state."

When in reality when I switched to the recommended client call(http://msdn.microsoft.com/en-us/library/aa355056.aspx):
       Dim xml As XDocument = MyXml 

        Dim actual() As Byte 

        Dim proxy As MyWebService.MyServiceClient = Nothing 
        Try 
            proxy = New MyWebService.MyServiceClient 
            actual = proxy.GenerateForm(xml.Root) 
            proxy.Close() 

        Catch ex As TimeoutException 
            proxy.Abort() 
            Throw ex 'Or do what ever 
        Catch ex As CommunicationException 
            proxy.Abort() 
            Throw ex 
        Catch ex As Exception 
            proxy.Abort() 
            Throw ex 
        End Try  

I got the real error message which was that in the config binding maxBufferSize cannot be a different value from maxReceivedMessageSize.

Comments

Popular posts from this blog

Show/Hide formatting text in MS Word 2010

Microsoft.ApplicationBlocks. ExceptionManagement The event source x does not exist error

ASP.NET 2.0 DropDownList EnableViewState