All Apps and Add-ons

await service.Jobs.CreateAsync() dosent respond splunk c#

csharpdev
New Member

Hi There,

I am trying to integrate splunk into a windows forms application. the code goes into eternal wait mode at this line. No execution occurs after this line. Can you please lookinto the code and let me know if I am missing out on something? I am using splunk-sdk-csharp-pcl sdk from your site.

// On a button click event I am calling the method search().
private void btnSearch_Click(object sender, EventArgs e)
    {
        search();
    }
    public void search()
    {
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
        using (var service = new Service(SdkHelper.Splunk.Scheme, SdkHelper.Splunk.Host, SdkHelper.Splunk.Port, new Namespace(user: "nobody", app: "search")))
        {
            Run(service).Wait();
        }
    }
    static async Task Run(Service service)
    {
        //await service.LogOnAsync(SdkHelper.Splunk.Username, SdkHelper.Splunk.Password);

        //// Search : Pull model (foreach loop => IEnumerable)
        Job job = await service.Jobs.CreateAsync("search index=_internal | head 10"); // **EXECUTION STOPS HERE**
        SearchResultStream stream;

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

                MessageBox.Show("End of search results");
            }
            catch (Exception e)
            {
                MessageBox.Show(string.Format("SearchResults error: {0}", e.Message));
            }
        }

        //// Search : Push model (by way of subscription to search result records => IObservable)

        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();
                }));

            manualResetEvent.Reset();
            manualResetEvent.WaitOne();
        }
    }
Tags (1)
0 Karma
Get Updates on the Splunk Community!

Fall Into Learning with New Splunk Education Courses

Every month, Splunk Education releases new courses to help you branch out, strengthen your data science roots, ...

Super Optimize your Splunk Stats Searches: Unlocking the Power of tstats, TERM, and ...

By Martin Hettervik, Senior Consultant and Team Leader at Accelerate at Iver, Splunk MVPThe stats command is ...

How Splunk Observability Cloud Prevented a Major Payment Crisis in Minutes

Your bank's payment processing system is humming along during a busy afternoon, handling millions in hourly ...