469,282 Members | 1,734 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

windows service (c#) through proxy

hi

i am developing a windows application (VS 2008 - .net 3.5) which is requesting some data from a server (XML) and depends by results it is doing something

this application is using a UI for load/ save setings and a windows service which is requesting data at each 20 minutes.

i realized that the form (UI) has no problem to communicate with server (with or without proxy) but windows service is working only when is no proxy.

can somebody help me? can be an example in c# or vb.

i really appreciate it
calin
Oct 27 '08 #1
9 9525
Plater
7,872 Expert 4TB
What do you mean by "no proxy" and with a proxy?
If your browser is using a Proxy, it does not mean your service is using the proxy too.
Or do you mean like the network that the PC with the service is attached to, has a proxy server that applies to every PC on that network, without any settings on the PCs?

If a pxoy server is indeed being the middleman for your requests, the webserver is probably going "hey, this isn't the computer/service i requested the XML data from, it must be bad data/not mine" and either ignores it or throws an error
Oct 28 '08 #2
Hi Plater

all time the service is trying to access DIRECT (doesn't matter if in IE is seted up a proxy)

Expand|Select|Wrap|Line Numbers
  1.  Dim objRequest As HttpWebRequest
  2.  objRequest = WebRequest.Create(URL + PostData)
  3.  Dim retProxy As WebProxy
  4.  Dim retURI As Uri
  5.  retURI = New Uri(URL)
  6.  retProxy = New WebProxy(WebRequest.DefaultWebProxy.GetProxy(retURI))
  7.  objRequest.Credentials = CredentialCache.DefaultCredentials
  8.  objRequest.Timeout = 30000   ' 30 secs
  9.  If retURI <> retProxy.Address Then
  10.           objRequest.Proxy = retProxy
  11.   End If
This code is working when is accessed from a form (working means that form is looking at IE proxy settings).. but id doesnot work when is accessed from windows service (windows service try to access DIRECT).

Thank you
Oct 28 '08 #3
Plater
7,872 Expert 4TB
Oh ok, let me make sure I understand this.
When you run the program as a form, the program gets the PROXY settings (via that default proxy settings) and works fine.
But as a SERVICE, the program is failing to get the proxy credentials?

I would venture that the problem is this: PROXY settings are generally on a "per user" level. When you run the program as a form, it gets the current user's proxy settings, which are configured. When run as a SERVICE, it runs as some other user (possibly some SYSTEM account) that does not have the PROXY server settings configured.
Try running the SERVICE with a specific user credentials that you know has the PROXY settings configured, and see if that helps?
Oct 28 '08 #4
Hi Plater

is all true what u said. the service is running like LocalSystem, but i need to be ran by localsystem. U know in vista is more complicate. for this i implemented Elevated method.

This service need to have ADMIN privileges. Any ideea how can i use default proxy in my windows service started like LocalSystem?

Thank you in advance
Calin
Oct 28 '08 #5
Plater
7,872 Expert 4TB
Hmm, the default proxy is the one configured by internet explorer right?
All those settings are kept in the registry.
Maybe you could find a way to either:
A) copy the registry keys over into the HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER
B) Read the registry entry of another user and get the settings?
C) Find a way to apply the proxy settings to "all users of the computer)

Can't say as I know how to do any of those, but it might help you find something about it?
Oct 28 '08 #6
Hi Plater

i tried this, but u know virtualization in Vista! registry ideea is not good.
at setup process (installation) i am writing default proxy setings in a file. when i am running the service, i am reading the proxy from that file. i thought that this will solve the problem, but appear a new one:
The <my_service_name>service hung on starting.

any idea?
thank u in advance!

calin
Oct 29 '08 #7
Plater
7,872 Expert 4TB
Can you have your service output to its own log file somewhere?
Perhaps its throwing some kind of error that is not caught by eventlog stuff?
Just make an entry into that file like "Entering function: (whatever)", "leaving function (whatever)", etc etc
Then you could find where it is stalling out?
Oct 29 '08 #8
yes i implemented

Expand|Select|Wrap|Line Numbers
  1. Public Class pcksrv
  2.     Protected Overrides Sub OnStart(ByVal args() As String)
  3.         ' Add code here to start your service. This method should set things
  4.         ' in motion so your service can do its work.
  5.         LogInfo("Stariting the service")
  6.         myClock.Enabled = True
  7.  
  8.     End Sub
  9.  
  10.     Protected Overrides Sub OnStop()
  11.         LogInfo("Stoping the service")
  12.         myClock.Enabled = False
  13.     End Sub
  14.  
  15.     Public Shared Sub LogInfo(ByVal sMessage As String)
  16.         ' Write info into the event viewer
  17.         Try
  18.             Dim oEventLog As EventLog = New EventLog("Application")
  19.             If Not Diagnostics.EventLog.SourceExists("NotificationService") Then
  20.                 Diagnostics.EventLog.CreateEventSource("NotificationService", "Application")
  21.             End If
  22.             Diagnostics.EventLog.WriteEntry("NotificationService", sMessage, _
  23.                 System.Diagnostics.EventLogEntryType.Information)
  24.         Catch e As Exception
  25.         End Try
  26.     End Sub
  27.  
  28.     Private Sub myClock_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles myClock.Tick
  29.         LogInfo("Tic Tac 1")
  30.         CheckMyServer()
  31.         LogInfo("Tic Tac 2")
  32.     End Sub
  33. End Class
  34.  
but no luck, the service crashed before!
Oct 29 '08 #9
Curtis Rutland
3,256 Expert 2GB
Please enclose your posted code in [code] tags (See How to Ask a Question).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use [code] tags in future.

MODERATOR
Oct 29 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Carl Waldbieser | last post: by
5 posts views Thread by Bill Hauver | last post: by
2 posts views Thread by Jeffrey Tate via DotNetMonster.com | last post: by
3 posts views Thread by =?Utf-8?B?RGFuZGFuIFpoYW5n?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.