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!

What's new in Splunk Cloud Platform 9.1.2312?

Hi Splunky people! We are excited to share the newest updates in Splunk Cloud Platform 9.1.2312! Analysts can ...

What’s New in Splunk Security Essentials 3.8.0?

Splunk Security Essentials (SSE) is an app that can amplify the power of your existing Splunk Cloud Platform, ...

Let’s Get You Certified – Vegas-Style at .conf24

Are you ready to level up your Splunk game? Then, let’s get you certified live at .conf24 – our annual user ...