Check if SERVICE is running VBNET - vb.net

I want to know if a service is running (VBNET) to return a false or true value
I found other questions with this but the codes didnt work for me..
Thanks you!

Use service controller class to determine whether the service is running or not.
For Each s As ServiceController In ServiceController.GetServices()
If s.ServiceName = "yourservicename" AndAlso s.Status = ServiceControllerStatus.Running Then
Return True
End If
Next
Hope this helps.

Related

Proxy-address-forwarding property from Thorntail to Quarkus

in my previous implementation using Thorntail I have used this property to manage the request behind the Proxy (nginx):
thorntail.undertow.servers.default-servers.http-listeners.default.proxy-address-forwarding = true
Which is the right property to use in Quarkus ?
From the documentation I have found out the following one but in my case it doesn't work:
quarkus.http.proxy.proxy-address-forwarding = true
Thanks
I have updated the Quarkus version from 1.7.3-Final to 1.9.2-Final and added these two properties:
quarkus.http.proxy.proxy-address-forwarding = true
quarkus.http.proxy.enable-forwarded-host = true
It works!

WCF Service Method - Refactoring for Unit Test and Mocking

I've a WCF service with the following method:
Public Function ScheduleEmail(ByVal request As ScheduleEmailRequest) As ScheduleEmailResponse _
Implements EmailProtocol.ISchedulingService.ScheduleEmail
Try
If Not Email.IsValidEmailAddress(request.EmailAddress) Then
EmailSchedulerTrace.Source.WriteError(String.Format("Email with template '{0}' was not sent to '{1}' because it the address is invalid.", request.EmailName, request.EmailAddress))
Else
Dim mgr As New JobManager
Dim job As New EmailJob
Dim suppression As New SuppressionManager
Dim emailItem As Email = Email.GetEmailByName(request.EmailName)
If suppression.CheckSuppresion(emailItem, request.EmailAddress) Then
job.JobGuid = Guid.NewGuid
job.EmailAddress = request.EmailAddress
job.EmailGuid = emailItem.ID
job.ScheduledSendTime = request.ScheduledTime
job.CustomAttributes = request.CustomAttributes
job.ConsumerID = Email.GetConsumerId(request.CustomAttributes)
mgr.ScheduleJob(job)
Else
EmailSchedulerTrace.Source.WriteWarning(String.Format("Email with template '{0}' was not sent to '{1}' because it was suppressed.", request.EmailName, request.EmailAddress))
End If
End If
Catch ex As Exception
EmailSchedulerTrace.Source.WriteError(ex)
Throw
End Try
Return New ScheduleEmailResponse
End Function
I need to write Unit Test for this Method. Please help me out with
Do i need to change anything in my method?
What should I mock?
Your help is greatly appreciated. Thanks in advance.
Regards,
Sachin
You need to be able to swap out any 'services' (classes that you new up in a method or fields in the class) which connect to other systems (database, email server etc) so you need to create interfaces for the classes and inject the correct implementation at runtime and in your unit test, you can create mock or fake implementations for testing purposes.
A good start would be to define an interface for:
JobManager
EmailSchedulerTrace
SuppressionManager
You also might need to move the functionality of your static methods on Email
GetEmailByName
GetConsumerId
if they encapsulate database access or any other service which you cannot isolate.

ASP VB .NET Passing Parameter to Controller Action

I am currently writing a ping status monitoring ASP. But i could not figure out how to pass data from calling a action to controller.
My Action Code as follow:-
Function showPing2(ByVal ipaddress As String) As String
If ipaddress = 1 Then
Return "Online"
Else
Return "Offline"
End If
End Function
Calling method from Index.vbhtml
#Html.Action("showPing2(1)")
I could not pass the value like that, it keep showing the error "HttpException was unhandled by user code"
Could anyone please tell me how to correctly pass value in ASP .NET?
Thank you very much!!
Wrong syntax, try this:
#Html.Action("showPing2", new { ipaddress = "1" })
OR
#Html.Action("showPing2", "ControllerName", new { ipaddress = "1" })
Added
This is C# syntax as I understand from Anonymous Types (Visual Basic) or Anonymous class initialization in VB.Net, VB.NET is something like:
#Html.Action("showPing2", New With { .ipaddress = "1" })

Lua global variable in module staying nil?

Im starting to learn Lua modules a bit, and I am having troubles with a small part in my Lua.
Everytime I change my variable it reverts back to nil.
myModule.lua
--I should note that client is a number.
module(..., package.seeall)
local LoggedIn = { }
function isLogged( client )
return LoggedIn[client]
end
function logIn(client)
table.insert(LoggedIn,client,true)
end
function logOut(client)
table.remove(LoggedIn,client)
end
main.lua an event happens
package.loaded.myModule= nil; require "myModule"
function event( client )
myModule.logIn(client)
end
function event_2( client )
myModule.logOut(client)
end
EDIT: Using functions instead, and making it local variable.
It is still returning nil even though I can confirm the logIn function happened with no errors. Without even using the logout function yet.
Any thoughts?
but later on in main.lua I check if client is logged in and it just returns nil.
Is this just a limitation of modules or am I just accessing the variable wrong.
I should note I need to be able to do this in other Luas that acces myModule.lua too.
Thanks in advance
You don't really give us enough code to fully help you, but this is a working example I set up based on what little example you gave us:
-- myModule.lua
module(..., package.seeall)
LoggedIn = {}
function isLoggedIn(client)
return LoggedIn[client] ~= nil
end
function LogIn(client)
LoggedIn[client] = true
end
function LogOut(client)
LoggedIn[client] = nil
end
and to test it:
-- main.lua
require "myModule"
myModule.LogIn("Joe")
myModule.LogIn("Frank")
print(myModule.isLoggedIn("Bill"))
print(myModule.isLoggedIn("Frank"))
myModule.LogOut("Joe")
print(myModule.isLoggedIn("Joe"))
this prints out as expected:
false
true
false
so my guess is that you are not checking the conditions correctly for LoggedIn[client] being empty, or you never actually remove entries from the LoggedIn table when someone 'logs out'.
The following using your own code (assuming you fix typo in funtion) works (it prints true\nnil):
package.loaded.myModule= nil; require "myModule"
function event( client )
myModule.LoggedIn[client] = true
end
event("foo")
print(myModule.isLogged("foo"))
A better way to do this would be to add a function logIn as #Mike suggested and avoid using module(); you can use something like this instead:
local myModule = require "myModule"
function event( client )
myModule.logIn(client)
end
event("foo")
print(myModule.isLogged("foo"))
print(myModule.isLogged("bar"))
And myModule.lua becomes:
local LoggedIn = { }
function isLogged( client )
return LoggedIn[client]
end
function logIn( client )
LoggedIn[client] = true
end
return { LoggedIn = LoggedIn, isLogged = isLogged, logIn = logIn }

Context issue in IHttpHandler

Sorry, this can be a basic question for advanced VB.NET programmers but I am a beginner in VB.NET so I need your advice.
I have a web application and the login is required for some specific pages. To check if the user is logged in, the old programmer used this technique:
Dim sv As New WL.SessionVariables(Me.Context)
If Not (sv.IsLoggedIn) Then
Response.Redirect(WL.SiteMap.GetLoginURL())
End If
Well, I have to use this Logged In checking in a handler done by me and I tried this:
Public Class CustomHandler
Implements System.Web.IHttpHandler, IReadOnlySessionState
Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim sv As New WL.SessionVariables(context)
If Not (sv.IsLoggedIn) Then
context.Response.Write("No access unless you're the CEO!!!" & sv.IsLoggedIn)
ElseIf sv.IsLoggedIn Then
DownloadFile(context)
Else
End If
End Sub
//other code
End Class
Well, the "is logged in" checking is always false (even after I login) and I think it's an issue with the context. So all the other pages works fine with logging checking but this handler have this specific issue.
Can you guys give a helping hand?
UPDATE:
The logged in is done trough this method:
Public Sub SetCreditialCookie(ByVal accountID As Integer)
Me.AccountID = accountID
m_context.Session.Item("loggedInAccount") = accountID
m_context.Response.Cookies.Add(New System.Web.HttpCookie("account_id", CStr(m_context.Session.Item("account_id"))))
m_context.Response.Cookies("account_id").Expires = DateTime.Now.AddDays(5)
End Sub
and to check it it's logged in, this method is called:
Public Function IsLoggedIn() As Boolean
If Not m_context.Session.Item("loggedInAccount") Is Nothing And Me.AccountID = m_context.Session.Item("loggedInAccount") Then
Return True
Else
Return False
End If
End Function
UPDATE 2:
- debugging the code shown that there were multiple kind of logins and I was checking the wrong one with the session.
Due to the use of IReadOnlySessionState, is it possible that the SessionVariables class attempts in some way to modify the Session, which in turn causes an error (possibly handled and not visible to you).
If this is the case it could mean that the IsLoggedIn property is not correctly initialised, or does not function as expected?
Do you have access to the code for the class. If so, try debugging it to see what is happening.