WCF rest client: unable to send HTTP message when triggered by another WCF service in the solution

StackOverflow https://stackoverflow.com/questions/23427253

  •  14-07-2023
  •  | 
  •  

Question

I'm building a solution meant to consume a REST service via a WCF proxy client, retrieve XML data and insert it into SQL tables.

My solution has 4 projects:

  • one console app for hosting everything
  • one WCF client class for connecting to the REST service and deserializing XML data into objects
  • one class for parsing deserialized objects and writing to SQL tables.
  • one WCF service class, hosted in the console, that implements one operation which raises en event.

What works fine:

  • instantiating my WCF client class and using it from the console directly,
  • instantiating my WCF client class and using it when manually triggering my WCF service event

What does not work:

  • instantiating my WCF client class and using it when my WCF service raises its event via HTTP method call => Error raised: Unexpected end of file

I looked at verbose trace logs, and it says that sending the HTTP message failed.

Any clue of where this can come from?


This is my WCF service implementation:

<ServiceBehavior(InstanceContextMode:=ServiceModel.InstanceContextMode.Single _
                , IncludeExceptionDetailInFaults:=True)> _
Public Class RemoteService
    Implements IRemoteService

    Public Event getGroups As MyHostEventHandler

    Public Sub DoWork() Implements IRemoteService.DoWork
        RaiseEvent getGroups()
    End Sub

End Class

Public Delegate Sub MyHostEventHandler()

Here is now the code I have for my console application:

Sub getGroups()
    ' Instantiate WCF client        
    Dim proxy As EE2014_DataSolution.EERestAPI = New EE2014_DataSolution.EERestAPI()

    ' Call WCF REST method getGroups2014()
    Dim response_groups As groups = proxy.getGroups2014()

    ' Instantiate SQL writer class
    Dim sql As SqlDataWriter.SqlDataWriter = New SqlDataWriter.SqlDataWriter()

    ' Pass deserialized object to SQL writter class
    Dim numRowsWritten As Integer = sql.WriteGroups(response_groups)

    Console.WriteLine(numRowsWritten & " rows updated")
End Sub

Sub Main()
    ' Instantiate WCF service
    Dim host As ServiceHost = New ServiceHost(New RemoteService.RemoteService())

    ' Handle event (THIS WILL FAIL)
    AddHandler CType(host.SingletonInstance, RemoteService.RemoteService).getGroups, AddressOf getGroups

    ' Start WCF service
    host.Open()
    Console.WriteLine("RemoteService started at " & Now)

    ' Wait for WCF messages
    Console.WriteLine("Press any key to send HTTP request manually.")
    Console.ReadLine()

    ' Get groups directly from console (THIS WILL WORK)
    getGroups()

    ' Wait for WCF messages
    Console.WriteLine("Press any key to send HTTP request manually via event.")
    Console.ReadLine()

    ' Raise the WCF service event manually (THIS WILL WORK)
    CType(host.SingletonInstance, RemoteService.RemoteService).DoWork()

    ' Exit
    Console.WriteLine("Press any key to exit.")
    Console.ReadLine()
End Sub
Was it helpful?

Solution

I found the origin of my problem: the context of the "client" WCF method is overridden by the context of my WCF service. Indeed, thanks to diagnostics configuration, I noticed that my application tried to send POST messages instead of GET messages, which led me to the following link, where lies the solution:

http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/03a2b109-c400-49d4-891e-03871ae0d083/#416d8cbc-b855-46aa-8a6d-5d6b09db97b6

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top