Webservices: The request failed with HTTP status 400: Bad Request#

We have a webservice that is called from an application under heavy use.

From time to time we are getting errors coming back from the webservice:

The request failed with HTTP status 400: Bad Request

The stack trace is not very helpful:

at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
   at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)

So basically the webservice is just throwing back a 400 error to the webservice request.

I haven't seen anyone online who has this type of issue.  I have seen several where they ALWAYS get this error, but none where it works most of the time and fails sometimes.

I am considering that it could be something in the network, like a proxy server or AV sniffer, but not sure.

 

Categories:  |  |  |  | 
Tuesday, November 18, 2008 5:21:48 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Compressing Web Service Calls#

Most webservers will compress data if the clients support it.

Unfortunately for .net developers the auto generated proxy classes that access webservices don't include this functionality.

I'll show you how to achieve this while avoiding changing any code in the auto generated proxy classes.

(These classes utilize ICSharpCode.SharpZipLib.dll so you'll have to download this)

This following class will deal with decompressing a response.

Imports System.Net
Imports ICSharpCode.SharpZipLib

Public Class UncompressedGzipResponse
    Inherits System.Net.WebResponse

    Private cCompressedGzipResponse As HttpWebResponse

    Public Sub New(ByVal compressedGzipResponse As HttpWebResponse)
        MyBase.New()
        cCompressedGzipResponse = compressedGzipResponse
    End Sub


    Public Overrides Function GetResponseStream() As System.IO.Stream

        Dim compressedStream As System.IO.Stream = New GZip.GZipInputStream(cCompressedGzipResponse.GetResponseStream)
        Dim decompressedStream As New System.IO.MemoryStream
        Dim size As Integer = 2048
        Dim writeData(2048) As Byte
        While True
            size = compressedStream.Read(writeData, 0, size)
            If size > 0 Then
                decompressedStream.Write(writeData, 0, size)
                Dim s As String = System.Text.Encoding.ASCII.GetString(writeData)
                s = s & ""
            Else
                Exit While
            End If
        End While
        decompressedStream.Seek(0, IO.SeekOrigin.Begin)

        Return CType(decompressedStream, IO.Stream)

    End Function

    Public Overrides Property ContentType() As String
        Get
            Return cCompressedGzipResponse.ContentType
        End Get
        Set(ByVal value As String)
            cCompressedGzipResponse.ContentType = value
        End Set
    End Property

    Public Overrides Property ContentLength() As Long
        Get
            Return cCompressedGzipResponse.ContentLength
        End Get
        Set(ByVal value As Long)
            cCompressedGzipResponse.ContentLength = value
        End Set
    End Property

    Public Overrides ReadOnly Property Headers() As System.Net.WebHeaderCollection
        Get
            Return cCompressedGzipResponse.Headers
        End Get
    End Property

End Class

This next class will handle most of the logic so that your proxy class can be as small as possible:

Public Class WebServiceCompressionHelper
    Public Shared Function GetWebRequest(ByVal uri As System.Uri, ByVal output As WebRequest, ByVal compress As Boolean) As WebRequest

        '*** add gzip
        If compress Then
            output.Headers.Item(Net.HttpRequestHeader.AcceptEncoding) = "gzip"
        End If

        Return output
    End Function

    Public Shared Function GetWebResponse(ByVal output As WebResponse, ByVal request As System.Net.WebRequest) As WebResponse

        Dim compressedResponse As HttpWebResponse = Nothing
        If TypeOf output Is HttpWebResponse Then
            compressedResponse = CType(output, HttpWebResponse)
        End If

        If compressedResponse IsNot Nothing AndAlso compressedResponse.ContentEncoding = "gzip" Then
            output = New UncompressedGzipResponse(compressedResponse)
        End If
        Return output
    End Function
End Class

In order to make use of this we need to tap into a few methods in the auto generated proxy.  The easiest way to do this is to extend the proxy class into your own subclass, like so:

Public Class MyService
    Inherits generated.proxy.class.here
    Protected Overrides Function GetWebRequest(ByVal uri As System.Uri) As System.Net.WebRequest
        Return Walshgroup.Webservices.Common.WebServiceCompressionHelper.GetWebRequest(uri, MyBase.GetWebRequest(uri), True)
    End Function
    Protected Overrides Function GetWebResponse(ByVal request As System.Net.WebRequest) As System.Net.WebResponse
        Dim output As System.Net.WebResponse = MyBase.GetWebResponse(request)
        Return Walshgroup.Webservices.Common.WebServiceCompressionHelper.GetWebResponse(output, request)
    End Function
End Class

Now, instead of instantiating your generated proxy class, you will simply instantiate your subclass.

That's it!

 

Categories:  |  | 
Monday, November 17, 2008 5:30:44 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

 

Microsoft P&P Software Factories#

I didn't notice that the Microsoft P&P team has released something they call "Software Factories" which are supposed to guide the developer in building different apps using best practices (at least that is what I think they do from the descriptions).

Specifically I am interested in the Web Client Software Factory:

... provides an integrated set of guidance that assists architects and developers in creating composite Web applications and page flow client applications.

These applications have one or more of the following characteristics:

  • They have complex page flows and workflows.
  • They are developed by multiple collaborating development teams.
  • They are composite applications that present information from multiple sources through an integrated user interface.
  • They support XCopy deployment of independently developed modules.
  • They support online business transaction processing Web sites.

 

and the Web Service Software Factory, which as they put it is:

... an integrated collection of tools, patterns, source code and prescriptive guidance. It is designed to help you quickly and consistently construct Web services that adhere to well known architecture and design patterns.

The package covers:

  • Designing ASMX and WCF messages and service interfaces.
  • Applying exception shielding and exception handling.
  • Designing business entities in the domain model.
  • Translating messages to and from business entities.
  • Designing, building, and invoking the data access layer.
  • Validating the conformance of service implementation, configuration, and security using code analysis.
  • Planning for the migration to WCF.
  • Applying security to WCF services.
  • Applying message validation.
  • Categories:  |  |  | 
    Tuesday, June 05, 2007 10:59:00 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    Webservices Compression#
    Categories:  |  |  |  | 
    Wednesday, May 02, 2007 2:34:54 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    Performance of Web Services / Remoting / Enterprise Services#

    I had previously read and heard in presentations by Rocky Lhotka that performance comparisons between Web Services and Remoting showed that they were basically the same, and that when talking about RPC protocols ES killed them both so badly (order of magnitude) that it wasn't worth fretting over the small diff between WS and remoting.

    Rocky kinda explains this position in this post:
    http://www.lhotka.net/weblog/RemotingVsWebServicesVsESCOMDCOM.aspx

    However, I actually tracked down the white paper on Microsofts website:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwebsrv/html/asmxremotesperf.asp

    Much to my suprise the giant performance improvement when using ES was only seen when calling empty functions.  In otherwords, it was basically a test of the transport.  Some "real world" tests showed ES performing faster than most other methods, but sometimes something like Remoting TCP Binary would outperform it as well.

    In the tests that actually did something, the performance across the board was usually fairly comperable.  I guess this makes sense.  If you liken the round trip of an RPC call to a person going on a business trip, the speed of the airplane is less important that the time it takes the person to do the job at the end of the trip.  So even if your plane takes 3 hours instead of 2 hours, if your going to be staying for a week then that 1 hour isn't a big deal.

    The major conclusion is to not pass datasets.  Datasets are serialized as XML even if you are using the binary serializer.  I have posted some stuff on this topic as well on my blog so you can search for it if you want. 

    Categories:  |  |  |  | 
    Thursday, September 14, 2006 8:12:30 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    Sending Datasets and Objects Over the Wire: Serialization and XML#

    I have tried to councel against sending datasets across web service calls, but we have a lot of instances where this is being done.

    One of the problems with this is that datasets get bloated when converted to XML.

    So I set out to compare the sizes of:

    1. Serialized List(Of MyType)
    2. Serialized DataTable
    3. Serialized DataSet
    4. XML Serialized Dataset

    I wish I had done some research on this, because I would have quickly been reminded that DataSets always serialize as XML, even if you are using the BinaryFormatter. 

    There are lots of people out there coming up with their own ideas for how to improve the serialization of datasets:

    Anyway, this isn't really THAT big of a deal, because my real goal wasn't to improve the dataset serialization, but to simply see what it would be, and compare it to some other ways to serialize data, like in a list of business objects or a datatable.

    The results are interesting:

    Given a list of 1013 Business Objects (Records) the serialization results are as follows:

    Method Size (bytes)
    List(Of MyType) 290,321
    DataTable 819,575
    DataSet 693,088
    XML Serialzied Dataset 851,614

    I have read that you can really decrease the size of the dataset by writing your own logic to do the serialization, but as everyone points out that is kind of a pain.


    Categories:  |  |  |  |  | 
    Thursday, April 20, 2006 12:44:19 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    Death of Dime (and WS-Attachments)#
    DIME and WS-Attachments are basically dead.

    MTOM (SOAP Message Transmission Optimization Mechanism) has shown up on MSDN (xml messaging page), and DIME, SwA, and PASwA are marked as superseded.

    I have seen some places describing MTOM as basically the same thing as XOP (XML-binary Optimized Packaging), not sure if that is true or whatever, but it seems the message is clear: DIME is yesterdays news.
    Categories:
    Wednesday, April 05, 2006 10:08:14 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    Web Services over SSL#
    This is a KB article about securing web service calls over SSL.

    I wouldn't expect this to be very hard, and because I havn't read it yet, this might be the case. :)
    Categories:  |  |  | 
    Monday, January 26, 2004 10:45:27 AM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    Flash MX and Web Services#
    I really need to read this article to learn more about how flash MX 2004 does this...

    I was really not impressed (disgusted) with how Flash MX does this kind of thing... or rather DOESN'T do this kind of thing.
    Categories:  |  | 
    Sunday, January 25, 2004 8:15:14 PM (Central Standard Time, UTC-06:00) #    Comments [0]  | 

     

    All content © 2009, Christopher May, Inc
    Open Job Positions
    On this page
    Google Ads
    This site
    Calendar
    <January 2009>
    SunMonTueWedThuFriSat
    28293031123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567
    Archives
    Sitemap