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!

Announcing Scheduled Export GA for Dashboard Studio

We're excited to announce the general availability of Scheduled Export for Dashboard Studio. Starting in ...

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics GA in US-AWS!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...