Splunk Dev

Can you please suggest as to why these errors are occurring? I copied and pasted this code into visual studio from Splunk documentation.

zd00191
Communicator

Error 1 The name 'manualResetEvent' does not exist in the current context
Error 2 Cannot await 'Splunk.Client.SearchResultStream'
Error 3 The name 'manualResetEvent' does not exist in the current context

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Text;
using System.Net;
using Splunk.Client;
using Splunk.Client.Helpers;

namespace Test
{
    public class Class1
    {     
        static void Main(string[] args)
        {
            //Service Point Manager for non-production splunk local instance
            #region
            ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
            {
                return true;
            };
            #endregion

            //Creates service instance for methods to log into with scheme, host, and port arguments
            using (var service = new Service(Scheme.Https, "localhost", 8089))
            {
                RunNormalSearches(service).Wait();
            }
        }


        //Normal Search Pull Model
        static async Task RunNormalSearches(Service service)
        {
            await service.LogOnAsync("admin", "CsIt30!q");

            Job job = await service.Jobs.CreateAsync("search index=_internal | head 10");
            SearchResultStream stream;

            using (stream = await job.GetSearchResultsAsync())
            {
                try
                {
                    foreach (SearchResult result in stream)
                    {
                        Console.WriteLine(string.Format("{0:D8}: {1}", stream.ReadCount, result));
                    }

                Console.WriteLine("End of search results");
                }

                catch (Exception e)
                {
                    Console.WriteLine(string.Format("SearchResults error: {0}", e.Message));
                }
            }

            Console.ReadLine();

            using (stream = await job.GetSearchResultsAsync())
            {
                stream.Subscribe(new Observer<SearchResult>(
                    onNext: (result) =>
                    {
                        Console.WriteLine(string.Format("{0:D8}: {1}", stream.ReadCount, result));
                    },
                    onError: (e) =>
                    {
                        Console.WriteLine(string.Format("SearchResults error: {0}", e.Message));
                        manualResetEvent.Set();
                    },
                    onCompleted: () =>
                    {
                        Console.WriteLine("End of search results");
                        manualResetEvent.Set();
                    }));

                await stream; // wait for stream to complete
            }
        }//EndOfAsyncRunNormalSearches

    }//EndOfClass1
}//EndOfNamespace
Tags (3)
0 Karma

gblock_splunk
Splunk Employee
Splunk Employee

Hi zd00191

Sorry you are having issues. You are getting a compilation error because manualResetEvent is not defined in that code snippet.

You can add the following code after line 60 and it should work:

var manualResetEvent = new ManualResetEvent(true);

I'll talk to our docs folks and get the code updated.

gblock_splunk
Splunk Employee
Splunk Employee

I see why. You cannot use await from a void method which is where you put your code ie in the main method.

Put your code in a method that returns a Task and then invoke it.

See how we did it here: https://github.com/splunk/splunk-sdk-csharp-pcl/blob/master/examples/normal-search/Program.cs

0 Karma

zd00191
Communicator

I get an error under line 61 when I insert the new line of code

0 Karma

gblock_splunk
Splunk Employee
Splunk Employee

What error?

0 Karma

gblock_splunk
Splunk Employee
Splunk Employee

Can you send me a gist showing the exact code with the line added?

0 Karma

zd00191
Communicator
    job = await service.Jobs.CreateAsync("search index=_internal | head 10");

            using (stream = await job.GetSearchResultsAsync())
            {
                var manualResetEvent = new ManualResetEvent(true);
                stream.Subscribe(new Observer<SearchResult>(
                    onNext: (result) =>
                    {
                        Console.WriteLine(string.Format("{0:D8}: {1}", stream.ReadCount, result));
                    },
                    onError: (e) =>
                    {
                        Console.WriteLine(string.Format("SearchResults error: {0}", e.Message));
                        manualResetEvent.Set();
                    },
                    onCompleted: () =>
                    {
                        Console.WriteLine("End of search results");
                        manualResetEvent.Set();
                    }));

                await stream; // wait for stream to complete
            }
0 Karma

gblock_splunk
Splunk Employee
Splunk Employee

And the error?

0 Karma

zd00191
Communicator

Error 1 Cannot await 'Splunk.Client.SearchResultStream'

0 Karma

martin_mueller
SplunkTrust
SplunkTrust

Do provide the URL where you got the code from.

zd00191
Communicator
0 Karma

jnicholsenernoc
Path Finder

What host are you trying to access? Localhost or somewhere else? Line 24

Do you get an error message when you try to use this class?

0 Karma

zd00191
Communicator

local host

I installed a free splunk enterprise instance this morning and added the sample tutorial data

0 Karma

zd00191
Communicator

These are the errors:
Error 1 The name 'manualResetEvent' does not exist in the current context

Error 2 Cannot await 'Splunk.Client.SearchResultStream'
Error 3 The name 'manualResetEvent' does not exist in the current context

0 Karma

zd00191
Communicator

Line 69, Line 74, Line 77 are where the errors are

0 Karma
Get Updates on the Splunk Community!

More Ways To Control Your Costs With Archived Metrics | Register for Tech Talk

Tuesday, May 14, 2024  |  11AM PT / 2PM ET Register to Attend Join us for this Tech Talk and learn how to ...

.conf24 | Personalize your .conf experience with Learning Paths!

Personalize your .conf24 Experience Learning paths allow you to level up your skill sets and dive deeper ...

Threat Hunting Unlocked: How to Uplevel Your Threat Hunting With the PEAK Framework ...

WATCH NOWAs AI starts tackling low level alerts, it's more critical than ever to uplevel your threat hunting ...