Not able to call C# function for CosmosDb Change feed from power shell

Ankit Kumar 91 Reputation points
2020-12-04T15:50:20.943+00:00

I am able to run the below code, if I run it as a console application, Now my requirement is that InstanceTime, ProcessorName and ChangeInstance is passed as an argument from PowerShell and run this method and get the required changed documents output. I created the cmdlet successfully but it doesn't give me any output, though it creates the changefeed instance in cosmos db, but there is no output regarding the changed documents in Power shell window. I would like to know what wrong am I doing here. I am not getting any error and I tried Logging Warning and Verbose but I dont get any thing. the code excutes without running anything but if run as a seperate console application, i get the desired docs.

My code:

{
    [Cmdlet(VerbsCommon.Get, "ChangedRecording")]
    [OutputType(typeof(Recording))]

    public class SyncRecording : PSCmdlet
    {




        [Parameter(Mandatory = true,
            Position = 0,
            ValueFromPipeline = true,
            ValueFromPipelineByPropertyName = true, HelpMessage = "Provide the duration in days from when the changes in recording are seeked")]
        [ArgumentCompleter(typeof(TwisterArgumentCompleter))]
        public int DaysDuration { get; set; }

        [Parameter(Mandatory = true,
            Position = 1,
            ValueFromPipeline = true,
            ValueFromPipelineByPropertyName = true, HelpMessage = "Provide a Unique name for the Instance")]
        [ArgumentCompleter(typeof(TwisterArgumentCompleter))]
        public string changeFeedInstanceName { get; set; }

        [Parameter(Mandatory = true,
            Position = 2,
            ValueFromPipeline = true,
            ValueFromPipelineByPropertyName = true, HelpMessage = "Provide a Unique name for the ChangeProcessor")]
        [ArgumentCompleter(typeof(TwisterArgumentCompleter))]
        public string ChangeFeedProcessorName { get; set; }


        protected override void ProcessRecord()
        {
            Task.Run(async () =>
            {
                DaysDuration = DaysDuration * -1;
                DateTime particularPointInTime = DateTime.Now.AddDays(DaysDuration).ToUniversalTime();
                CosmosClient Client = new CosmosClient("AccountEndpoint = https://test.documents.azure.com:443/;AccountKey=*********;");
                if (DaysDuration == 0)
                {
                    particularPointInTime = DateTime.Now.ToUniversalTime();
                }
                var database = Client.GetDatabase("testdatabase");
                var container = database.GetContainer("testcontainer");
                var leaseContainer = database.GetContainer("leases");               
                var cfp = container.GetChangeFeedProcessorBuilder<dynamic>(ChangeFeedProcessorName, ProcessChanges)
                    .WithLeaseContainer(leaseContainer)
                    .WithInstanceName(changeFeedInstanceName)
                    .WithStartTime(particularPointInTime)
                    .Build();               
                await cfp.StartAsync();
                await Task.Delay(5000);
                await cfp.StopAsync();
            }).Wait();
        }


        public async Task ProcessChanges(IReadOnlyCollection<dynamic> docs, CancellationToken cancellationToken)
        {
            foreach (var doc in docs)
            {
                WriteObject(doc, true);
                await Task.Delay(1000);
            }

        }

    }
Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,434 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,180 questions
{count} votes