Download a html file in Silverlight - 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.

Related

Silverlight application ria service list files in service side

Having a silverlight application, intended to implement backup restore mechanism for the end user.
I have to get list of files in a specific directory resided in WebSite project via ria services.
By using which object I will be able to list files in specific directory of WebSite project.
Thanks for your attention.
You can use the Directory class to enumerate files on the server. Adding a method to your domain service to return the list of file names to the Silverlight client should be fairly trivial after that.
http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx
The answer is some kind of hack. I was inspired by the method that I have used to send client IP address to the service.
In default.aspx add this param to your silverlight object :
<param name="initParams" value="clientIP=<%=Request.UserHostAddress%>,serverPath=<%=Server.MapPath(".")%>" />
And in silverlight application :
public string ClientIP=string.Empty;
public string ServerPath = string.Empty;
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
try
{
ClientIP = e.InitParams["clientIP"].ToString();
ServerPath = e.InitParams["serverPath"].ToString();
}
catch
{
}
}
Consider that I sent client ip to the xap file for logging issues. You can omit it if you care.
and in Silverlight application call service method in this way :
ser.GetFileList(((App)(App.Current)).ServerPath, FilesListReceived, null);
And the service side :
public List<string> GetFileList(string baseDirectory)
{
var result = new List<BRFile>();
var files =Directory.EnumerateFiles( baseDirectory + "\\DBBackup" );
....
}
Good Luck.

silverlight exit call a Web service

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).

Silverlight calling WCF

I am new to silverlight, just wanted know if the steps involved in calling the WCF service same in Silverlight as it was in asp or is there any difference.If there is any difference then request you to help me out.
Thanks in advance.
The main things that you needs aware are
silverlight support only basichttpbinding
you need to have either clientaccesspolicy.xml file or crossdomain.xml in your root folder of wcf host server, then only you can successfully call a webservice from silerlight
http://msdn.microsoft.com/en-us/library/cc197955%28v=vs.95%29.aspx
I got the answer, there is slight difference between asp and silverlight.
In silverlight all service calls are asynchronous, so you have to have an eventhandler for when the async call is completed you get your data.
Just a small e.g
When you call your WCF service from asp, you use
proxy_http.FunctionClient fc = new proxy_http.FunctionClient();
txtDisplay.Text = fc.Add(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text)).ToString();
in case of Silverlight you will use
private void Add_Click(object sender, RoutedEventArgs e)
{
proxy_htt.FunctionClient fc = new proxy_htt.FunctionClient();
fc.AddCompleted += new EventHandler<proxy_htt.AddCompletedEventArgs>(fc_AddCompleted);
fc.AddAsync(Convert.ToInt32(txtFirst.Text),Convert.ToInt32(txtSecond.Text));
}
void fc_AddCompleted(object sender, proxy_htt.AddCompletedEventArgs e)
{
txtResult.Text = e.Result.ToString();
}

Problem accessing localhost URI from Silverlight

(repost because of SO outage; apologies if the other one re-appears)
I'm building a Silverlight app that will run on Azure. My VS solution has two projects: the web role and the Silverlight. The web role has a Service that works. (I can go to localhost:88/expenseservice.svc/expenses and get the data I want.)
I am trying to access that data from Silverlight:
private void MainPage_Loaded(object sender, RoutedEventArgs args)
{
WebClient data = new WebClient();
data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(data_DownloadStringCompleted);
Uri dataSource = new Uri("localhost:88/expenseservice.svc/expenses");
data.DownloadStringAsync(dataSource);
}
void data_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error.InnerException.Message);
return;
}
// ...
}
However, this does not work. The message box shows the error:
The URI prefix is not recognized.
Here is the full exception:
e.Error.InnerException = {System.NotSupportedException: The URI prefix is not recognized.
at System.Net.WebRequest.Create(Uri requestUri)
at System.Net.WebClient.GetWebRequest(Uri address)
at System.Net.WebClient.DownloadStringAsync(Uri address, Object userToken)}
Is it complaining about localhost? Am I supposed to be doing something differently? Perhaps this is what "Add Service Reference" is for?
I think the prefix is not recognized because it is missing. The prefix should be the first part that describes what kind of service your URI is pointing to. For example http:// of svn:// and so on..
Just add the right one and it should work.. (I've never used Silverlight neither anything Microsoftish so I'm just guessing)

Is it possible to show/hide UserControls within a Silverlight XAP file from JavaScript?

I've created a Silverlight project that produces [something].xap file to package a few silverlight UserControls. I would like to manipulate that .xap file through the use of javascript in the browser to show and hide user controls based upon java script events.
Is it possible to do this?
If so any sample could or links to documentation would be appreciated.
Thanks in advance
Kevin
Here's my solution...not sure if it's the "best-practices" way...comments????
In the App class within my Silverlight application I have the following code:
private Page _page = null;
private void Application_Startup(object sender, StartupEventArgs e)
{
_page = new Page();
this.RootVisual = _page;
HtmlPage.RegisterScriptableObject("App", this);
}
Also to the App class I add a [ScriptableMember] to be called from JavaScript
[ScriptableMember]
public void ShowTeamSearch(Guid ctxId, Guid teamId)
{
_page.ShowTeamSearcher(ctxId, teamId);
}
The Page class is the default one that get's created within the Silverlight Control project, it really doesn't have any UI or logic, it's just used to swap in/out the views.
Login oLogin;
TeamSearcher oSearcher;
public Page()
{
InitializeComponent();
oLogin = new Login();
oSearcher = new TeamSearcher();
oLogin.Visibility = Visibility;
this.LayoutRoot.Children.Add(oLogin);
}
Also a method is added to show/hide the views...this could/will probably get more advanced/robust with animations etc...but this shows the basic idea:
public void ShowTeamSearcher(Guid ctxId, Guid teamId)
{
oSearcher.UserTeamId = teamId;
oSearcher.UserContextId = ctxId;
LayoutRoot.Children.Remove(oLogin);
LayoutRoot.Children.Add(oSearcher);
}
Then to invoke this in the JavaScript after assigning the id of oXaml to the instance of the silverlight host.
var slControl = document.getElementById('oXaml');
slControl.Content.App.ShowTeamSearch(sessionId, teamId);
This seems to work and isn't all that bad of a solution, but there might be something better...thoughts?
Here is a my collections of my links for this subject.
Javascript communication to
Silverlight 2.0
Silverlight
interoperability
Silverlight
and JavaScript Interop Basics

Resources