Here is the code for my Windows Form. I used the same info on the example application generated from the Splunk Template and it worked. I have tried several different ways to get this working and this is about as simple as I can get it and still no error and no response.
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
using Splunk.Client;
using System.Net;
namespace AI.Splunk.WinFormApp
{
public partial class Form1 : Form
{
private const string SplunkServerName = "servername";
private const int SplunkServerPort = 8089;
private const string SplunkUserName = "username";
private const string SplunkPassword = "password";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
BasicTest().Wait();
}
private async Task BasicTest()
{
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{
return true;
};
var service = new Service(Scheme.Https, SplunkServerName, SplunkServerPort);
try
{
await service.LogOnAsync(SplunkUserName, SplunkPassword);
}
catch(Exception ex)
{
richTextBox1.Text = ex.ToString();
}
richTextBox1.Text = "Done";
}
}
}
Hi jaeshort,
Have you tried using a BackgroundWorker. It worked for me.
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{
BGW_argumento bgw_arg = (BGW_argumento)e.Argument;
BackgroundWorker worker = sender as BackgroundWorker;
if (worker.CancellationPending == true)
{
e.Cancel = true;
}
else
{
RunSearch(bgw_arg).Wait(60*1000);
e.Result = bgw_arg;
worker.ReportProgress(100);
}
}
private async Task RunSearch(BGW_argumento p_bgw_argumento)
{
AOSplunkServer aosplunkserver = p_bgw_argumento.aosplunkserver;
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) =>
{ return true;};
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
var service = new Service(aosplunkserver.scheme, aosplunkserver.host, aosplunkserver.port);
try
{ await service.LogOnAsync(aosplunkserver.user, aosplunkserver.password);}
catch (Exception ex)
{ p_bgw_argumento.exception = ex;}
Job job = await service.Jobs.CreateAsync("search " + aosplunkserver.search);
SearchResultStream stream;
using (stream = await job.GetSearchResultsAsync())
{
try
{
p_bgw_argumento.result = "";
foreach (SearchResult result in stream)
{
//Console.WriteLine(string.Format("{0:D8}: {1}", stream.ReadCount, result));
p_bgw_argumento.result = p_bgw_argumento.result + result.ToString() + Environment.NewLine;
}
p_bgw_argumento.result = p_bgw_argumento.result.Trim();
}
catch (Exception e)
{
p_bgw_argumento.exception = e;
}
}
resultado = p_bgw_argumento.result;
}
struct BGW_argumento
{
public string result;
public Exception exception;
public AOSplunkServer aosplunkserver;
}
class AOSplunkServer
{
// Propiedades de usuario
public string user { get; }
public string password { get; }
// Propiedades de servidor
public string host { get; }
public int port { get; }
public Splunk.Client.Scheme scheme { get; } = Splunk.Client.Scheme.Https;
public string search { get; set; }
public string result { get; set; }
public AOSplunkServer(string p_user, string p_password, string p_host, int p_port)
{
user = p_user;
password = p_password;
host = p_host;
port = p_port;
}
}
There's a couple of different things going on here. The issue with LogOnAsync never completing might just be down to a deadlock with the UI thread. In order to get your example running, I changed your click handler to:
private async void button1_Click(object sender, EventArgs e)
{
await BasicTest();
}
If there's still an issue with the connection closing unexpectedly, you may need to specify:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
I'm getting an error too when I try this through WinForms, WPF, or a Universal App - but somehow it's working through a console app. I'm not 100% sure the error I'm seeing is the one you're seeing, though. To help me debug this, can you let me know what version of Splunk and .NET you're using, and what OS all this is running on?
Sorry for the delayed response!
I am using Splunk Enterprise 6.5.0, .NET 4.6.1, Windows 10 on Client, Windows Server 2012R2 Standard on Server.
Visual Studio 2015
Exception thrown: 'System.ArgumentNullException' in mscorlib.dll
I always get this error in the Output window when I call: await service.LogOnAsync("xxxxx", "xxxxx");
I added the Splunk project to the Windows Forms app, this project works when it is stand alone, but now does not work when called by the Windows app. I have no idea why this is such a problem.
I have upgraded the console example application to the latest packages. The only problem updating was a reference to System.Collections.Immutable. After all the updates the example app still works. I even added some parts of other examples into it and it worked.
I am not sure why this Windows application will not work. All the packages are the same, the .NET version is the same. All the settings seem similar.
Does anyone have an example of a windows application that can log in to a Splunk server with the C# SDK?