I wrote a method to post to Twitter using webclient. Everything works fine when calling the method from a console app.
I migrated the method to Silverlight 4. Here is where the fun begins. After cleaning up the code, switching to an asynchronous method call, and getting rid of the red squiggled underlines, the code now runs inside my SL4 app. The tweets do not, however, actually post to my twitter page.
I wired up an event handler for the "_completed" event. It gets fired.
Also wired up an event handler for the "_uploadProgressChanged" event. It never gets fired. Could be upload is too quick?
Any suggestions for troubleshooting this?
Update #2-
Correction... the "_completed" event DOES NOT get fired. I also added Fiddler to the mix to watch the traffic. It does not appear the app is transmitting anything. Fiddler does show activity if I post to stackoverflow (so Fiddler is working).
public static void PostTwitterUpdate(string handle, string pwd, string tweet)
{
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential(handle, pwd);
Uri uriString = new Uri("http://twitter.com/statuses/update.xml", UriKind.Absolute);
try
{
// event handlers added tongiht for debugging...
webClient.UploadProgressChanged += webClient_UploadProgressChanged;
webClient.UploadStringCompleted += webClient_UploadStringCompleted;
webClient.UploadStringAsync(uriString, "It's just me testing...");
}
catch (Exception ex)
{
throw;
}
}
static void webClient_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
{
// breakpoint set here for debugging...
}
static void webClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
// breakpoint set here for debugging...
}
This only would work in an elevated trust Out-of-Browser application because the Twitter Cross Domain Policy File at http://twitter.com/crossdomain.xml does not permit the calls for web page based apps.
Michael
Related
I have 2 projects acting as a webserver and a game server. The game server has the webserver as a dependency so I can access its files in the code and I then want to subscribe to an event in the web server.
I have a Discord bot sending information to the webserver that in turn needs to send data to the game server. But as I can't access the game servers file from the webserver I created a static event in the webserver that the game server, in turn, subscribes to. But the event is always null, no matter what I do. Is it even possible to subscribe to an event cross-project like this?
Here is the event:
public static event EventHandler<CommandEventArgs> RecievedCommand = delegate{};
Here is the invoking method:
public static void InvokeEvent(string exector, string command, string channel)
{
if (!ulong.TryParse(exector, out var user))
{
Console.WriteLine("Not a user");
return;
}
if (!ulong.TryParse(channel, out var from))
{
Console.WriteLine("not a channel");
return;
}
Console.WriteLine("Full invoke");
CommandEventArgs cmd = new CommandEventArgs();
cmd.Channel = channel;
cmd.Command = command;
cmd.Executor = exector;
RecievedCommand.Invoke(typeof(BotBridge), cmd);
}
Here you can see how I invoke it:
if (query["execute"] != null)
{
Console.WriteLine("Invoking");
BotBridge.InvokeEvent(query["user"], query["command"], query["channel"]);
Write(context, "Success!", false);
return;
}
And here is a picture from the game server where I subscribe to the event. The event is registered in the creation of this class so I know for sure the code reaches that part:
BotBridge.RecievedCommand += HandleBotCommand;
}
public void HandleBotCommand(object sender, CommandEventArgs e)
{
The webserver reaches Console.Writeline("Full invoke") in the invoking method but nothing else happens after that. Any ideas?
It's not possible to subscribe to an event from a separate process via a project reference.
Even though your game server is referring the "correct web server code/project", it's not pointing to the correct instance.
Here's what's happening:
Your game server code is subscribing to an event from the web server assembly, but in its own process. It's not subscribing to the actually deployed web server.
In red: what you were expecting.
In green: what is actually happening.
For your situation, you would need another communication mechanism such as HTTP, TCP, etc.
i made a winForms application that uses web browser control that enables the user to sign in with his google or facebook account then after getting the information or any error or exception the form which contains the web browser closes and will return back to the main form, i do the following, prepare the request, navigate() using that request then catch the response with the event Navigated.
in the code all is working fine, but when i build as Release and install the application the Navigated event is never fired up, which is really weird since nothing in my code is in debug condition, any ideas how to solve this issue??
the code at form's load event is:
private void GoogleLogin_Load(object sender, EventArgs e)
{
AuthHelper authHelper = new AuthHelper();
Uri loginURI = authHelper.GetGoogleAuthURI(_appID, redirectURI);
GoogleLoginControl.Navigate(loginURI);
}
the code at the web browser control at Navigated event
private void GoogleLoginControl_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
CoreStaticMembers.ologger.LogToFile("Navigated event with the absolute URL "+ e.Url.AbsoluteUri );
MessageBox.Show("Navigated event is fired");
///used if parameters are empty so ignore the following messages
bool ignore = false;
string code = null;
if (e.Url.AbsolutePath.Equals("/connect/login_success.html"))
{
AuthHelper authHelper = new AuthHelper();
//NameValueCollection parameters = HttpUtility.ParseQueryString(e.Url.Query);
try
{
NameValueCollection parameters = HttpUtility.ParseQueryString(e.Url.Query);
if (parameters != null)
{
string jsonResponse = null;
and after that i continue with normal authentication and data acquiring, keep in mind that this issue only appears when i build as Release then install the application, also the Navigating even is fired for the web browser control, so does the Load event for the parent form
Thanks in advance
UPDATE
by the way i also tried to Navigate with the required URL when i initialize the form by calling Initialize function inside the form's constructor, but the same problem appeared,
i found out that this problem is related to a compatibility issue related to a DLL file, particularly "Newtonsoft" DLL.
I'm trying to add share functionality to my Windows Phone App. The code behaves in an unpredictable way. Sometimes it works, but mostly it doesn't and I haven't been able to get any details about what's causing the crash. Could someone please go through the code below and let me know if I've missed something? Thanks!
public ArticlePage()
{
this.InitializeComponent();
//..
RegisterForShare();
}
private void RegisterForShare()
{
DataTransferManager dataTransferManager = DataTransferManager.GetForCurrentView();
dataTransferManager.DataRequested += new TypedEventHandler<DataTransferManager,
DataRequestedEventArgs>(this.ShareLinkHandler);
}
private void ShareLinkHandler(DataTransferManager sender, DataRequestedEventArgs e)
{
DataRequest request = e.Request;
DataRequestDeferral defferal = request.GetDeferral();
request.Data.Properties.Title = this.article.Title;
request.Data.Properties.Description = this.article.Summary;
request.Data.SetWebLink(new Uri(this.article.UrlDomain));
defferal.Complete();
}
private void ShareCommand_Click(object sender, RoutedEventArgs e)
{
DataTransferManager.ShowShareUI();
}
UPDATE
The code always works while I'm debugging from visual studio but pretty much never otherwise. I made a release build thinking there might be some code in the debug build which is causing the problem but that didn't make any difference.
I also had that problem recently. The share UI crashes when one of the important parameters is not set. In your case I'd suspect that
this.article.UrlDomain
is null or not a valid Uri pattern. You should build an if-clause around it and make sure that you're dealing with a real Uri. To test your code you should insert hardcoded constants and run it again. If it doesn't crash, check your Title, Summary and UrlDomain one by one.
Other places to investigate:
Try adding your handler in the OnNavigatedTo method and remove it when you're leaving the page
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
DataTransferManager.GetForCurrentView().DataRequested += SharePage_DataRequested;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
base.OnNavigatingFrom(e);
DataTransferManager.GetForCurrentView().DataRequested -= SharePage_DataRequested;
}
I also searched my code and looked at official samples again and did not find any defferals. Just to be sure - if I were you I'd strip all unnessecary lines in my code and get it as closest as possible to the official examples and then extend it back to where it was from there which is why I would comment out these two lines as well:
void SharePage_DataRequested(DataTransferManager sender, DataRequestedEventArgs args)
{
DataRequest request = e.Request;
//DataRequestDeferral defferal = request.GetDeferral();
request.Data.Properties.Title = this.article.Title;
request.Data.Properties.Description = this.article.Summary;
request.Data.SetWebLink(new Uri(this.article.UrlDomain));
//defferal.Complete();
}
Okay, I had the same problem. ShowShareUi actually suspends your app. If you try suspending your app you would get the error. It is actually the serialization problem.
If you want to look into the error, then while debugging, press the lifecycle events and suspend, you will crash in debug mode now.
If you are navigating between pages with a custom class you would get error. *My suggestion is that you would convert to jsonstring and send and get it back.*
I've faced similar problem (crash on ShowShareUI).
After very long investigations I've found, that this appears because unhandled exception in SaveFrameNavigationState (SuspensionManager class from template project).
In my case it was because SessionStateForFrame method failed on parsing class that couldn't be serialized.
Check out what you're saving in page state on SaveState of the page.
It happens not only on ShowShareUI but in suspend mode generally.
In a Windows 8 app (C#) using the MediaCapture class (Windows.Media.Capture) to show a webcam feed, I'm trying to restart the preview when the app has lost and then come back into focus (e.g. by clicking in the top left screen corner to another app, then clicking again to come back to my app).
How I'm trying to restart the preview right now is:
Application.Current.Resuming += (sender, o) => StartVideo(video);
Application.Current.Suspending += (sender, args) => StopVideo();
internal async void StartVideo(CaptureElement e)
{
try
{
this.stream = new MediaCapture();
await this.stream.InitializeAsync();
e.Source = this.stream;
await this.stream.StartPreviewAsync();
}
catch
{
new MessageDialog("Unable to start the video capture.").ShowAsync();
}
}
internal async void StopVideo()
{
try
{
await stream.StopPreviewAsync();
}
catch { }
}
But, the Resuming and Suspending events don't seem to fire in the example I describe above. Is this not "suspending" an app? If so, what is it/what events should I be on the lookout for?
Alternatively, should I, instead of using a long-running "preview" to display the webcam, use one of the this.stream.StartRecord... methods?
EDIT: If I fire the events manually using Visual Studio's Suspend/Resume button (on the Debug Location toolbar), the functionality works as desired (the video restarts when the app is resumed).
I see a few things wrong:
You should avoid async void; use async Task for all methods except event handlers.
For "command" events such as Suspending, use the deferral provided by args.SuspendingOperation.GetDeferral if you have an async event handler.
I'm new in Silverlight and i am doing some tests. With my current test I try to display in real time the current Clipboard content. But there is a weird behaviors with this code :
namespace SilverlightTest
{
public partial class MainPage : UserControl
{
private Timer _timer;
public MainPage()
{
InitializeComponent();
var dispatcher_timer = new DispatcherTimer {Interval = new TimeSpan(0, 0, 0, 5)};
dispatcher_timer.Tick += new EventHandler(timer_Callback);
dispatcher_timer.Start();
}
private void timer_Callback(object state, EventArgs eventArgs)
{
current_clip_board.Content = Clipboard.GetText();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
current_clip_board.Content = Clipboard.GetText();
}
}
}
The button Event and the timer Event are suppose to do exactly the same action.
But it doesn't ! The Button works fine and set the clipboard text into the label but the timer throw an exception :
Clipboard access is not allowed
The question is : why ? :)
Thanks.
PS : I would bet on a thread problem :p
Clipboard access, in a partial trust (in-browser) Silverlight application (the scenario you're likely referring to above), is restricted. The GetText property is accessible only in scenarios that the Silverlight runtime determines were initiated by the user. Your example is perfect -- by a button click for example. A dispatch timer however is not user initiated, so the property throws an exception (this is especially important within the context of a in-browser application, which could be a big security hole if you could create a Silverlight application that just ran silently in the browser, watching the user's clipboard updates without their knowledge).
See this clipboard documentation for more details.
Just trigger Clipboard.ContainsText() instead of Text. The method ContainsText is allowed!
Have you tried this:
private void timer_Callback(object state, EventArgs eventArgs)
{
Dispatcher.Invoke(new System.Threading.ThreadStart(delegate()
{
current_clip_board.Content = Clipboard.GetText();
}
}
edit
After a quick search, it appears that Clipboard is only available in response to a user action see here and here.
In partial trust (the default mode for
browser-hosted Silverlight-based
applications), Silverlight also
restricts clipboard access to its two
key APIs GetText and SetText. These
APIs can only be invoked from within a
context that is determined by the
Silverlight runtime to be in response
to a user-initiated action. For
example, clipboard access is valid
from within a handler for a Click or
KeyDown event. In contrast, clipboard
access is not valid from a handler for
Loaded or from a constructor, and
access attempts throw exceptions.
If your only option is to use a timer, then don't do it at all. The clipboad is a shared resource, and you're going to raise "cannot open clipboard" errors in other programs as they try to access the clipboard. i.e. user copies something from WinWord, WinWord tries to open the clipboard, but can't, because you've got it locked while you're examining it.
Hello this works for me but only in IE Microsoft.LightSwitch.Threading.Dispatchers.Main.BeginInvoke(() => HtmlPage.Window.Eval("window.clipboardData.setData('Text','testtestest')"));
just use getData method