Loading a .dll file into silverlight (hosted with IIS7) - silverlight

I seem to have a problem loading .dll files into my silverlight application.
this.pluginDownloader = new WebClient();
this.pluginDownloader.OpenReadCompleted += new OpenReadCompletedEventHandler(pluginDownloader_OpenReadCompleted);
this.pluginDownloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(pluginDownloader_DownloadProgressChanged);
String path = String.Format("http://localhost/Resources/Plugins/{0}.dll", this.pluginDetails.AssemblyName);
this.pluginDownloader.OpenReadAsync(new Uri(path, UriKind.Relative));
I can manually download the .dll assembly file by navigating to it (eg: http://localhost/Resources/Plugins/myAssembly.dll) but it just stalls the silverlight app when the above code is executed. This works when I launch the project in visual studio so it must be some setting in IIS7.
Anyone know how to fix this?

Thanks for answering Jeff... I tried relative paths also but that didn't work either. I thought an absolute URL would be the best thing to try as I can actually navigate and download the assembly file using the localhost link.
EDIT:
Got it working - if i replace localhost with the actual IP address of the server it works find
String path = String.Format("http://192.168.2.107/code/Platform/Website/Resources/Plugins/{0}.dll", this.pluginDetails.AssemblyName);
this.pluginDownloader.OpenReadAsync(new Uri(path, UriKind.Absolute));
I'd still like to know how to get it working using a relative path...
The relative path should work - i'm using a one in the same class to load an image
this.pluginImage.Source = new BitmapImage(new Uri("/Resources/Plugins/PluginTile/" + this.pluginDetails.ImageFilename, UriKind.Relative));
Strange...

Related

Reading a xap file from webclient

I have hosted my silverlight service on the server. I have created a client application on my local machine referencing the silverlight service on the server. Everything compiles and work fine.
Now I coped the xap file into another silverlight application and tried to read the xap using the following code.
StreamResourceInfo _streamResourceInfo = new StreamResourceInfo(e.Result, null);
StreamResourceInfo _streamResourceInfoDll = Application.GetResourceStream(_streamResourceInfo,
new Uri(string.Format("{0}.dll", abc), UriKind.Relative));
AssemblyPart _assemblyPart = new AssemblyPart();
Assembly _assembly = _assemblyPart.Load(_streamResourceInfoDll.Stream);
UserControl _userControl = _assembly.CreateInstance(string.Format("{0}.MainPage", abc)) as UserControl;
this.Main_Canvas.Children.Add(_userControl);
Everything works fine. Now I added Navigation Framework to my application.
I modified the main page with navigation framework. In my main page I have
<navigation:Frame x:Name="ContentFrame" Source="/Views/abc.xaml" Grid.Row="1" >
When I Copy the modified xap on to the other application. It complains No xaml is found at the location /Views/abc.xaml.
Any help will be highly appreciated.
This answer is pretty comprehensive
But... if dynamic XAP loading is your thing, do yourself a favour and take a look at Jounce - It's an MVVM framework, but that doesn't mean you absolutely must use ViewModels.

Changing Silverlight Application name dynamically

I'm creating a deployable module where some parts are written in Silverlight, and I'm making the SL application deployable for OOB usage. However, I want to make Silverlight take the name of the website that it's deployed from, such as, when a user installs it from Example.com, I want to have "example.com application" with the site's own icon in the shortcut. Is there any "supported" method of doing this, or will I be going with locating the XAP file and manually changing AppManifest.xaml inside it?
You will need to find out your URL of your application:
string appURL = Application.Current.Host.Source.AbsoluteUri.Substring(0, Application.Current.Host.Source.AbsoluteUri.IndexOf(#"ClientBin/"));
So this will solve the title problem, next is the icon. You could load the image from the page:
Uri uri = new Uri(String.Format("{0}/favicon.png", appURL));
IconImage.Source = new BitmapImage(uri);
It's not perfect, you will have to manipulate appURL to get the domain name only.

Pointing to an image on Server within XAML

I can do this within code to load a bitmapimage located on the Server under ClientBin\Images folder:
var image = new BitmapImage(new Uri(Application.Current.Host.Source, "./Images/Default.JPG"));
However how do I do this within XAML itself? Is it even possible?
<Image x:Name="NewImage" Source="../Images/Default.JPG"/>
Many Thanks,
Did you try this:-
<Image x:Name="NewImage" Source="/Images/Default.JPG"/>
Urls in Xaml treat the folder from which the Xap is downloaded (ClientBin usually) as the root, i.e. the path "/" points actually at ClientBin.
You can't use a relative path in silverlight for an image on the server since the xap file is downloaded to the client, so the application is not actually running on the server.
you will have to use the full "http://mysite.com/myImage.jpg" path

Silverlight Asp.Net project integration

I added a Silverlight application to my ASP.NET website. Visual Studio made a new silverlight project and added its xap to the ClientBin folder under the project of my website. So both the projects are under one solution.
My Silverlight app is supposed to read an xml file and I was unable to make it access the file from the client bin folder under the website project. Adding a reference to that project does not work since it says only references to other silverlight applications can be added. Right now its working when the file is under the silverlight project but not when it is under the website project.
how can I make it read the file from website project?
The project structure is
WEBSITE1 (solution)
-WEBSITE1 (project)
-ClientBin
-file0.xml
-silverlightchart.xap
-SilverlightChart
-file1.xml
I can access file1.xml using
XDocument document = XDocument.Load("file1.xml");
I want to access file0.xml but no path works for me, for e.g,
XDocument document = XDocument.Load("~/ClientBin/file0.xml");
and WEBSITE1 is the startup project
You should be able to read a file from the ClientBin folder simply enough without needing to do anything special. At a guess I would say the you have accidentally set the Silverlight application as the startup project. In this scenario you want the website to be the startup project then either have the Silverlight apps test page marked as the start page or to navigate to the silverlight page once the browser has started.
Edit
The problem you have is that the Load method is synchronous but Silverlight does not support synchronous access to web resources. Hence passing a Uri to the Load method will only work if the that Uri can be fulfilled by content in the Xap. Thats why an Xml file in the silverlight project works because it ends up in the Xap.
To fetch Xml from the site you need to do this:-
WebClient client = new WebClient();
client.DownloadStringCompleted += (s, args) =>
{
XDocument document = XDocument.Load(args.result);
SomeFunctionToContinueWithDocumentProcess(document);
}
client.DownloadStringAsync(new Uri("file0.xml", UriKind.Relative);
// code exits here but _document won't be loaded yet

Opening an image on demand with Silverlight 2 WebClient

I'm trying to show some images on my silverlight application whenever user wants to. The images are in a folder in my silverlight project and I don't want the user to download all of them when he/she loads the web page for the first time.
I have tried the OpenReadAsync method with a relative address to the image file which is in a folder named images and its Build Action is set to Content and also its "Copy to Output Direcoty" property is set to Always.
But I get the following exception in the OpenReadCompleted event:
The URI prefix is not recognized.
Here is the code I used:
Dim webClient As New WebClient
AddHandler webClient.OpenReadCompleted, AddressOf webClient_OpenReadCompleted
WebClient.AllowReadStreamBuffering = True
WebClient.OpenReadAsync(New Uri("images/myimage.jpg", UriKind.Relative))
Can anyone tell me how can I solve this problem?
Thanks
To start with take the images out of the silverlight project. You want the images to be in the web project then you can use a normal image tag with an empty source, then when you need to download the image set the source to the uri.

Resources