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!

CX Day is Coming!

Customer Experience (CX) Day is on October 7th!! We're so excited to bring back another day full of wonderful ...

Strengthen Your Future: A Look Back at Splunk 10 Innovations and .conf25 Highlights!

The Big One: Splunk 10 is Here!  The moment many of you have been waiting for has arrived! We are thrilled to ...

Now Offering the AI Assistant Usage Dashboard in Cloud Monitoring Console

Today, we’re excited to announce the release of a brand new AI assistant usage dashboard in Cloud Monitoring ...