问题描述:

I'm trying to write a simple IHttpModule that (in the case of this example) records each page a logged-in user is hitting. I'm trying to make it generic across several Web apps so that it can be used with several different types of user objects.

I have an interface ILogUser that has one property:

public interface ILogUser

{

public string LoggingRef { get;}

}

I have several different user objects across various Web apps that inherit from this interface:

public class StaffMember : ILogUser

{

public int Id { get; set; }

public string Name { get; set; }

// many more properties

public string LoggingRef { get { return Name; }}

}

public class ExtranetUser : ILogUser

{

public int Id { get; set; }

public string Email { get; set; }

// many more properties

public string LoggingRef { get { return Email; }}

}

Etc.

My HttpModule is coded as follows:

public class LogVisitModule<T> : IHttpModule where T : ILogUser

{

private readonly ILog log;

public LogVisitModule()

{

log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

}

public void Init(HttpApplication app)

{

app.PostAcquireRequestState += new EventHandler(this.PostAcquireRequestState);

}

private void PostAcquireRequestState(object sender, EventArgs e)

{

HttpApplication app = (HttpApplication)sender;

HttpContext context = app.Context;

if (context.Session != null && context.Session["CurrentUser"] != null)

{

var user = (T)context.Session["CurrentUser"];

log.Info($"User {user.LoggingRef} has accessed {context.Request.FilePath}");

}

}

public void Dispose() { }

}

And I dynamically load the module in my Global.asax with the appropriate assembly attribute and method call:

[assembly: PreApplicationStartMethod(typeof(WebPortal.MvcApplication), "Start")]

public static void Start()

{

HttpApplication.RegisterModule(typeof(LogVisitModule<StaffMember>));

}

When I boot up my Web app I get the Server Error:

Could not load type 'WebPortal.Modules.LogVisitModule'.

Description: An unhandled exception occurred during the execution of

the current web request. Please review the stack trace for more

information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: Could not load type

'WebPortal.Modules.LogVisitModule'.

Source Error:

An unhandled exception was generated during the execution of the

current web request. Information regarding the origin and location of

the exception can be identified using the exception stack trace below.

ANY help would be appreciated! I hope what I'm trying to do makes sense? I don't even know if what I'm doing is possible. I've Googled dynamically loading HttpModules with generic types but haven't found a whole lot.

相关阅读:
Top