1

我有以下代码

[Quartz.DisallowConcurrentExecutionAttribute()]
public class SearchIndexJob : IJob
{
    private readonly ILog _Log = null;
    private SearchManager _SearchManager;

    public SearchIndexJob()
    {
        _Log = LogManager.GetLogger(GetType());
    }

    #region IJob Members

    public void Execute(IJobExecutionContext context)
    {
        var container = new StructureMap.Container();

        IServiceConfigurationProvider services = new StructureMapConfiguration(container); 

        var locator = new EPiServer.ServiceLocation.StructureMapServiceLocator(container);
        var context2 = new EPiServer.ServiceLocation.ServiceConfigurationContext(HostType.WebApplication, services);

        new Mediachase.Commerce.Initialization.CommerceInitialization().ConfigureContainer(context2);

        container.Configure(ce =>
        {
            ce.For<IMarketService>().Use<MarketServiceDatabase>();
            ce.For<IMarket>().Use<MarketImpl>();
            ce.For<ICurrentMarket>().Singleton().Use<Mediachase.Commerce.Markets.CurrentMarketImpl>();
            ce.For<ISynchronizedObjectInstanceCache>().Singleton().Use<EPiServer.Events.RemoteCacheSynchronization>();
            ce.For<IObjectInstanceCache>().Use<HttpRuntimeCache>();
            //ce.For<ITypeScannerLookup>().Use<FakeTypeScannerLookup>();
            ce.For<IWarehouseRepository>().Singleton().Use<Mediachase.Commerce.Inventory.Database.WarehouseRepositoryDatabase>();
            ce.For<IChangeNotificationQueueFactory>().Singleton().Use<CommerceChangeQueueFactory>();
            ce.For<IPriceService>().Singleton().Use<PriceServiceDatabase>();
            ce.For<IPriceDetailService>().Use<PriceDetailDatabase>();
            ce.For<IWarehouseInventoryService>().Singleton().Use<WarehouseInventoryProxy>();
            ce.For<IInventoryService>().Singleton().Use<InventoryServiceProvider>();
            ce.For<IApplicationContext>().Use<FakeAppContext>();
            ce.For<CatalogConfiguration>().Use(CatalogConfiguration.Instance);
            ce.For<IRequiredMetaFieldCollection>().Singleton().Use<DefaultRequiredMetaFields>();
            ce.For<MetaDataContext>().Singleton().Use(() => CatalogContext.MetaDataContext);
            //ce.For<EventContext>().HybridHttpOrThreadLocalScoped().Use(eventContext);
            ce.For<FrameworkContext>().Use(() => FrameworkContext.Current);
            //ce.For<SqlContext>().Use(() => new SqlContext(BusinessFoundationConfigurationSection.Instance.Connection.Database));
            ce.For<IChangeNotificationManager>().Singleton().Use<ChangeNotificationManager>();

            ////ce.For<Mediachase.Commerce.Catalog.ICatalogSystem>().Singleton().Use(() => Mediachase.Commerce.Catalog.CatalogContext.Current);

            ce.For<IEventRegistry>().Use<EPiServer.Events.Clients.EventRegistry>();
            ce.For<IEventBroker>().Use<FakeEventBroker>();

            ce.For<Mediachase.Search.IndexBuilder>().Use<FakeIndexer>();
        });


        EPiServer.ServiceLocation.ServiceLocator.SetLocator(locator);

        string applicationName = context.JobDetail.Description;

        if (String.IsNullOrEmpty(applicationName) || applicationName == "all") // index all applications
        {
            AppDto dto = AppContext.Current.GetApplicationDto();

            foreach (AppDto.ApplicationRow row in dto.Application)
            {
                IndexApplication(row.Name);
            }
        }
        else
        {
            IndexApplication(applicationName);
        }
    }
    #endregion

    void IndexApplication(string applicationName)
    {
        _Log.Info(String.Format("Creating Search Manager for \"{0}\" Application.", applicationName));
        _SearchManager = new SearchManager(applicationName);

        _Log.Info("Created Search Manager.");

        try
        {
            _SearchManager.SearchIndexMessage += new SearchIndexHandler(_SearchManager_SearchIndexMessage);
            _SearchManager.BuildIndex(true);
        }
        catch (Exception ex)
        {
            _Log.Error("Search Manager Failed.", ex);
        }
    }

    void _SearchManager_SearchIndexMessage(object source, SearchIndexEventArgs args)
    {
        _Log.Info(String.Format("Percent Complete: {0}%, {1}", Convert.ToInt32(args.CompletedPercentage), args.Message));
    }

}

public class FakeEventBroker : IEventBroker
{
    public bool Enabled { get; set; }

    public System.Threading.Tasks.Task RaiseEventAsync(Guid eventId, Object parameter)
    {
        return null;
    }

    public event EventHandler<EventReceivedEventArgs> EventReceived;

    public event EventHandler<EventMissedEventArgs> EventMissed;
}
public class FakeAppContext : IApplicationContext
{
    public bool HasContentModelTypes { get; set; }

    public bool DisableVersionSync { get; set; }
}

public class FakeIndexer : Mediachase.Search.IndexBuilder
{
    public FakeIndexer() : base("","","")
    {

    }
}

我收到此错误“未注册默认实例,无法自动确定类型'EPiServer.Framework.Cache.IRequestCache”

在这一行“_SearchManager.BuildIndex(true);”

有任何想法吗?

4

2 回答 2

0

很难说,但我认为您需要在容器中注册 IRequestCache

IE

container.Configure(ce =>
{
    ce.For<IMarketService>().Use<MarketServiceDatabase>();
    ce.For<IMarket>().Use<MarketImpl>();
    ce.For<IRequestCache>().Use<NoRequestCache>(); // or whatever implementation you need
    ...
}
于 2017-03-28T05:49:50.770 回答
0

Schedule Job 正在尝试初始化 Commerce,很可能您需要修复更多的 IRequestCache,包括 DBContext,请参阅此处的集成示例。GIT 集成示例

于 2017-03-28T15:46:11.213 回答