I am trying to inherit a base class into one of my classes so I can use it's protected functions. The base class is built in VB which is a reference in the project and is as follows:
Public MustInherit Class RequestLifetimeCacheBase
Private ReadOnly _requestContext As HttpContextBase
Public Sub New(requestContext As HttpContextBase)
Me._requestContext = requestContext
End Sub
Public Sub New(controller As Controller)
Me.New(controller.HttpContext)
End Sub
Protected Function GetCachedValue(Of TValue As Structure)(cacheKey As String) As TValue?
Dim value = Me._requestContext.Items(cacheKey)
If TypeOf value Is TValue Then
Return DirectCast(value, TValue)
Else
Return Nothing
End If
End Function
Protected Sub SetCachedValue(Of TValue As Structure)(cacheKey As String, value As TValue?)
If value.HasValue Then
Me._requestContext.Items(cacheKey) = value.Value
Else
Me._requestContext.Items.Remove(cacheKey)
End If
End Sub
Protected Function GetCachedObject(Of TObject As Class)(cacheKey As String) As TObject
Return TryCast(Me._requestContext.Items(cacheKey), TObject)
End Function
Protected Sub SetCachedObject(Of TObject As Class)(cacheKey As String, value As TObject)
If value IsNot Nothing Then
Me._requestContext.Items(cacheKey) = value
Else
Me._requestContext.Items.Remove(cacheKey)
End If
End Sub
End Class
The old way in VB is being created like this:
Dim cache = New RequestLifetimeCache(Me)
Dim cachedDatabase = cache.CustomerDatabase
If cachedDatabase IsNot Nothing Then
Return cachedDatabase
End If
I am creating a new instance of it like this in C#:
public WebsiteDatabaseContext customerDB
{
get
{
RequestLifetimeCache cache = new RequestLifetimeCache();
var cachedDB = cache.CustomerDatabase;
return cachedDB;
}
set
{
}
}
My class code is as follows in C#:
public class RequestLifetimeCache : RequestLifetimeCacheBase
{
public RequestLifetimeCache()
{
}
public WebsiteDatabaseContext CustomerDatabase
{
get
{
return base.GetCachedObject<WebsiteDatabaseContext>("Customer database");
}
}
}
But I am getting the error 'RequestLifetimeCacheBase' does not contain a constructor that takes 0 arguments on the following line:
public RequestLifetimeCache()
What am I doing wrong? Am I missing passing an HttpContext/Controller argument or something?
The VB is on the .NET Framework and I am working on .NET Core
I have had to rewrite the VB Base class in C# as a temporary measure:
public abstract class RequestLifetimeCacheBase
{
private readonly HttpContext _requestContext;
protected RequestLifetimeCacheBase(HttpContext requestContext)
{
_requestContext = requestContext;
}
protected RequestLifetimeCacheBase(Controller controller)
: this(controller.HttpContext)
{
}
#region Generic access to cached value-type data
protected TValue? GetCachedValue<TValue>(string cacheKey)
where TValue : struct
{
var value = _requestContext.Items[cacheKey];
if (value is TValue)
{
return (TValue)value;
}
else
{
return null;
}
}
protected void SetCachedValue<TValue>(string cacheKey, TValue? value)
where TValue : struct
{
if (value.HasValue)
{
_requestContext.Items[cacheKey] = value.Value;
}
else
{
_requestContext.Items.Remove(cacheKey);
}
}
#endregion
#region Generic access to cached reference-type data
protected TObject GetCachedObject<TObject>(string cacheKey)
where TObject : class
{
return _requestContext.Items[cacheKey] as TObject;
}
protected void SetCachedObject<TObject>(string cacheKey, TObject value)
where TObject : class
{
if (value != null)
{
_requestContext.Items[cacheKey] = value;
}
else
{
_requestContext.Items.Remove(cacheKey);
}
}
#endregion
}