Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How I can fix this issue using this repo with WinForms app? #2

Open
zydjohnHotmail opened this issue Jun 10, 2022 · 1 comment
Open

Comments

@zydjohnHotmail
Copy link

Hello:
I found your repo., it seems to be a good one, so I wanted to give it a try.
For using the console app by following the example from here:
https://github.com/mahdibland/Selenium-Proxy-Authentication.Chrome/blob/main/SeleniumProxyAuthentication.Sample/Program.cs
It works.

However, I want to use this repo in WinForms app. So have done the following:

  1. Create a WinForms app project targeting .NET 6.0 (X64 CPU) on Windows 10 (Version 21H2). My IDE is Visual Studio 2022.

  2. Install NUGET packages:
    PM> Install-Package Selenium.Support -Version 4.2.0
    PM> Install-Package Selenium.WebDriver -Version 4.2.0
    PM> Install-Package Selenium.WebDriver.ChromeDriver -Version 102.0.5005.6102
    PM> Install-Package SeleniumProxyAuthentication.Chrome -Version 2.1.1

  3. The following is my C# code trying to do the same as console app.

using OpenQA.Selenium.Chrome;
using SeleniumProxyAuthentication;

namespace SeleniumProxy1Form
{
public partial class Form1 : Form
{

    private static readonly ChromeOptions Chrome_Options = new();

#pragma warning disable CA2211 // Non-constant fields should not be visible
public static ChromeDriver Proxy_Driver;
#pragma warning restore CA2211 // Non-constant fields should not be visible

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Chrome_Options.AddProxyAuthenticationExtension(new Proxy(ProxyProtocols.HTTP, 		"proxy_server:proxy_port:proxy_username:proxy_password"));));
    }

    private async void BTNUKProxy_Click(object sender, EventArgs e)
    {
        Proxy_Driver = new(Chrome_Options)
        {
            Url = "http://ip-api.com/json/"
        };
        await Task.Delay(5000);
        Proxy_Driver.Navigate().GoToUrl("https://www.bing.com/");
        Console.Beep();
    }
}

}
But when I run my program, I always get this kind of error:
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Exception has been thrown by the target of an invocation.
Source=System.Private.CoreLib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Delegate.DynamicInvoke(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, WM msg, IntPtr wparam, IntPtr lparam)
at Interop.User32.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at SeleniumProxy1Form.Program.Main() in C:\Selenium\SeleniumProxy1Form\SeleniumProxy1Form\Program.cs:line 14

This exception was originally thrown at this call stack:
Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions)
Microsoft.Win32.SafeHandles.SafeFileHandle.Open(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long)
System.IO.Strategies.OSFileStreamStrategy.OSFileStreamStrategy(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long)
System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, System.IO.FileOptions, long)
System.IO.Strategies.FileStreamHelpers.ChooseStrategy(System.IO.FileStream, string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int, System.IO.FileOptions, long)
System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int, bool)
System.IO.Compression.ZipFileExtensions.DoCreateEntryFromFile(System.IO.Compression.ZipArchive, string, string, System.IO.Compression.CompressionLevel?)
System.IO.Compression.ZipFile.DoCreateFromDirectory(string, string, System.IO.Compression.CompressionLevel?, bool, System.Text.Encoding)
System.IO.Compression.ZipFile.CreateFromDirectory(string, string)
SeleniumProxyAuthentication.Utilities..cctor.AnonymousMethod__9_5(string, string)
...
[Call Stack Truncated]

Inner Exception 1:
IOException: The process cannot access the file 'C:\Users\John\AppData\Roaming\ProxyAuthCache\4.3.1.1796950237348\Details\manifest.json' because it is being used by another process.

By the way, the following C# code works for console app project.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;

namespace SeleniumProxyAuthentication.Sample
{
public class Program
{
private static readonly ChromeOptions Chrome_Options = new();

    static void Main()
    {
        Chrome_Options.AddProxyAuthenticationExtension(new Proxy(ProxyProtocols.HTTP, 		"proxy_server:proxy_port:proxy_username:proxy_password"));));
        IWebDriver driver = new ChromeDriver(Chrome_Options);
        driver.Navigate().GoToUrl(new Uri("http://ip-api.com/json/"));
        Console.WriteLine("Done!");
        Console.Beep();
    }
}

}

I was thinking that my console project is using the json file, but I stop the console project, and change different proxy, or I even reboot my PC. But I still get the same error, but the console app project always works.
Please let me know how I can fix this issue, why it happens only for WinForms app?
Thanks,

@sagihgithub
Copy link

sagihgithub commented Aug 29, 2023

Hi @zydjohnHotmail
I'm experiencing the same error,
I'm also using

options.AddProxyAuthenticationExtension(new SeleniumProxyAuthentication.Proxy(
        ProxyProtocols.HTTP,
        "ip:port:user:pass"));

This code run in a .net core application inside a loop with a 30 second interval
Did you manage to found an answer ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants