silverlight exit call a Web service - silverlight

I want to send data to the server when you close the application
public App()
{
this.Startup += this.Application_Startup;
this.Exit += this.Application_Exit;
this.UnhandledException += this.Application_UnhandledException;
InitializeComponent();
}
private void Application_Exit(object sender, EventArgs e)
{
ClientReverse.UserExitGameAsync((Guid)Login);
}
Server:
public void UserExitGame(Guid UserGuid)
{
Games.Games.ExitUserGames(UserGuid);
}
but the server side is not satisfied.

It is already too late when you reach the ApplicationExit event.
I have seen Javascript that keeps on a webpage until confirmed (Stack Overflow does it a lot). You might want to modify a version of that Javascript that sends a message back to the Silverlight app before it allows page closing.
Calling Silverlight methods from JS is easy (you can simply expose SL methods to JS with the [Scriptable] attribute).

Related

FiddlerCore Without Registering As A System Proxy

I have a .NET Application that I need to alter some WCF traffic headers on. The Microsoft folks told me there is no way to really inject the headers I need in the request and pick them up out of the response with the current framework. What I would like to do is just add FiddlerCore to the application, and if the header isn't there on the outgoing request from my application, then I would like to add it. (Real simple).
I can get everything to work, however the events only fire if I register the FiddlerApplication as a system proxy. I would like this transparent to the user so that it doesn't screw up their proxy settings in the OS.
private void Form1_Load(object sender, EventArgs e)
{
Fiddler.FiddlerApplication.SetAppDisplayName("FiddlerCoreTester");
Fiddler.FiddlerApplication.RequestHeadersAvailable += this.RequestHeadersAvailable;
Fiddler.FiddlerApplication.BeforeRequest += this.BeforeRequest;
Fiddler.FiddlerApplication.AfterSessionComplete += this.SessionComplete;
FiddlerApplication.OnNotification += this.OnNotification;
Fiddler.FiddlerApplication.ResponseHeadersAvailable += this.ResponseHeadersAvailable;
Fiddler.URLMonInterop.SetProxyInProcess("127.0.0.1:80", "<-loopback>");
Fiddler.FiddlerApplication.Startup(80, false, false);
WebClient wc = new WebClient();
string s = wc.DownloadString("http://www.google.com");
System.Windows.Forms.MessageBox.Show(s);
Fiddler.FiddlerApplication.Shutdown();
}
private void ResponseHeadersAvailable(Session oSession)
{
}
private void OnNotification(object sender, NotificationEventArgs e)
{
}
private void SessionComplete(Session oSession)
{
}
private void RequestHeadersAvailable()
{
}
private void BeforeRequest(Fiddler.Session oSession)
{
if (oSession.RequestHeaders.Exists("TESTHEADER") == false) {
oSession.RequestHeaders.Add("TESTHEADER", "TEST");
}
}
The events never get called in this case, however if I change this over to the below it does:
Fiddler.FiddlerApplication.Startup(80, true, false);
Does anyone know how to get this working?
Thanks so much
You should let your webclient use FiddlerCore as the proxy. You need to do this by setting the proxy of the webclient equal to proxy url that Fiddler listens to. Now your webclient loads the string through http://127.0.0.1:80 and Fiddler can capture your request.
WebClient wc = new WebClient();
wc.Proxy = new WebProxy(new Uri("http://127.0.0.1:80"));
string s = wc.DownloadString("http://www.google.com");
Fiddler.FiddlerApplication.Shutdown();

Duplex WCF Client does not work with WinForms or WPF, but works fine with Console

I have a very peculiar case here.
I have implemented a wsDualHttpBinding WCF duplex server that is being consumed by a Windows Service.
The contract and implementation on the server side is defined as follows:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class FixServerContract : IFixContract
{
public FixServerContract()
{
}
public void Requestlogin(string message)
{
try
{
IServerCallback callback = OperationContext.Current.GetCallbackChannel<IServerCallback>();
callback.BroadcastToClient("Greetings from server");
}
catch (Exception ex)
{
}
}
[ServiceContract(CallbackContract=typeof(IServerCallback))]
public interface IFixContract
{
[OperationContract]
void Requestlogin(string message);
}
public interface IServerCallback
{
[OperationContract(IsOneWay = true)]
void BroadcastToClient(string eventData);
}
On the client end, I also have a single callback class, with the attached callback interface accessed by adding a Service Reference to the Client project as follows:
class MyCallbackClass : IFixContractCallback
{
public void RegisterClient()
{
InstanceContext context = new InstanceContext(this);
FixContractClient proxy = new FixContractClient(context);
proxy.Requestlogin("hello");
}
public void BroadcastToClient(string eventData)
{
}
}
When proxy.RequestLogin("hello"); is called by the client, the server should respond by signaling BroadcastToClient(string eventData)
But, here is where the peculiar behavior begins!
When my client calls RegisterClient directly from Main, outside of a Winform or WPF environment, everything works as it should be. That is, RequestLogin is successfully called to the server, and the server successfully responds by signalling BroadcastToClient on the client end.
This is shown here:
[STAThread]
private static void Main(string[] args)
{
MyCallbackClass callbackClass = new MyCallbackClass();
callbackClass.RegisterClient();
}
However, when I do the following below simply by wrapping the above lines inside an button_Click handler in the Winform, this bug manifests itself with a crash:
[STAThread]
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
and then have a button click do the rest like so:
private void button1_Click(object sender, EventArgs e)
{
MyCallbackClass callbackClass = new MyCallbackClass();
callbackClass.RegisterClient();
}
then the following bug manifests itself:
1) RequestLogin is called on the server side, but the call to BroadcastToClient never reaches the client. Further to this, the RequestLogin never returns to client side, but instead, will timeout with the following exception:
2) An unhandled exception of type 'System.TimeoutException' occurred in mscorlib.dll. This request operation sent to XXXX did not receive a reply within the configured timeout (00:00:59.6359791). The time allotted to this operation may have been a portion of a longer timeout. This may be because the service is still processing the operation or because the service was unable to send a reply message. Please consider increasing the operation timeout (by casting the channel/proxy to IContextChannel and setting the OperationTimeout property) and ensure that the service is able to connect to the client.
The same behavior occurs if I use a WPF client. On the otherhand, it works perfectly if I use a Console based client!
Why does my Duplex Callback only work outside of a Winform or WPF?
How should I go about resolving this issue?
I am at my witts end here.
Thanks.
Kudos to this individual who has supplied me with an answer:
https://groups.google.com/forum/#!topic/microsoft.public.dotnet.framework.webservices/8Y6C8dRCFws
The solution is to modify MyCallbackClass as follows:
[CallbackBehavior(UseSynchronizationContext = false)]
class MyCallbackClass : IFixContractCallback
{
public void RegisterClient()
{
MyCallbackClass tester = this;
InstanceContext context = new InstanceContext(this);
FixContractClient proxy = new FixContractClient(context);
proxy.Requestlogin("hello");
}
public void BroadcastToClient(string eventData)
{
}
}

Silverlight Web Service

Below is the code i used to consume web servcie in SilverLight.
private void button1_Click(object sender, RoutedEventArgs e)
{
BasicHttpBinding bind = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress("http://loalhost/Service.asmx");
ServiceSoapClient client = new ServiceSoapClient(bind, endpoint);
client.RunHelloCompleted += new EventHandler<RunHelloCompletedEventArgs>(client_RunQwinCompleted);
client.RunHelloAsync(command);
}
void client_RunHelloCompleted(object sender, RunHelloCompletedEventArgs e)
{
txtProcess.Text=Process(e.Result);
}
I want to know a way that after i run RunHelloAsync(Command), I want to get the returned result without going to Completed event. Please advise me. thank you.
Simple answer : You can't. Everything in Silverlight is Asynchronous so there is no way to block after the client.RunHelloAsync(command) call and wait for the result.
Long answer : There are ways to simulate working with calls in a synchronous fashion, but the calls still being made asynchronously. Take a look at this thread for a few more answers.

Silverlight 3 Out of the Browser Updates

I have a few users that are using a silverlight app that aren't recieving updates when a new release is published. Isn't this suppose to be automatic or perhaps I'm missing an option somewhere? I was also starting to think that maybe the XAP file is cached and I some how need to prevent that.
Any thoughts out there?
You need to write a few lines of code.
If you're familiar with 'one click' deployment then some of the options you're used to don't exist in Silverlight. You need to write the code yourself.
http://nerddawg.blogspot.com/2009/07/silverlight-out-of-browser-apps-how.html
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
if (Application.Current.IsRunningOutOfBrowser)
{
Application.Current.CheckAndDownloadUpdateAsync();
}
and then in your App() constructor :
Application.Current.CheckAndDownloadUpdateCompleted +=
new CheckAndDownloadUpdateCompletedEventHandler(Current_CheckAndDownloadUpdateCompleted);
and an event handler :
void Current_CheckAndDownloadUpdateCompleted(object sender, CheckAndDownloadUpdateCompletedEventArgs e)
{
// http://nerddawg.blogspot.com/2009/07/silverlight-out-of-browser-apps-how.html
if (e.UpdateAvailable)
{
MessageBox.Show("The application has been updated! Please close and reopen it to load the new version.");
}
else if (e.Error != null && e.Error is PlatformNotSupportedException)
{
MessageBox.Show("An application update is available, " +
"but it requires a new version of Silverlight. " +
"Please contact tech support for further instructions.");
}
}
It only auto updates if the developer performs the CheckAndDownloadUpdateAsync() call. See updates: http://timheuer.com/blog/archive/2009/07/10/silverlight-3-released-what-is-new-and-changed.aspx#oob

Download a html file in Silverlight

I'm trying to use the WebClient class to download a html file from another website and present it as a text stream, But I'm getting a security error, what am I doing wrong, or is this another one of Silverlights security "Features"
[code]
namespace ImageScrape
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
WebClient cl = new WebClient();
cl.OpenReadCompleted += new OpenReadCompletedEventHandler(cl_OpenReadCompleted);
cl.OpenReadAsync(new Uri(#"http://www.google.co.uk/",UriKind.Absolute));
}
void cl_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
testTextBlock.Text = e.Result.ToString();
}
}
}
[/code]
EDIT
Thanks guys, I was really hoping I wouldn't have to create this as a WCF service as 1) I only know the basics and 2) The idea is that you can use this .xap without having to connect to a central server, mainly because for this I don't have a server that I could host a WCF service on.
Does anyone know a way to get around this, or anywhere that would host a WCF service for free?
I think there are security issues with going directly to another site from the silverlight client.
The best work around for this would be to move this code into a web service and then serve the content you require to client from there.

Resources