Problem creating ISphinxContext

When I try to create an ISphinxContext from within a Service method it fails as it cannot find the options

the call is

Result := SphinxServer1.CreateContext;

function TSphinxServer.CreateContext: ISphinxContext;
var
  Options: ICoreOptions;
  Manager: TObjectManager;
  Factory: IUserManagerFactory;
begin
  CheckConfig;
  if THttpServerContext.Current <> nil then
    Options := THttpServerContext.Current.Item<ICoreOptions>
//...
end;

and Options := THttpServerContext.Current.Item<ICoreOptions> returns nil

I'm sure I have done this before, but can't remember what I'm missing.

Ideas?

You are calling CreateContext from a service that is not a Sphinx service. In this case you should create the context manually.

But we will improve this for next version. If you want to do that at your side, please replace the TSphinxServer.CreateContext method with this one:

function TSphinxServer.CreateContext: ISphinxContext;
var
  Options: ICoreOptions;
  Manager: TObjectManager;
  Factory: IUserManagerFactory;
begin
  CheckConfig;
  Options := nil;
  if THttpServerContext.Current <> nil then
    Options := THttpServerContext.Current.Item<ICoreOptions>;
  if Options = nil then
    Options := TConfigCoreOptions.Create(Config);

  Manager := nil;
  if (TXDataOperationContext.Current <> nil) and (TXDataOperationContext.Current.Handler <> nil) and
    SameText(TXDataOperationContext.Current.Handler.XModel.Explorer.ModelName, cSphinxModelName) then
  begin
    Manager := TXDataOperationContext.Current.GetManager;
  end;

  Factory := nil;
  if TXDataOperationContext.Current <> nil then
    Factory := THttpServerContext.Current.Item<IUserManagerFactory>;
  if Factory = nil then
    Factory := TDefaultUserManagerFactory.Create;

  if Manager <> nil then
    Result := TSphinxContext.Create(Options, Factory, Manager)
  else
    Result := TSphinxContext.Create(Options, Factory, GetConnectionPool);
end;

Then rebuild your packages with tms build. Please let me know if this solves the issue.

1 Like

For the moment I've just created an ObjectManager as that is enough for what I need to do.

1 Like