Hi Ive been pulling my hair out over this for the last couple of days, I have a mediaelement in a Silverlight App written in C#
Im trying to stream an MP3 over http and play it through the silverlight app. Although it keeps catching an exception that says
AG_E_NETWORK_ERROR
The mp3 will not play and the MediaElement.MediaOpened event never fires
After searching on the net it appears this error is due to a curropt source but the mp3 plays fine when I point a webbrowser at the mp3.
heres the code
private void button1_Click(object sender, RoutedEventArgs e)
{
Uri source = new Uri("http://www.sm-testing.co.uk/mixes/youdontknow.mp3");
mediaElement1.Source = source;
mediaElement1.MediaFailed += new EventHandler<ExceptionRoutedEventArgs>(mediaElement1_MediaFailed);
mediaElement1.MediaOpened +=new RoutedEventHandler(mediaElement1_MediaOpened);
}
void mediaElement1_MediaFailed(object sender, ExceptionRoutedEventArgs e)
{
MessageBox.Show( e.ErrorException.ToString());
}
private void mediaElement1_MediaOpened(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
I see you have used an actual URL in your example. I checked your site and there is no http://www.sm-testing.co.uk/ClientAccessPolicy.xml file or http://www.sm-testing.co.uk/crossdomain.xml file.
Without either of those files (preferably ClientAccessPolicy.xml as the other is an old Flash compatibility format and missing some features) Silverlight will only retrieve files from the domain it is hosted on. This is a security feature to stop Silverlight app taking data or images from just anywhere without permission.
Example ClientAccessPolicy.xml below allows any access to any http or https request:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="http://*" />
<domain uri="https://*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
You don't need a crossdomain.xml as well unless you want to open up access to Flash apps too :)
Related
I have made a silverlight application for drag and drop uploading. Now, this application works fine when I deploy it from local server. But whenever I deploy it from QA server (machine on the same LAN but has a global ip associated with it), it does nothing. It just shows a white background screen (default parameter). There is no error that it couldn't find xap file or such, neither any other console errors as well.
I can't seem to figure out what the error is for.
As suggested by some other blogs and posts, I have tried to change the extension from xap to DLL, but still same thing.
Please help.
To access a silverlight app on a remote server, you need to put on the root folder of this server a ClientAccessPolicy.xml file.
A basic one, allowing you to use it from any domain:
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from>
<Domain uri="*"/>
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
More details here:
http://msdn.microsoft.com/fr-fr/library/cc197955%28v=vs.95%29.aspx
I use Controls from Silverlight SDK and Silverlight Toolkit and I'm curious if the client has to install the packages as well?
How do I build such an application correctly to deploy it afterwards?
EDIT:
I just ask because I'm unable to get my application to run on a client running a bare SL4 runtime. It just throws the following exception:
Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.
bei System.Windows.Application.Application_Starting(Object sender, StartupEventArgs e)
bei MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
bei MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
I use the VS2010 devserver to deploy the application to the client.
The app makes use of a WCF Service I also serve by VS2010 for now, it also contains a "clientaccesspolicy.xml":
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
Thanks in advance.
You don't need to install those packages on the client side,
it's will be part of your xap file,
and about the server running your Silverlight application,
Silverlight is a client technology and can be hosted on any web server. It can be any type of web server that is capable of delivering the XAP file along with the correct content MIME type from the server (application/x-silverlight-app).
As long as it does that, we don’t care what type of web server it is delivering the XAP file.
you will think: "This problem has been solved many, many times. So why doesn't he use Google?" Please, believe me, I tried everything. I'm dealing with this problem since last week. I read many blogs and searched the MSDN. But I don't get it.
So here is the problem. There is one Silverlight 4 app and a WCF DataService, both are running on localhost.
This is the code. Nothing special I guess.
private void InitializeData()
{
var query = (from item in ObjCtx.TestTgt
select item) as DataServiceQuery<TestTgt>;
Debug.Assert(query != null, "'query' is null");
query.BeginExecute(OnLoadDataFinished, query);
}
private void OnLoadDataFinished(IAsyncResult ar)
{
try
{
var query = ar.AsyncState as DataServiceQuery<TestTgt>;
Debug.Assert(query != null, "'query' is null");
var res = query.EndExecute(ar).ToList();
Data.Data = new ObservableCollection<TestTgt>(res);
}
catch(Exception ex)
{
Data.StateDescription = String.Format("Exception occured.{0}{0}{1}", Environment.NewLine, AgExMsgFormatter.GetExText(ex));
}
}
In OnLoadData at this line: var res = query.EndExecute(ar).ToList(); the following exception occures.
Exception occured.
[EXCEPTION]
[TYPE:] 'InvalidOperationException'
[MESSAGE:] 'An error occurred while processing this request.'
[CALLSTACK:]
at System.Data.Services.Client.BaseAsyncResult.EndExecute[T](Object source, String method, IAsyncResult asyncResult)
at System.Data.Services.Client.QueryResult.EndExecute[TElement](Object source, IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceRequest.EndExecute[TElement](Object source, DataServiceContext context, IAsyncResult asyncResult)
at System.Data.Services.Client.DataServiceQuery`1.EndExecute(IAsyncResult asyncResult)
at SimpleGrid.SimpleGridVm.OnLoadDataFinished(IAsyncResult ar)
[INNEREXCEPTION]
[TYPE:] 'SecurityException'
[MESSAGE:] ''
[CALLSTACK:]
at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Http.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)
[INNEREXCEPTION]
[TYPE:] 'SecurityException'
[MESSAGE:] 'Security error.'
[CALLSTACK:]
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass2.b__0(Object sendState)
[/INNEREXCEPTION]
[/INNEREXCEPTION]
[/EXCEPTION]
As authentication mode I want to use Windows. Both are configured for this in IIS. The web service is running and is delivering the correct data.
So what am I missing? I thought that this should work. Any help would be appreciated.
Regards
your issue looks like a cross domain error: A silverlight app is not allowed to perform cross domain webservice calls by default. It means that if your cassini hosted SL app (domain localhost:4314 for example) attempts to access a WCF service on the domain localhost, the call will fail with a security exception.
You can easily catch the issue by using fiddler or the network tab of firebug (on firefox): The app first attempt to access a file named "clientaccesspolicy.xml" on the webservice domain root. This file defines a policy on the WCF server which authorize cross domain calls on the server.
Here is an exemple of crossdomain policy file, which allow any SL apps to access any webservices on this domain:
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
<policy >
<allow-from http-methods="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
However You can be a little more specific. More information is available on MSDN:
Hmm, ok that's suprising but I got it. The problem is not the authentication or something like this. It's the debugger. The web service is not hosted on the web site of the Silverlight application. So the web service calls are failing.
When loading the deployed site in a browser everything works absolutly fine. For me, it appears somekind of strange, but perhaps someone can give me a good explanation for this behavior.
But this brings up another problem. How do I debug a Silverlight application, when it references an external web service. Perhaps I will open a seperate thread for this.
Regards
Here my solution:
The RIA service runs, exactly as you said, on localhost.
The error comes when the Silverlight project is the statup project.
The RIA service is called in that case in the context of a different
web page.
Visual Studio Debugger warns about that, but the popup is often
checked to "Don't show".
The solution is:
Set the the Web Project in the solution as startup project.
Set the generated, TestPage.html as start page.
And the RIA Service works.
You probarly need to start Visual Studio as Administrator
I am having a heck of a time calling a RESTful service from within silverlight. I am encountering this error:
{System.Security.SecurityException ---> System.Security.SecurityException: Security error.
at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.<EndGetResponse>b__4(Object sendState)
at System.Net.AsyncHelper.<>c__DisplayClass2.<BeginOnUI>b__0(Object sendState)
--- End of inner exception stack trace ---
at System.Net.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state)
at System.Net.BrowserHttpWebRequest.EndGetResponse(IAsyncResult asyncResult)
at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result)
at System.Net.WebClient.OpenReadAsyncCallback(IAsyncResult result)}
Which seems to be a popular error when using the webclient. I have put in place a clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*" />
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true" />
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
and I have watched the silverlight in fiddler and it does make a request to the web site and does get a 200 status back.
public void login(string userName, string password)
{
WebClient client = new WebClient();
Uri uri = new Uri(serverURI + "/clientaccesspolicy.xml");
client.OpenReadCompleted += new OpenReadCompletedEventHandler(login_Complete);
client.OpenReadAsync(uri);
}
private void login_Complete(object sender, OpenReadCompletedEventArgs e)
{
byte[] buffer = new byte[e.Result.Length]; //crashes here with exception
...
}
I am more or less out of ideas. Anybody know what I'm doing wrong? Is there some issue with running the silverlight directly from a file:// uri?
Update: I deleted the clientaccesspolicy.xml file and kept just the crossdomain.xml file in place and bingo everything worked. That makes me believe that the error is in the clientaccesspolicy file but I copied that directly from microsoft. What gives?
I just spent 3 hours looking into this very issue. The cross domain access policy and client access policy files were a dead-end for me. Nothing would work. Then finally I ran into a post on the Silverlight.net forums by a Microsoft employee that helped me fix the issue.
The answer, at least in my case was the test webpage that Visual Studio generates when you create a new Silverlight application.
Basically you get two options when you start a Silverlight project. The first option will generate an html page dynamically when you run your application. The second option will create a separate ASP.NET project that will host your Silverlight application. If you choose the first option (dynamic test page) you will not be able to do any cross domain requests, even if both your projects are on the same box it will somehow consider this a cross domain call and fail (I am not sure why)
Create another Silverlight project, choose the second option, and move your XAML files in. That should fix your issue.
You can't request content from a website if your Silverlight application is running from a file:// URL.
For more information, see URL Access Restrictions in Silverlight.
Mmm...
Try with this cross domain file
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="SOAPAction" >
<domain uri="*"/>
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
X-Cubed is correct, you cannot do cross-domain requests from file://, as Adam Berent pointed out, this means if you use the TestPage generated by visual studio your network requests will fail.
A workaround is to launch the TestPage using Chiron (usually used for dynamic languages) to serve it (because then the access is on http://) or off a development web server.
The catch is you actually have to attach the debugger manually to the browser in order to debug with networking (you can't just hit F5.)
I've seen a lot of links to MSDN and "works on my machine!" answers so I'd like to ask my question with the exact steps to duplicate what I'm doing. Because we are using an already existing webservice, I'm asking with the context of having a webservice hosted outside of my project, unlike many of the tutorials and videos online. So here goes:
*** Create a new ASP.NET webservice project.
It will come with an existing Service.asmx file exposing a "HelloWorld" web method.
View in browser, hit the "Invoke" button. It should work returning the "Hello World" string.
On my machine, the URL is: "http://localhost:15511/WebSite5/Service.asmx"
*** Start a new instance of Visual Studio, create a Silverlight Web Application Project.
*** Stick a single button on there with an event handler to call the web service. I personally nuke the Grid and use a simple StackPanel. eg.
<UserControl x:Class="SilverlightApplication1.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="400" Height="300">
<StackPanel>
<Button Click="Button_Click">
<Button.Content>
<TextBlock Text="Test"/>
</Button.Content>
</Button>
</StackPanel>
</UserControl>
Add the web reference, using statement and event handler for the Button_Click:
private void Button_Click(object sender, RoutedEventArgs e)
{
ServiceSoapClient client = new ServiceSoapClient();
client.HelloWorldCompleted += (object s, HelloWorldCompletedEventArgs ea) => {
MessageBox.Show(ea.Result);
};
client.HelloWorldAsync();
}
Run the Silverlight Application. In my case I'm going to my Silverlight Test Page at:
http://localhost:15558/SilverlightApplication1TestPage.aspx
Run and of course it blows up because of crossdomain issues. So next add the clientaccesspolicy.xml file with the following to the root of your web application hosting the service:
<?xml version="1.0" encoding="utf-8" ?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource include-subpaths="true" path="/"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
This should open things up since it's got a wildcard for headers, uris, and resources, right?
Run again and you get an error:
An error occurred while trying to make a request to URI 'http://localhost:15511/WebSite5/Service.asmx'. This could be due to attempting to access a service in a cross-domain way without a proper cross-domain policy in place, or a policy that is unsuitable for SOAP services. You may need to contact the owner of the service to publish a cross-domain policy file and to ensure it allows SOAP-related HTTP headers to be sent.
So question: is there a secret to the clientaccesspolicy file? One could alternately try with the crossdomain.xml but it gives a similar result.
I've encountered this problem (SL v5.0 and Visual Studio 2010), what fixed it for me is that I went into the Silverlight project properties >> Silverlight tab and selected "Require elevated trust when running in-browser"
I've had this same problem a couple of times. In the past I've solved this by using the Web App as start up, but it looks like you've already done that.
My post on the subject: http://www.donnfelker.com/silverlight-cross-domain-issue/
Make sure that you put the clientaccesspolicy.xml file in the root of IIS web directory e.g.
C:\Inetpub\wwwroot\clientaccesspolicy.xml
This will make sure that it is accessible directly at http:///clientaccesspolicy.xml
I was getting the same error and I resolved it doing the above steps.
Client Configuration
Maybe your ServiceReferences.ClientConfig for your Silverlight client is pointing to the wrong URL?
Also, check the location of your cross-domain policy file. This MSDN article has more information.
Locally Running Silverlight
Additional note for running Silverlight locally (Vista Sidebar, for example). As reported in this blog entry, "Silverlight cannot use any network provider, when running locally." The workaround is to use javascript to interface to the web service in this situation.
Something that worked for me began with what I found on the silverlight forums here. It essentially asked if I could even get to my clientaccesspolicy.xml or crossdomain.xml from localhost (http://localhost/clientaccesspolicy.xml). When I tried to navigate there, I couldn't so I simply found the code for both of them (also within the aforementioned thread), and copy-pasted over the code inside of those files existing in my inetpub\wwwroot\ directory (I opened them up using Notepad++). The weird part was the code didn't change at all , and yet, it works! Hope that helps someone! This was extremely strange.
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?>
<access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
crossdomain.xml
<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
<cross-domain-policy>
<allow-http-request-headers-from domain="*" headers="*"/>
</cross-domain-policy>
Be blessed!
-sf
Have you tried fiddler when using this through IE, you might be able to see the traffic silverlight is causing, such as which cross policy files it is looking for?
I modified my Internet explorer settings. The website runs on a intern webserver so I added it in the Iexplorer Trusted Sites list. (Tools->Internet Options->Security->Sites).
Then I changed the security level and enabled cross domains. Done, Works but took me while to find the solution.
Best,
Jeppen
You have to be aware of situation that when you reference to your service inside your project there 'll be created file "Reference.ClienConfig" and there is:
**
<endpoint address="http://localhost:57675/Servis.asmx" binding="basicHttpBinding"
bindingConfiguration="ServisSoap" contract="ServiceReference1.ServisSoap"
name="ServisSoap" />
**
Make sure that your page is still using the same port (for example here is 57675). By default your localhost 'll get random port, so you have to change it to be static number and not Dynamic. (Right click on asp.net project/Tab Web/Specific port /type number
Hope it helps
I ran into something like this and adding a ServiceHostFactory fixed my issue. The cross-domain policy file alone did not fix it.
class MyHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
MyHost customServiceHost =
new MyHost(serviceType, new Uri("[Your URL goes here]",UriKind.Absolute));
return customServiceHost;
}
}
class MyHost : ServiceHost
{
public MyHost(Type serviceType, params Uri[] baseAddresses) base(serviceType, baseAddresses)
{ }
protected override void ApplyConfiguration()
{
base.ApplyConfiguration();
}
}
You also have to add Factory="MyHostFactory" in the tag that defines your service
The problem could be that your development server is not able to serve the xml file, try this - explicitly make it available through WebGet
[ServiceContract]
public interface ICrossDomainService
{
[OperationContract]
[WebGet(UriTemplate = "ClientAccessPolicy.xml")]
Message ProvidePolicyFile();
}
and then the ProvidePolicyFile() can be
public System.ServiceModel.Channels.Message ProvidePolicyFile()
{
FileStream filestream = File.Open(#"ClientAcessPolicy.xml", FileMode.Open);
// Either specify ClientAcessPolicy.xml file path properly
// or put that in \Bin folder of the console application
XmlReader reader = XmlReader.Create(filestream);
System.ServiceModel.Channels.Message result = Message.CreateMessage(MessageVersion.None, "", reader);
return result;
}
It worked in my pc successfully, you can place your
clientaccesspolicy.xml
<?xml version="1.0" encoding="utf-8"?> <access-policy>
<cross-domain-access>
<policy>
<allow-from http-request-headers="*">
<domain uri="*"/>
</allow-from>
<grant-to>
<resource path="/" include-subpaths="true"/>
</grant-to>
</policy>
</cross-domain-access>
</access-policy>
both your project directory and your webservices root.
I was facing the same problem and take more than 3 days to figure out the Problem. I notice also when I was calling the internet Cloud Service WCF from a Silverlight app hosted in another web server it just shows Cross-Domain Errors.
After looking into some posts I didn't solve the problem, even putting cross-domain.xml and clientaccesspolice.xml files in service root directory.
So I just try to instead of using http://example.com I just change it to secure https://example.com and it just worked fine. The cross-domain errors disappears. service was called without problems.
Check which project is set default.
It should be the web project that should be set default and not the silverlight project.