Since you can't control precisely when a
Shared
field initializer runs, I'd be inclined to avoid using
HttpContext.Current
from one. If the class is initialized outside of a request context, you could end up with a
NullReferenceException
, which would then render your entire class unusable for the lifetime of your application.
Instead, map the path inside the method, and use an app-relative path as Solution 1 suggested:
Public Shared Function GetClientConfiguration() As GoogleClientSecrets
Dim context As System.Web.HttpContext = System.Web.HttpContext.Current
If context Is Nothing Then Throw New InvalidOperationException("Cannot call this method outside of a request.")
Dim filePath As String = context.Server.MapPath("~/App_Data/client_secret.json")
Using stream = File.OpenRead(filePath)
Return GoogleClientSecrets.Load(stream)
End Using
End Function