orleans icon indicating copy to clipboard operation
orleans copied to clipboard

Nullref when calling injected IGrainServiceClient from a FooGrainService

Open borbmizzet opened this issue 8 years ago • 4 comments

I'm getting a nullref when trying to invoke an injected IBarGrainServiceClient from a FooGrainService. GrainServiceClient.CallingGrainReference is throwing a nullref due to RuntimeContext.Current?.ActivationContext?.Activation being null.

public interface IAService: IGrainService
{
  Task Execute();
}
public interface IAServiceClient: IGrainServiceClient<IAService>, IAService
{}
public interface IBService: IGrainService
{
  Task Execute();
}
public interface IBServiceClient: IGrainServiceClient<IBService>, IBService
{}

public interface ICService :IGrainService{}

public class CService :GrainService, ICService
{
        private readonly IAServiceClient _aService;
        private readonly IBServiceClient _bService;
        public CService(IAServiceClient aService, IBServiceClient, IGrainIdentity id, Silo silo, ILoggerFactory loggerFactory): base(id, silo, loggerFactory)
        {
            _aService = aService;
            _bService = bService;
        }
        public override async Task Start()
        {
            await base.Start();
            _timer = this.RegisterTimer(_ => Execute(), null, TimeSpan.Zero, TimeSpan.FromSeconds(30));
        }

        public override async Task Stop()
        {
            _timer?.Dispose();
            await base.Stop();
        }
        private async Task Execute() //service execution must be sequential
        {
            await _aService.Execute(); //throws at GrainServiceClient.GrainService_get
            await _bService.Execute();
        }
}

borbmizzet avatar Dec 04 '18 16:12 borbmizzet

Hi, how to you construct CService ? I asked because, GrainServiceClient needs to be called under an activation context for it to work properly. By that I mean, for example, CService is constructed from a grain's DI container. And Execute() is called from a grain's activation context.

xiazen avatar Dec 14 '18 20:12 xiazen

@xiazen I think I have a similar problem, could it be possible to get the GrainServiceClient from outside a grain activation context ? I tried from host.Services.GetService<IMyServiceClient>(); and I get an instance but its GrainService property return null.

@borbmizzet was it the problem on your side ?

jrouaix avatar Jan 21 '19 14:01 jrouaix

@jrouaix to my knowledge GrainServiceClient has to be called inside a grain activation context.

xiazen avatar Jan 24 '19 22:01 xiazen

close due to inactivity. Feel free to reopen.

xiazen avatar Mar 18 '19 18:03 xiazen