Awesomium C# WebControl, change proxy settings - c#

How can i change the proxy settings in Awesomium (c#)?
i've this simple code for now
Awesomium.Windows.Forms.WebControl browser =
new Awesomium.Windows.Forms.WebControl();
browser = new Awesomium.Windows.Forms.WebControl();
browser.Paint += browser_Paint;
browser.Location = new System.Drawing.Point(1, 1);
browser.Name = "webControl";
browser.Size = new System.Drawing.Size(1024, 768);
browser.Source = new System.Uri("http://checkip.dyndns.com/", System.UriKind.Absolute);
browser.TabIndex = 0;

WebPreferences prefs = new WebPreferences(){ ProxyConfig = "xxx.xxx.xxx.xxx:port" }
session = WebCore.CreateWebSession(prefs);
browser.WebSession = session;
browser.Source = new System.Uri("http://checkip.dyndns.com/", System.UriKind.Absolute);
check this answer for more details

You can set the proxy configuration on the WebCore that governs all of your webcontrols. Have a look at the WebCoreConfig.ProxyServer Property.
You need to create a WebCoreConfig object and then use it during WebCore construction.
Take a look at the WebCore API.

You need to add a WebSessionProvider control from the Toolbox to your form. Then set your Webcontrol's WebSessionProvider property to the one you just added.
Then, go (always in designer view) to the newly added WebSessionProvider's properties and expand the Preferences one: you will then find a ProxyConfig value to be filled with whatever proxy configuration you want to use.

Related

How to programmatically "translate" a page in EPiServer 10

I am currently creating a new page from code to use as my startpage for a sitedefinition which I also create from code..however.. even if I publish the newly created startpage I always end up with the following message in the CMS UI:
"This content is in English. It does not exist in svenska. Would you like to translate it now?"
How can I "Translate" the page from programmatically and then publish it as well? I haven't been able to find anything related to this here or while googling it.
You need to use the CreateLanguageBranch available in the IContentRepository.
In my example below Swedish is the default language on the site
var parent = ContentReference.RootPage;
IContentRepository contentRepository =
EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
StartpagePage startpage = contentRepository.GetDefault<StartpagePage>(parent);
startpage.PageName = "Teststartsida";
startpage.Title = "Teststartsida";
// this will create a startpage in the default language, Swedish in my case,
// use SaveAction.Publish and save the page into a new variable
var createdPage = contentRepository.Save(startpage,
EPiServer.DataAccess.SaveAction.Publish,
AccessLevel.NoAccess);
// invoke CreateLanguageBranch with LanguageSelector
var startpageLanguageBranch =
contentRepository.CreateLanguageBranch<StartpagePage>(createdPage,
new LanguageSelector("en"));
startpageLanguageBranch.PageName = "Start page test";
startpageLanguageBranch.Title = "Start page test";
// this will create a languagebranch in the language stated with the LanguageSelector.
// Use SaveAction.Save
contentRepository.Save(startpageLanguageBranch,
EPiServer.DataAccess.SaveAction.Save,
AccessLevel.NoAccess);

How to set ImageResizer configuration programmatically?

I want to set the configuration parameter clientcache.minutesprogrammatically but im struggling with the config design in ImageResizer.
My approach currently is:
var lWebConfigReader = new System.Xml.XmlTextReader(#"Web.config");
var lXmlDocument = new System.Xml.XmlDocument();
lXmlDocument.Load(lWebConfigReader);
var lResizerNode = lXmlDocument.SelectSingleNode("/configuration/resizer");
var lSection = new ImageResizer.ResizerSection(lResizerNode.OuterXml);
var lConfig = new ImageResizer.Configuration.Config(lSection);
int mins = lConfig.get("clientcache.minutes", -1);
...
ImageResizer.Configuration.Config.Current.setConfigXml(lConfig.getConfigXml());
It seems a bit hacky and also doesn't work as the ClientCache plugin doesn't sent the Expires header as it normally should when clientcache.minutes is set.
What could be the issue?
After some digging in the source code i found out that in this particular case you need to alter the global configuration object as the ClientCache plugin reads the parameter via Get() from it. So my current solution is:
// read a XML where a <resizer>...</resizer> is present, in this case a typical Web.config as mentioned in the ImageResizer docs
var lWebConfigReader = new System.Xml.XmlTextReader(#"Web.config");
var lXmlDocument = new System.Xml.XmlDocument();
lXmlDocument.Load(lWebConfigReader);
// read the resizer tag to a node
var lResizerNode = lXmlDocument.SelectSingleNode("/configuration/resizer");
// create a section from the node
var lSection = new ImageResizer.ResizerSection(lResizerNode.OuterXml);
// create a new config object from the section
var lConfig = new ImageResizer.Configuration.Config(lSection);
// override the global configugration with the newly created one
ImageResizer.Configuration.Config.Current.setConfigXml(lConfig.getConfigXml());
// test the Get() call used by the ClientCache plugin
int mins = ImageResizer.Configuration.Config.Current.get("clientcache.minutes", -1);
This code could be placed in a ICurrentConfigProvider implementation or Application_Start() in Global.asax.

Selenium can't handle multiple ChromiumWebBrowser instances in C#

I have two instances of the ChromiumWebBrowser in my WinForms project (Visual Studio 2012). My goal is to have the second browser instance "copy" the behavior of the user input in the first browser instance. I can successfully retrieve the input from the first browser, and I managed to hook up Selenium in the project as well.
However, I'm having one issue. Whenever Selenium sends its commands, the first browser is the one that responds to them. For the life of me, I can't seem to figure out how to make the second browser respond. Whenever I completely remove the first browser, the second one starts responding correctly, but adding the first browser again will make only have the first browser use the Selenium commands. I even tried to switch out the moments the browsers are added to the form, but to no avail: whenever there are two available, the wrong one is responsive.
Relevant code:
public BrowserManager(Controller controller, string startingUrl)
{
_controller = controller;
var settings = new CefSettings { RemoteDebuggingPort = 9515 };
Cef.Initialize(settings);
// Input browser
inputBrowser = new ChromiumWebBrowser(startingUrl);
var obj = new XPathHelper(this);
inputBrowser.RegisterJsObject("bound", obj); //Standard object registration
inputBrowser.FrameLoadEnd += obj.OnFrameLoadEnd;
// Output browser
var browserSettings = new BrowserSettings();
var requestContextSettings = new RequestContextSettings { CachePath = "" };
var requestContext = new RequestContext(requestContextSettings);
outputBrowser = new ChromiumWebBrowser(startingUrl);
outputBrowser.RequestContext = requestContext;
outputBrowser.AddressChanged += InitializeOutputBrowser;
outputBrowser.Enabled = false;
outputBrowser.Name = "outputBrowser";
}
The selenium part:
public class SeleniumHelper
{
public SeleniumHelper()
{
DoWorkAsync();
}
private Task DoWorkAsync()
{
Task.Run(() =>
{
string chromeDriverDir = #"ActionRecorder\bin\x64\Debug\Drivers";
var chromeDriverService = ChromeDriverService.CreateDefaultService(chromeDriverDir);
chromeDriverService.HideCommandPromptWindow = true;
ChromeOptions options = new ChromeOptions();
options.BinaryLocation = #"ActionRecorder\bin\x64\Debug\ActionRecorder.exe";
options.DebuggerAddress = "127.0.0.1:9515";
options.AddArguments("--enable-logging");
using (IWebDriver driver = new OpenQA.Selenium.Chrome.ChromeDriver(chromeDriverService, options))
{
driver.Navigate().GoToUrl("http://www.google.com");
var query = driver.FindElement(By.Name("q"));
query.SendKeys("A google search test");
query.Submit();
}
});
return null;
}
}
And finally, a screenshot for some visualization:
Some help with the issue would be very much appreciated. If i missed some crucial info, feel free to ask for it. Thanks in advance!
Greetz,
Tybs
The behavior is correct. You have one debug address and you can only have one debug address for CEF. Which means when you use Selenium it is only seeing one browser.
By default Selenium will send an command to current active Tab or Window. Now in your case you have multiple Chrome view embedded, but they are technically Chrome Tab/Windows which you have placed on the same form.
So if you are in luck below code in should be able to move you to the Window you are interested in
driver.SwitchTo().Window(driver.WindowHandles.Last());
See if it works. If it doesn't then your only other workaround would be to change the order of Adding ChromiumWebBrowser and that should reverse the window it works on.
Below are some important threads that you should read from top to bottom. Very relevant to your issue/request
https://code.google.com/archive/p/chromiumembedded/issues/421
https://github.com/cefsharp/CefSharp/issues/1076

Object reference not set to instance of object, despite creating a new object

I'm currently working on a basic drawing program. One of the requirements is to be able to save the list of drawn objects and load it back in. So far, I have written a save function that exports all items of the list to an XML format, and I'm currently working on the loading-part.
Whenever the program encounters a "< RechthoekTool >" (Dutch for RectangleTool), it executes the following code:
//Create new tool.
RechthoekTool tool = new RechthoekTool();
//Turn <Startpoint> and <Endpoint> into actual points
var sp = Regex.Replace(xn["Startpunt"].InnerText, #"[\{\}a-zA-Z=]", "").Split(',');
tool.startpunt = new Point(int.Parse(sp[0]), int.Parse(sp[1]));
var ep = Regex.Replace(xn["Eindpunt"].InnerText, #"[\{\}a-zA-Z=]", "").Split(',');
tool.eindpunt = new Point(int.Parse(ep[0]), int.Parse(ep[1]));
//Set colour and width of brush
string kleur = xn["Dikte"].InnerText;
kleur.Replace(#"Color [", "");
kleur.Replace(#"]", "");
Color c = Color.FromName(kleur);
tool.kwastkleur = c;
tool.kwast = new SolidBrush(c);
tool.dikte = int.Parse(xn["Dikte"].InnerText);
//Add to list
s.listItems.Add(tool);
Whenever I run the program, I get the 'NullReferenceException was unhandled' error("Object reference not set to an instance of an object.") at
s.listItems.Add(tool);
I do, however, instantiate the tool right at the beginning, don't I? What could be causing this error? Some Googling told me it might be because I forgot to assign a property, but as far as I can tell I've got them all covered...
Help would be greatly appreciated.
The error is due to s or s.listItems not being instantiated.
Without seeing more code, it's difficult to know which is null, but at a guess you are creating a new object for s, which contains a property/field listItems, but you aren't assigning a list to listItems.
The problem is that you aren't instantiating listItems or s. Not enough information is given to tell you how to instantiate s but you can do the other one like this:
s.listItems = new List<RechthoekTool>();
If you don't instantiate something using the new keyword; it simply won't work.
Using your code, try this:
//Create new tool.
RechthoekTool tool = new RechthoekTool();
//Turn <Startpoint> and <Endpoint> into actual points
var sp = Regex.Replace(xn["Startpunt"].InnerText, #"[\{\}a-zA-Z=]", "").Split(',');
tool.startpunt = new Point(int.Parse(sp[0]), int.Parse(sp[1]));
var ep = Regex.Replace(xn["Eindpunt"].InnerText, #"[\{\}a-zA-Z=]", "").Split(',');
tool.eindpunt = new Point(int.Parse(ep[0]), int.Parse(ep[1]));
//Set colour and width of brush
string kleur = xn["Dikte"].InnerText;
kleur.Replace(#"Color [", "");
kleur.Replace(#"]", "");
Color c = Color.FromName(kleur);
tool.kwastkleur = c;
tool.kwast = new SolidBrush(c);
tool.dikte = int.Parse(xn["Dikte"].InnerText);
List<RechthoekTool> s = new List<RechthoekTool>(); // You can now use your list.
//Add to list
s.listItems.Add(tool);

can not save application settings in .net 3.5

I am creating a application that store basic database connection info like host,user, password and default database name in application settings using User Scope.
I am using .net 3.5 with Visual Studio 2008
I put 4 text boxes in a user control and bound their text property to the application settings individual properties.
//
// textBox_database
//
this.textBox_database.Location = new System.Drawing.Point(103, 101);
this.textBox_database.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBox_database.Name = "textBox_database";
this.textBox_database.Size = new System.Drawing.Size(255, 27);
this.textBox_database.TabIndex = 5;
this.textBox_database.Text = global::PHP_Code_Generator_2.Properties.Settings.Default.mysql_database;
//
// textBox_password
//
this.textBox_password.Location = new System.Drawing.Point(103, 69);
this.textBox_password.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBox_password.Name = "textBox_password";
this.textBox_password.Size = new System.Drawing.Size(255, 27);
this.textBox_password.TabIndex = 4;
this.textBox_password.Text = global::PHP_Code_Generator_2.Properties.Settings.Default.mysql_password;
this.textBox_password.UseSystemPasswordChar = true;
//
// textBox_user
//
this.textBox_user.Location = new System.Drawing.Point(103, 37);
this.textBox_user.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBox_user.Name = "textBox_user";
this.textBox_user.Size = new System.Drawing.Size(255, 27);
this.textBox_user.TabIndex = 7;
this.textBox_user.Text = global::PHP_Code_Generator_2.Properties.Settings.Default.mysql_user;
//
// textBox_server
//
this.textBox_server.Location = new System.Drawing.Point(103, 5);
this.textBox_server.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.textBox_server.Name = "textBox_server";
this.textBox_server.Size = new System.Drawing.Size(255, 27);
this.textBox_server.TabIndex = 6;
this.textBox_server.Text = global::PHP_Code_Generator_2.Properties.Settings.Default.mysql_server;
these text boxes will get user data from users to set their own database info. i have a button that saves the modified info back to the settings file
private void button_save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
}
but the design is not being saved. any help anyone?
regards,
Anjan
To Cyril: Yes i had the code to assign modified value in properties, but then even the scope was set to "User" it said readonly. So i removed the property assignment codes.
now i brought the code back and restarted VS, it works perfect now :D silly me, i should have try this old procedure before.
Thank you Cyril.
Hmm... I remember having the same problem a few months ago in one of my projects. Unfortunately I don't recall how I solved it.
Some checks...
Try setting the property scope to User in the Project Properties.
In the code you've listed I don't see where you set the property (only see you retrieving the property). Where have you set it?

Categories