GeckoDocument.CreateEvent throws an exception - c#

I have a GeckoFX 29 GeckoWebBrowser in my C# application.
I navigate successfully to a webpage and I have an element that doesn't accept a simple "click()" and so I have to forcefully dispatch events.
The problem is that when I try to create the event the GeckoFX core throws an exception.
var eventName = "mouseover";
var domEvent = browser.DomDocument.CreateEvent(eventName);
domEvent.DomEvent.InitEvent(new nsAString(eventName), true, true);
The exception is thrown at CreateEvent(eventName) and the exception is this:
System.Runtime.InteropServices.COMException (0x80530009): Exception from HRESULT : 0x80530009
at Gecko.nsIDOMDocument.CreateEvent(nsAStringBase eventType)
at Gecko.nsString.GenericPass[T,TString](Func`2 func, String value) in c:\Users\micro_000\Documents\Visual Studio 2008\Projects\geckofx-29.0\Geckofx-Core\nsString.cs:line 221
at Gecko.nsString.Pass[T](Func`2 func, String value) in c:\Users\micro_000\Documents\Visual Studio 2008\Projects\geckofx-29.0\Geckofx-Core\nsString.cs:line 476
at Gecko.GeckoDomDocument.CreateEvent(String name) in c:\Users\micro_000\Documents\Visual Studio 2008\Projects\geckofx-29.0\Geckofx-Core\DOM\GeckoDomDocument.cs:line 155
at MyApp.Window.createEvent(GeckoDocument doc, String eventName)
I tried to look into the source code, but nsIDOMDocument is just an interface and there is no other information available for this issue that I can find.

It turns out that CreateEvent accepts only a few strings.
Because it didn't work I was trying to dispatch the events by calling a java script that I was going to insert in the document. Researching that I noticed that CreateEvent was used with the parameter "Events" and then you would init your event with the actual event name.
So far I found out that params that work with CreateEvent are "Events", "MouseEvent", "KeyboardEvent".
This code works:
var eventName = "mouseover";
var domEvent = browser.DomDocument.CreateEvent("MouseEvent");
domEvent.DomEvent.InitEvent(new nsAString(eventName), true, true);

Related

VS2019, C#, "just-my-code", and DLLs that aren't real [duplicate]

When debugging an application I always get the following error when break on exception is enabled in Visual Studio. This is really bugging me, since we work with break on exception. The funny thing is, that it still works when I continue (the StringCollection is loaded).
The Message is:
Could not load file or assembly 'System.XmlSerializers,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or
one of its dependencies. The system cannot find the file specified.
Here is the code that is causing the exception (designer generated)
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Specialized.StringCollection Mru {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["Mru"]));
}
set {
this["Mru"] = value;
}
}
I tried to create an empty test application that shows the error, but the exception didn't occur. Our project is huge so it tough to find the cause. Maybe someone on this site has a clue how to solve this.
Just an explanation for why this exception is thrown. You can repro the exception with this sample Windows Forms app. Start by adding a setting named "Setting" of type StringCollection. Click the dots in the Value column and enter a couple of strings. Make the form class code look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
Properties.Settings.Default.Setting[0] = DateTime.Now.ToString();
Properties.Settings.Default.Save();
base.OnFormClosing(e);
}
}
Debug + Exceptions, tick the Thrown checkbox for CLR exceptions. Run the form and close it, the debugger will stop when the exception is thrown. The top of the call stack looks like this:
mscorlib.dll!System.Reflection.Assembly.nLoad(System.Reflection.AssemblyName fileName, string codeBase, System.Security.Policy.Evidence assemblySecurity, System.Reflection.Assembly locationHint, ref System.Threading.StackCrawlMark stackMark, bool throwOnFileNotFound, bool forIntrospection) + 0x2c bytes
mscorlib.dll!System.Reflection.Assembly.InternalLoad(System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity, ref System.Threading.StackCrawlMark stackMark, bool forIntrospection) + 0x80 bytes
mscorlib.dll!System.Reflection.Assembly.Load(System.Reflection.AssemblyName assemblyRef) + 0x1d bytes
System.Xml.dll!System.Xml.Serialization.TempAssembly.LoadGeneratedAssembly(System.Type type = {Name = "StringCollection" FullName = "System.Collections.Specialized.StringCollection"}, string defaultNamespace = null, out System.Xml.Serialization.XmlSerializerImplementation contract = null) + 0xcd bytes
System.Xml.dll!System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type type = {Name = "StringCollection" FullName = "System.Collections.Specialized.StringCollection"}, string defaultNamespace = null) + 0x105 bytes
You can see the XmlSerializer class hunting for an assembly that contains the XML serializer for the StringCollection class. The LoadGeneratedAssembly method looks like this with the boring bits removed:
internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract)
{
...
AssemblyName parent = GetName(type.Assembly, true);
partialName = Compiler.GetTempAssemblyName(parent, defaultNamespace);
parent.Name = partialName;
parent.CodeBase = null;
parent.CultureInfo = CultureInfo.InvariantCulture;
try
{
serializer = Assembly.Load(parent); // <=== here
}
catch (Exception exception)
{
...
}
....
}
And Compiler.GetTempAssemblyName():
internal static string GetTempAssemblyName(AssemblyName parent, string ns)
{
return (parent.Name + ".XmlSerializers" + (((ns == null) || (ns.Length == 0)) ? "" : ("." + ns.GetHashCode())));
}
This GetTempAssemblyName is the evil-doer in this case. The StringCollection class lives in the System.dll assembly, the method generates the name "System.XmlSerializers". This method is designed to find the assembly for your own classes, the one generated by Sgen.exe. Like WindowsApplication1.XmlSerializers.dll for your sample program. But StringCollection is a class in the .NET Framework, the assembly name it generates just isn't valid. There isn't actually a "System.XmlSerializers.dll" assembly in the framework.
Feedback reports about this behavior at connect.microsoft.com have all been closed with "By Design". It was, the original designers considered the cost of preventing the exception too high and decided to just catch the exception. Which all works fine, the exception is indeed caught. You just happen to see it because you got the Thrown checkbox turned on in the Debug + Exceptions dialog.
Making the Xml serialization code behave differently here is not an option. It would have been easy enough for them to simply filter out types in the System.dll assembly, but that's a potentially never-ending battle, there are a lot more assemblies in the framework. A workaround is to use your own class to store the setting instead of using a StringCollection.
As this really seems to be part of the normal operation (see also:
XmlSerializer giving FileNotFoundException at constructor), I can only offer two workarounds:
Disable this specific exception: goto Debug/Exceptions, click Add, Type: C++ Exceptions, Name: EEFileLoadException (if this is the exception you're seeing), uncheck the Thrown checkbox for this exception.
Change the type of the setting to string and access it e.g. like so:
var mru = Settings.Default.Mru.Split('|');
Settings.Default.Mru = string.Join("|", mru.ToArray());
You are catching too many exceptions, the System.XmlSerializer will always throw this exception as part of it's normal operation, it is caught and handled by the class itself. Change your debugging options to only catch your exceptions, not exceptions that are caught and handled within the .net farmework classes.

Successful use of SP.SOD.executeOrDelayUntilEventNotified(func, eventName) in CSOM/JSOM?

I have used the SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName) quite a bit in my JSOM (https://msdn.microsoft.com/en-us/library/ff411788(v=office.14).aspx). Has anybody out there used SP.SOD.executeOrDelayUntilEventNotified(func, eventName) (https://msdn.microsoft.com/en-us/library/ff410354(v=office.14).aspx) successfully? For eventName is it as something as simple as "click"? I have searched the web but haven't found anything useful. Any Feedback appreciated.
Basically the difference between those functions that in first case the name of the file from client library is specified, for example sp.js (parameter depScriptFileName). In the latter case the event name should be specified, for example "sp.scriptloaded-sp.js" ( parameter eventName)
Here is the declaration for SP.SOD.executeOrDelayUntilEventNotified(func, eventName) from SharePoint Client library init.js:
function ExecuteOrDelayUntilScriptLoaded(func, depScriptFileName) {
depScriptFileName = depScriptFileName.toLowerCase();
var eventName = "sp.scriptloaded-" + depScriptFileName;
return ExecuteOrDelayUntilEventNotified(func, eventName);
}
About event names
The list of event names is stored in global variable called g_ExecuteOrWaitJobs. For every SharePoint Client library file is used a predefined event name, for example for a file sp.clienttemplates.js the corresponding event name is sp.scriptloaded-clienttemplates.js
Lets demonstrate how to utilize both SP.SOD.executeOrDelayUntilScriptLoaded(func, depScriptFileName) and SP.SOD.executeOrDelayUntilEventNotified(func, eventName) functions.
For that purposes let's introduce a simple example that prints SP.Web Title property:
function printWebInfo(){
var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
ctx.load(web,'Title');
ctx.executeQueryAsync(
function(){
console.log(web.get_title());
},
function(sender,args){
console.log(args.get_message());
});
}
In the following example
ExecuteOrDelayUntilScriptLoaded(printWebInfo, "sp.js");
printWebInfo function will be invoked once SharePoint Client library sp.js is loaded.
The same example that utilizes SP.SOD.executeOrDelayUntilEventNotified(func, eventName) will look like this:
var eventName = "sp.scriptloaded-sp.js";
ExecuteOrDelayUntilEventNotified(printWebInfo,eventName);
where "sp.scriptloaded-sp.js" event name is used to determine whether sp.js library is loaded or not.

System.Runtime.InteropServices.SEHException in UnityEngine.dll

I am importing unity3D project for windows phone 8. and adding few lines of code which interact the c# code to the event changing handler of Object(SphereScript) in unity3D.
var sphereScript = UnityEngine.Object.FindObjectOfType<SphereScript>();
sphereScript.SphereStateChanged += sphereScript_SphereStateChanged;
sphereScript_SphereStateChanged(sphereScript.IsSphereMoving);
The project compiled fine with no errors but when it runs on phone it gives an error on line
UnityEngine.Object.FindObjectOfType<SphereScript>();
An exception of type 'System.Runtime.InteropServices.SEHException' occurred in UnityEngine.DLL but was not handled in user code. i don't know why this error occurs. but first time when i started the project it asked me to locate the file name UnityEngineObject.cs. Where can i find this file or how can i solve the problem. Thanks.
Url for complete code http://docs.unity3d.com/Manual/wp8-unity-interaction.html
Exception details:
System.Runtime.InteropServices.SEHException was unhandled by user code
HResult=-2147467259
Message=External component has thrown an exception.
Source=UnityEngine
ErrorCode=-2147467259
StackTrace:
at UnityEngine.Internal.$Calli.Invoke38(Int32 arg0, IntPtr method)
at UnityEngine.Object.FindObjectsOfType(Type type)
at UnityEngine.Object.FindObjectOfType(Type type)
at UnityEngine.Object.FindObjectOfType[T]()
at UnityXamlInteractionExample.MainPage.Unity_Loaded()
InnerException:
in Unity 5x i had equal the problem.
So try this:
In script SphereScript.cs:
re-write this:
public event Action<bool> SphereStateChanged;
to this:
public static event Action<bool> SphereStateChanged = delegate { };
next, in Visual Studio after build project un-comment these parts of code:
UnityApp.SetLoadedCallback(() => { Dispatcher.BeginInvoke(Unity_Loaded); });
private void Unity_Loaded() { }
now insert into private void Unity_Loaded() this:
ShareScript.SphereStateChanged += SphereStateChanged;
and finally add this:
void SphereStateChanged(bool currentSpehreState)
{
Dispatcher.BeginInvoke(() =>
{
SphereStateTextBlock.Text = currentSpehreState ? "Spehre is moving" : "Sphere is stopped";
});
}
Good luck, bye :)

MyAssembly.XmlSerializers.dll cannot be loaded while debugging (FileNotFound) [duplicate]

When debugging an application I always get the following error when break on exception is enabled in Visual Studio. This is really bugging me, since we work with break on exception. The funny thing is, that it still works when I continue (the StringCollection is loaded).
The Message is:
Could not load file or assembly 'System.XmlSerializers,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or
one of its dependencies. The system cannot find the file specified.
Here is the code that is causing the exception (designer generated)
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::System.Collections.Specialized.StringCollection Mru {
get {
return ((global::System.Collections.Specialized.StringCollection)(this["Mru"]));
}
set {
this["Mru"] = value;
}
}
I tried to create an empty test application that shows the error, but the exception didn't occur. Our project is huge so it tough to find the cause. Maybe someone on this site has a clue how to solve this.
Just an explanation for why this exception is thrown. You can repro the exception with this sample Windows Forms app. Start by adding a setting named "Setting" of type StringCollection. Click the dots in the Value column and enter a couple of strings. Make the form class code look like this:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e) {
Properties.Settings.Default.Setting[0] = DateTime.Now.ToString();
Properties.Settings.Default.Save();
base.OnFormClosing(e);
}
}
Debug + Exceptions, tick the Thrown checkbox for CLR exceptions. Run the form and close it, the debugger will stop when the exception is thrown. The top of the call stack looks like this:
mscorlib.dll!System.Reflection.Assembly.nLoad(System.Reflection.AssemblyName fileName, string codeBase, System.Security.Policy.Evidence assemblySecurity, System.Reflection.Assembly locationHint, ref System.Threading.StackCrawlMark stackMark, bool throwOnFileNotFound, bool forIntrospection) + 0x2c bytes
mscorlib.dll!System.Reflection.Assembly.InternalLoad(System.Reflection.AssemblyName assemblyRef, System.Security.Policy.Evidence assemblySecurity, ref System.Threading.StackCrawlMark stackMark, bool forIntrospection) + 0x80 bytes
mscorlib.dll!System.Reflection.Assembly.Load(System.Reflection.AssemblyName assemblyRef) + 0x1d bytes
System.Xml.dll!System.Xml.Serialization.TempAssembly.LoadGeneratedAssembly(System.Type type = {Name = "StringCollection" FullName = "System.Collections.Specialized.StringCollection"}, string defaultNamespace = null, out System.Xml.Serialization.XmlSerializerImplementation contract = null) + 0xcd bytes
System.Xml.dll!System.Xml.Serialization.XmlSerializer.XmlSerializer(System.Type type = {Name = "StringCollection" FullName = "System.Collections.Specialized.StringCollection"}, string defaultNamespace = null) + 0x105 bytes
You can see the XmlSerializer class hunting for an assembly that contains the XML serializer for the StringCollection class. The LoadGeneratedAssembly method looks like this with the boring bits removed:
internal static Assembly LoadGeneratedAssembly(Type type, string defaultNamespace, out XmlSerializerImplementation contract)
{
...
AssemblyName parent = GetName(type.Assembly, true);
partialName = Compiler.GetTempAssemblyName(parent, defaultNamespace);
parent.Name = partialName;
parent.CodeBase = null;
parent.CultureInfo = CultureInfo.InvariantCulture;
try
{
serializer = Assembly.Load(parent); // <=== here
}
catch (Exception exception)
{
...
}
....
}
And Compiler.GetTempAssemblyName():
internal static string GetTempAssemblyName(AssemblyName parent, string ns)
{
return (parent.Name + ".XmlSerializers" + (((ns == null) || (ns.Length == 0)) ? "" : ("." + ns.GetHashCode())));
}
This GetTempAssemblyName is the evil-doer in this case. The StringCollection class lives in the System.dll assembly, the method generates the name "System.XmlSerializers". This method is designed to find the assembly for your own classes, the one generated by Sgen.exe. Like WindowsApplication1.XmlSerializers.dll for your sample program. But StringCollection is a class in the .NET Framework, the assembly name it generates just isn't valid. There isn't actually a "System.XmlSerializers.dll" assembly in the framework.
Feedback reports about this behavior at connect.microsoft.com have all been closed with "By Design". It was, the original designers considered the cost of preventing the exception too high and decided to just catch the exception. Which all works fine, the exception is indeed caught. You just happen to see it because you got the Thrown checkbox turned on in the Debug + Exceptions dialog.
Making the Xml serialization code behave differently here is not an option. It would have been easy enough for them to simply filter out types in the System.dll assembly, but that's a potentially never-ending battle, there are a lot more assemblies in the framework. A workaround is to use your own class to store the setting instead of using a StringCollection.
As this really seems to be part of the normal operation (see also:
XmlSerializer giving FileNotFoundException at constructor), I can only offer two workarounds:
Disable this specific exception: goto Debug/Exceptions, click Add, Type: C++ Exceptions, Name: EEFileLoadException (if this is the exception you're seeing), uncheck the Thrown checkbox for this exception.
Change the type of the setting to string and access it e.g. like so:
var mru = Settings.Default.Mru.Split('|');
Settings.Default.Mru = string.Join("|", mru.ToArray());
You are catching too many exceptions, the System.XmlSerializer will always throw this exception as part of it's normal operation, it is caught and handled by the class itself. Change your debugging options to only catch your exceptions, not exceptions that are caught and handled within the .net farmework classes.

Custom task runner method throws ArgumentException

Because of the fact that CultureInfo is not being copied from thread to thread I have made following method to do that thing for me.
public static StartCustomTask(Action action, TaskCreationOptions tco = TaskCreationOptions.None)
{
var currentCult = Thread.CurrentThread.CurrentCuture;
var currentUiCult = Thread.CurrentThread.CurrentUICulture;
return Task.Factory.StartNew(() =>
{
Thread.CurrentThread.CurrentCuture = currentCult;
Thread.CurrentThread.CurrentUICulture = currentUiCult;
action();
}, tco);
}
basically this code copies culture info from current thread to the thread that is gonna execute the action. I don't know why but it throws System.ArgumentException saying Value does not fall within the expected range. I've tried to run the action itself regularly on the main thread and it goes perfectly. By that I mean, that method that is being an action does not have a problem itself there is a problem somewhere in the code above I guess.
here is the stack trace of an exception
at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)
at System.Web.Hosting.IIS7WorkerRequest.GetServerVariableInternal(String name)
at System.Web.HttpRequest.BuildUrl(Func`1 pathAccessor)
at System.Web.HttpRequest.get_Url()
at SL.CoreLogic.Web.FrontEnd.Controllers.AccountController.<>c__DisplayClass37.<ResetPassword>b__31() in d:\CoreProjects\CoreLogic\CoreLogic-RusSlot\SL.CoreLogic\SL.CoreLogic.Web.FrontEnd\Controllers\AccountController.cs:line 447
at SL.CoreLogic.Common.CustomTask.<>c__DisplayClass1.<StartWithCurrentCulture>b__0() in d:\CoreProjects\CoreLogic\CoreLogic-RusSlot\SL.CoreLogic\SL.CoreLogic.Common\CustomTask.cs:line 22
at System.Threading.Tasks.Task.
and one more thing. this code was working perfectly but all of a sudden it started doing this.
I got it. the problem was that the action contained a line like this Url = Request.Url, as I guess at the time code was being executed the Request object did not exist or was not set.

Categories