Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have developed an application that calls Web APIs on a remote listener service to perform various back end functions (mainly database queries and updates). For debugging purposes, I originally wrote the listener side as a self host console application running on my local machine. I have everything working so the next step was to convert the console application to a windows service and install it on my main server. I converted the console application to a service, installed it on my server and started it up. I also configured the firewall on the server to accept incoming connections on the port that I am using. The service appears to be running as expected. The service is configured to run under the administrator account.

When I run the application, it breaks when I call GetResponseStream. Here is the error that I get:

No connection could be made because the target machine actively refused it

The URL that I am calling looks fine. It is the same one that I am calling against my console application (which works).

When I check the service after receiving this error, it has stopped. When I check the application log, I see that there is a "System.NullReferenceException" error. I have code all over the place to record when the application has received commands. None of this is being executed which makes me think that the error is happening in the parser.

Here is my startup code:
Protected Overrides Sub OnStart(ByVal args() As String)
Dim config As New HttpSelfHostConfiguration("http://localhost:21212")

config.Routes.MapHttpRoute( _
name:="DefaultApi", _
routeTemplate:="api/{controller}/{id}", _
defaults:=New With {.id = RouteParameter.Optional} _
)

Using server As New HttpSelfHostServer(config)
Try
server.OpenAsync()

Catch aggEx As Exception
ProcessError("Error opening server.", "OnStart")

End Try

End Using

End Sub

I am calling the Web API using http://192.168.0.2:21212/api/employees/12.

Here is my EmployeesController code:

Imports System.Web.Http
Imports System.Net
Imports System.Net.Http

Namespace Controllers
Public Class EmployeesController
Inherits ApiController

Public Function GetGetEmployeeInfoUsingID(id As Integer) As Employee
ProcessDebug("GetEmployeeInfoUsingID called: " + CStr(id)) ' this is where I write to a file to indicate that this function has been called
Return GetEmployeeInfoUsingID(id)
End Function

End Class
End Namespace

I am not doing anything differently than the console application so I can't figure out why the service would fail.

Any ideas?

John
Posted
Comments
Mathi Mani 4-Jun-15 15:53pm    
Question is bit unclear. Are you calling the WebAPI service from windows service or hosting it in windows service?
jgehlsen 4-Jun-15 16:28pm    
The Windows Service is the listener. It is responding to Web API calls from a remote application.
Mathi Mani 4-Jun-15 18:12pm    
It seems like it is a problem with your windows service, as you are saying, the same is working from console application. Try to find out why it is stopping and what is causing NullReference exception. You may get an answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900