I'm having a bit of trouble with a Windows Service webbrowser object. It's attempting to load in values of username and password to a site but keeps failing and throwing the following error:
System.InvalidCastException: Specified cast is not valid.
at System.Windows.Forms.UnsafeNativeMethods.IHTMLDocument2.GetLocation()
at System.Windows.Forms.WebBrowser.get_Document()
at MyWindowsService.MyDataProcessor.login()
The code that I'm using to make this call is:
MyWebBrowser.Document.All["Login"].SetAttribute("Value", username);
MyWebBrowser.Document.All["Password"].SetAttribute("Value", password);
MyWebBrowser.Document.All["submit"].InvokeMember("Click");
Any ideas as to why it keeps failing? Thanks in advance for the help.
I'm not sure if this solves the problem, but you can check InvokeRequired property on the current object, or WebBrowser.InvokeRequired, and use something like MethodInvoker to call your function or a helper function to access WebBrowser.Document.
http://www.megasolutions.net/cSharp/(WebBrowser_Document-==-null)-throws-InvalidCastException-43126.aspx
I had a similar problem using SHDocVW.WebBrowserClass. I got an InvalidCastException when I tried to access Document.all from an instance of SHDocVW.WebBrowserClass (from the main thread) and I was able to fix it by casting to IHTMLDocument2 instead of HTMLDocument. This took me a long time to figure out because casting to HTMLDocument works most of the time.
SHDocVW.WebBrowserClass Explorer = [instance of IE];
((IHTMLDocument2)Explorer.Document).all // works all the time
((HTMLDocument)Explorer.Document).all // works some times
I hope this helps someone.
Related
I'm using c# SeleniumWebdriver (chromedriver 2.38.*) and running into an error of the sort
unknown error: d.elementFromPoint is not a function
It happens when I do a call like this
Actions a = new Actions(Driver);
a.DoubleClick(theElement).Perform();
Respectively theElement.Location, theElement.Displayed, theElement.Enabled are not null, true, true.
I read stackOverflow a bit, and so to try to resolve this I called
js = (IJavaScriptExecutor)_webDriver;
js.ExecuteScript("arguments[0].scrollIntoView(true);", theElement);
but it didn't help! There is some shadow root stuff going on in the page, I don't know if that changes anything or not. What do people suggest to fix this general issue?
I'm aware that there is the method shown in the ErrorHAndler.dna example:
private object ErrorHandler(object exceptionObject)
{
ExcelReference caller = (ExcelReference)XlCall.Excel(XlCall.xlfCaller);
// Calling reftext here requires all functions to be marked IsMacroType=true, which is undesirable.
// A better plan would be to build the reference text oneself, using the RowFirst / ColumnFirst info
// Not sure where to find the SheetName then....
string callingName = (string)XlCall.Excel(XlCall.xlfReftext, caller, true);
[...]
}
But has anyone determined how the issue described in the comment might be resolved? I.e. is there a way to do this without setting IsMacroType=true?
You don't need IsMacroType=true to get the caller (the first line in your sample works fine without it). As the comment in your sample indicates, if you want to build a text description for the range, you need to build it yourself from the Row/Column info in the ExcelReference. You can get the sheet name with a call to XlCall.Excel(XlCall.xlSheetNm, caller).
I am getting an InvalidCastException after I migrated my solution from VS2010 to VS2013.
In VS2010 everything works fine, the same code gives the following error in VS2013:
An exception of type 'System.InvalidCastException' occurred in
System.Web.dll but was not handled in user code
Additional information: Unable to cast object of type
'System.Web.Hosting.SimpleWorkerRequest' to type
'System.Web.Hosting.IIS7WorkerRequest'.
I don't know what to do, couldn't find anything related on the internet.
The error occurs on line 5 in the following piece of code:
public static void AuthenticateUser(String UserName, Int32 Minutes = 30, Boolean IsPersistent = true)
{
FormsAuthenticationTicket _ticket = new FormsAuthenticationTicket(1, UserName, DateTime.Now, DateTime.Now.AddMinutes(Minutes), IsPersistent, String.Empty);
FormsIdentity _id = new FormsIdentity(_ticket);
var _roles = Roles.GetRolesForUser(UserName);
HttpContext.Current.User = new System.Security.Principal.GenericPrincipal(_id, _roles);
}
Technically, this is not an answer. But it helped me fix my problem.
Therefore I am sharing this with you all, hopefully this will prevent some headache for somebody in the near future.
In Visual Studio 2010 Professional I was running my project using the Visual Studio Development Server using a Specific Port (4302).
In Visual Studio 2013 Community edition i was running my project on IIS Express.
For some reason, I still do not know why, this caused my software to cast a 'IIS7WorkerRequest' to a 'SimpleWorkerRequest' object.
After I switched to the Local IIS Server with a custom virtual directory my error was gone. If there is somebody who can explain this, I would love to know why this is happening. But for now this fixed the problem above.
You cannot cast from a sibling class to another sibling; you can cast from parent to child, or child to parent, but not between siblings.
https://msdn.microsoft.com/en-us/library/ms173105.aspx
Since both SimpleWorkerRequest and IIS7WorkerRequest inherit from HttpWorkerRequest, you cannot cast between them. I would first ask yourself why you want to or think you have to perform this conversion, and if you cannot find an alternative then write a conversion method to map between them. However, this is usually a good sign that you're headed down the wrong path for your code.
Update: I downloaded Ramone project, added it to my project and then ran the application again with debugger. The error is shown below:
public MediaTypeWriterRegistration GetWriter(Type t, MediaType mediaType)
{
...
CodecEntry entry = SelectWriters(t, mediaType).FirstOrDefault(); => this line throws error
...
}
Error occurs in CodecManager.cs. I am trying to figure out why it does not recognize json-patch media type. Could it be because writer is not being registered correctly? I am looking into it. If you figure out the problem, please let me know. Since you are the author of the library, it will be easier for you to figure out the issue. I will have to go through all the code files and methods to find the issue. Thanks!
I was excited to know that Ramone library supports json-patch operations but when I tried it, I got following error:
415- Unsupported Media Type
This is the same error that I get when I use RestSharp. I thought may be RestSharp does not support json-patch and errors out so I decided to try Ramone lib but I still get same error. Endpoint has no issues because when I try same command using Postman, it works but when I try it programmatically in C#, it throws unsupported media type error. Here is my code:
var authenticator = new TokenProvider("gfdsfdsfdsafdsafsadfsdrj5o97jgvegh", "sadfdsafdsafdsfgfdhgfhehrerhgJ");
JsonPatchDocument patch = new JsonPatchDocument<MetaData>();
patch.Add("/Resident2", "Boyle");
//patch.Replace("/Resident", "Boyle");
RSession = RamoneConfiguration.NewSession(new Uri("https://api.box.com"));
RSession.DefaultRequestMediaType = MediaType.ApplicationJson;
RSession.DefaultResponseMediaType = MediaType.ApplicationJson;
Ramone.Request ramonerequest = RSession.Bind("/2.0/files/323433290812/metadata");
ramonerequest.Header("Authorization", "Bearer " + authenticator.GetAccessToken(code).AccessToken);
//var ramoneresponse = ramonerequest.Patch(patch); //results in error: 405 - Method Not Allowed
var ramoneresponse = ramonerequest.Put(patch); //results in error: 415 - Unsupported Media Type
var responsebody = ramoneresponse.Body
Endpoint information is available here: http://developers.box.com/metadata-api
I used json-patch section in the following article as a reference:
http://elfisk.dk/Ramone/Documentation/Ramone.pdf
By the way I tried Patch() method (as shown in above ref. article) but that resulted in "Method not allowed" so I used Put() method which seems to work but then errors out because of json-patch operation.
Any help, guidance, tips in resolving this problem will be highly appreciated. Thanks much in advance.
-Sham
The Box documentation says you should use PUT (which is quite a bit funny). The server even tells you that it doesn't support the HTTP PATCH method (405 Method Not Allowed) - so PUT it must be.
Now, you tell Ramone to use JSON all the time (RSession.DefaultRequestMediaType = MediaType.ApplicationJson), so you end up PUT'ing a JSON document to Box - where you should be PUT'ing a JSON-Patch document.
Drop the "RSession.DefaultRequestMediaType = MediaType.ApplicationJson" statement and send the patch document as JSON-Patch with the use of: ramonerequest.ContentType("application/json-patch+json").Put(...).
I'm trying to save a collection where a store a diagnostic log temporarily when navigating away from my app - I've looked at other sample code and it seems pretty basic as I've done below:
Saving:
PhoneApplicationService.Current.State["DiagnosticLog"] = DiagnosticLog;
Loading:
if (PhoneApplicationService.Current.State.ContainsKey("DiagnosticLog"))
DiagnosticLog = (ObservableCollection<DiagnosticLogEntry>)
PhoneApplicationService.Current.State["DiagnosticLog"];
However I get this error:
A first chance exception of type
'System.Runtime.Serialization.InvalidDataContractException' occurred
in System.Runtime.Serialization.dll
Any suggestions please?
Usually, when this happens, it means you do not have a default public constructor on your Diagnostic class (or one of it's contained classes).