Ninject 拦截方法未触发之前和之后

问题描述 投票:0回答:0

我正在研究 Mastering Ninject for Dependency Injection 一书中的一个例子,但是之前和之后的拦截没有触发,我不知道为什么。

我所有的代码都在下面,任何帮助将不胜感激。

Program.cs

using Ninject;
using Ninject.Extensions.Interception.Infrastructure.Language;
using System;

namespace MasteringNinject.Interception
{
  internal class Program
  {
    static void Main(string[] args)
    {
        Console.Title = "Mastering Ninject - Interception";

        using (var kernel = new StandardKernel())
        {
            var logger = new Logger();

            kernel.InterceptAround<CustomerService>(
                s => s.GetAllCustomers(),
                invocation => logger.Info("Retrieving all customers..."),
                invocation => logger.Debug("Customers retrieved."));

            var customerService = kernel.Get<CustomerService>();
            var customerList = customerService.GetAllCustomers();
            foreach (var customer in customerList)
            {
                Console.WriteLine(customer.FullName);
            }
        }

        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("Press enter to exit...");
        Console.ReadLine();
    }
  }
}

InterfacesAndClasses.cs

using System;
using System.Collections.Generic;

namespace MasteringNinject.Interception
{
  public class CustomerService 
  {
    public List<Customer> GetAllCustomers()
    {
        var customer1 = new Customer { FullName = "John Cardiel" };
        var customer2 = new Customer { FullName = "Stacy Peralta" };
        var customer3 = new Customer { FullName = "Mike McGill" };
        var customerList = new List<Customer>
        {
            customer1,
            customer2,
            customer3
        };
        return customerList;
    }
  }

  public class Customer
  {
    public string FullName { get; set; }
  }

  public class Logger
  {
    public void Info(string str)
    {
        Console.WriteLine("INFO: " + str);
    }

    public void Debug(string str)
    {
        Console.WriteLine("DEBUG: " + str);
    }
  }
}
ninject ninject-extensions ninject-interception
© www.soinside.com 2019 - 2024. All rights reserved.