• Silverlight cross domain services and a helpful tool

    I’ve been getting a few notes on issues relating to people trying Silverlight beta 2 and WCF or other services.  The most common issue I’m seeing reported is “my exception is showing a 404-not found error message, but the service is there and works!”

    Okay, there could be several things happening here, but let’s tackle the “make sure it is plugged in” type situations.  I don’t mean to make light of the error, because at first I, too, was banging my head against a wall.  Sometimes it helps to have a second set of eyes or a deeper understanding of the issue…or both.

    First, the situation.  Most of the time you’ll see this exception when your Silverlight application is accessing a service not hosted on the same application domain.  This is considered cross-domain access and requires the service host to enable an opt-in policy file so rich client platforms are allowed to access the service.  In Silverlight we call that the clientaccesspolicy.xml file.  You can learn all about cross-domain policy files by viewing this video on the Silverlight community site (a great resource).  In beta 2, there was a subtle change to the policy file that is required.  I wrote about that here as well (and note the code download for the video has the updated policy file).

    Ok, so under what conditions might you get the “(404) Not found” error message when accessing services?

    No policy file at all

    Silverlight will first check for clientaccesspolicy.xml first and then fallback and see if a supported crossdomain.xml file exists.  If neither exists at all, 404 baby.  Also remember Silverlight is looking for this file in the root of the requesting domain.  So if you have a file but it is in your app root…this could be the issue at all.

    Incorrect, mail-formed, just plain wrong policy file

    Silverlight will check for a clientaccesspolicy.xml file and if it finds one but it has an incorrect format or is mal-formed it will treat it as invalid and then look for crossdomain.xml…and if not found, boom: 404.  This is what most are running into in starting to use beta 2 with your policy files.  The missing http-request-headers attribute renders the file mal-formed.

    HTML response

    Most sites have custom error messages for page not found.  For example, when you visit google.com/timheuer you’ll get a less-than-helpful message, but custom nonetheless or as another example microsoft.com/timheuer you’ll get another custom response with a sitemap.  Both of these are essentially custom error messages that are returning valid HTML, but not a valid policy file.  In these instances, Silverlight sees the response, but sees it as invalid/mal-formed and treats it like it didn’t exist: 404.

    These are the most common instances where a 404 would be generated and making you bang your head against the closest semi-hard surface.  How can you figure out what is going on?  Well first, make sure you do your best to ensure you meet all the requirements.  But also use some development helper tools.

    Web Development Helper

    For me, in service/remote/AJAX development there is a single indispensable tool that I can’t live without.  That is NikhilK’s Web Development Helper.  This tool is a plugin to Internet Explorer (yes I know there are others similar in nature for Firefox, etc. – but I LOVE this implementation and IE is fine by me) that provides in the browser HTTP-traffic sniffing.  No need for any funky port configuration or changing proxy server settings, etc.  Just enable it and it works.  I highly recommend you use this tool or something similar like it (Fiddler is another good one although requires some additional config steps usually when working with Visual Studio’s web development server).

    Seriously, a tool like this will save you so much time in troubleshooting your service interactions with Silverlight, Flash, AJAX, whatever – it will help you immediately figure out where to start looking rather than grabbing your climbing gear and spelunking in unknown caverns.

    So why a ‘404’ – what gives?

    I’ve also heard people say “you need to make that exception be more descriptive, 404 is not accurate.”  I’m on the fence on this one.  As a .NET developer I can see where the concerns are coming from in having the most descriptive exception possible.  But one must realize what is happening under the hood.  The polciy file is being requested as a GET request, so basically an HttpWebRequest object is our object here.  Because of this, we return HTTP-specific errors.  There isn’t one for “Silverlight policy file found, but not correct” in the HTTP spec right now.  So because of this, we use a RESTful approach in providing a standard HTTP response.  In our case “404-not found” seems to be a valid response – indicating “The request for a valid policy file resulted in a valid policy file not being found.”  We make no distinction between partially valid or finding a specific typo, etc. – we simply indicate that a valid policy wasn’t found.  One could argue 406,409 or 417 might be other responses, but I’m not sure that would make anyone feel any better – we’re still going to use an HTTP response code.

    Hope this helps!


    This work is licensed under a Creative Commons Attribution By license.

    Tuesday, June 10, 2008 12:18 PM

Comments

  • Michael said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Hi Tim,

    I'm getting a 404 error since beta 2 (in beta 1 everything worked perfect). I tried a HTTP sniffing tool (Firebug in Firfox) as you recommended. But in this trace is something I do not understand. The first http request is for the xap file, which returns with 200. The next request then is for the http://soap.amazon.com/clientaccesspolicy.xml (I'm using the Amazon Webservice) which returns 404 because it is not there. Now I do expect another request for crossdomain.xml (which exists for the Amazon Webservice), but the application throws the 404 exception before looking for this file!

    6/18/2008 7:05 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    @Michael: adobe has recently changed their format as well which might be causing some of the issues -- they've implemented similar header changes in the crossdomain.xml file that we understand in SL2, but some sites have not implemented yet.

    6/18/2008 10:39 AM
  • Shaun said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Nice post. I am having a 404 issue, and was able to see exactly where/why it was messing up with "Web Development Helper".

    Unfortunately I still can't figure out how to fix it.

    My service is located at on my local intranet server at "http://ssisvr/ElmerService/ElmerService.asmx" Navigating to this URL shows the service and everything works fine.

    My Silverlight project has a service reference with the endpoint address properly set to the service:

    <endpoint address="http://ssisvr/ElmerService/ElmerService.asmx" ...

    When I run the project out of the debugger, everything works fine - no 404, beautiful.

    When I publish the application to the server however (http://ssisvr/Elmer/), things mess up. The page and silverlight app loads fine, but when I push the button to invoke the service I see this behavior in Web Development Helper:

    ..... <multiple status 200 requests from http://ssisvr/Elmer/...>
    http://ssisvr/elmer/ClientBin/ElmerFudd.xap - 200

    At this point the silverlight aplication is up and running just fine, then I hit the button to invoke the service:

    http://localhost/clientaccesspolicy.xml - 404
    http://localhost/crossdomain.xml - 404

    I have no clue why the silverlight application is looking at "localhost" for these files. The words "localhost" do not even appear in either of my projects, anywhere - including in hidden text. I have also tried manually setting the endpoint for the service:

    BasicHttpBinding binding = new BasicHttpBinding();
    EndpointAddress ep = new EndpointAddress("http://ssisvr/ElmerService/ElmerService.asmx");
    //client = new ElmerServiceSoapClient("ElmerServiceSoap");
    client = new ElmerServiceSoapClient(binding, ep);

    Yet invoking the service function still checks "localhost" for those files, and of course returns a 404.

    It is driving me insane! Any ideas?



    6/23/2008 1:23 PM
  • brian said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    I think it would make more sense to throw a silverlight specific exception and place the 404 in the inner exception.

    Since the 404 is not in response to the resource that is being requested.

    6/24/2008 10:49 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    @brian: in this regard we are limited to the information provided by the browser's networking stack. we don't get a ton of information. for example that is why SOAP faults return a 404 instead of a 500 or more-specific information -- we simply aren't getting detail from the browser stack.

    6/24/2008 12:11 PM
  • Jody said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Watching the http traffic was helpful, but still not sure what my problem is. The silverlight app works fine querying the service from IE, I see the accesses to the clientaccesspolicy.xml file. However when i run the same silverlight app from the same URL as IE, I get the 404 error. The http sniffing doesn't show any attempt to access the clientaccesspolicy.xml, it stops after getting the xap.

    7/25/2008 10:56 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Jody: if you can send me a repro i can help troubleshoot.

    7/25/2008 12:03 PM
  • Terry said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Your video and suggestions are clear and helpful. Thanks. Now I can run a WCF service from a Silverlight/WPF app using the VS Development Server.

    However, it looks like I encoutered an issue in IIS 5.1 with Win XP. I created a new solution, in VS2008 + Silverlight Beta 2, used IIS option in the WCF service project's properties>Web tab, and pressed the Create Virtual Directory button. Then in a client, say WPF or Silverlight client, I did a Add Service Reference. In the Address field, I pressed the Discover button, the Address field showed http://localhost/WcfServcie1/Service1.svc (instead of http://localhost:9033/Service1.svc). Once I click on the service, the error is:

    There was an error downloading 'http://localhost/WcfService1/Service1.svc'.
    Metadata contains a reference that cannot be resolved: 'http://localhost/WcfService1/Service1.svc'.
    The requested service, 'http://localhost/WcfService1/Service1.svc' could not be activated. See the server's diagnostic trace logs for more information.
    If the service is defined in the current solution, try building the solution and adding the service reference again.


    So I tried the IE browser by typing http://localhost/WcfService1/Service1.svc, it displayed Server Error in /WcfService1 Application with the error below:

    Server Error in '/WcfService1' Application.

    Exception Details: System.InvalidOperationException: Service 'WcfService1.Service1' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.

    ...

    Stack Trace:
    [InvalidOperationException: Service 'WcfService1.Service1' has zero application (non-infrastructure) endpoints. This might be because no configuration file was found for your application, or because no service element matching the service name could be found in the configuration file, or because no endpoints were defined in the service element.]
    System.ServiceModel.Description.DispatcherBuilder.EnsureThereAreNonMexEndpoints(ServiceDescription description) +259
    System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost) +64
    System.ServiceModel.ServiceHostBase.InitializeRuntime() +37
    System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout) +63
    System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout) +268
    System.ServiceModel.Channels.CommunicationObject.Open() +30
    System.ServiceModel.HostingManager.ActivateService(String normalizedVirtualPath) +104
    System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath) +498

    I suspected it may be my IIS setup. So I ran "ServiceModelreg.exe /lv",
    it shows my System.serviceModel is 3.0.0.0, same for system.servicemodel.activation and system.runtime.serialization, system.web etc. Do I need to see 3.5 in ServiceModelreg? Or some extra settings in the IIS virtual directory? Your pointer is very much appreciated. Thanks for reading.

    7/29/2008 1:37 AM
  • Terry said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Thanks for reading my last long description. This has nothing to do with the policy file. I finally fixed the problem. I don't know the exact cause. I un-installed Silverlight Tool Beta 2, re-installed IIS, install the Silverlight Tool again, then somehow I added the .svc in the Application Mappings of IIS to point to aspnet_isapi.dll, then even did aspnet_iisreg.exe -i. Afterall, I think there could be due to a potential setup issue with the ASP.NET 2.0 setting in IIS in my SOE laptop.

    7/30/2008 9:04 PM
  • Dimitry said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Hi,
    I also have cross domain access problem. I found same problem on the forum http://silverlight.net/forums/p/16839/57457.aspx. Answer contains good example. But example does not work :(.
    I ran this example and see in the Web Developer Helper:
    http://localhost:50467/TestCalculatorWeb/TestCalculatorTestPage.html - status 200
    http://localhost:50467/TestCalculatorWeb/ClientBin/TestCalculator.xap - status 200
    http://localhost:7999/clientaccesspolicy.xml - status 200

    But VS debugger returns "The remote server returned an unexpected response: (404) Not Found."
    I enabled WCF server debugging. And looked logs of service. It does not contain any requests except clientaccesspolicy.xml

    Can you help?

    8/8/2008 1:04 PM
  • Tom Winans said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Can you see http traffic with Web Development Helper using WFC?

    9/20/2008 7:17 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Tom: If by WFC you mean WCF-windows communication foundation, then yes.

    9/20/2008 7:32 AM
  • Gravatar
    # re: Silverlight cross domain services and a helpful tool


    I had a Silverlight app consuming a webservice in beta 2 on windows 2003 that worked just fine (and still works fine when running in beta 2). When I upgraded to the release of Silverlight 2 (running on XP professional) the call to the service stopped working, the error was "The remote server returned an error: NotFound". I can see my webservice being called on the server, and Web Developer Helper shows that the response code is 200 and the response contains the data I expect. However, as I said, I still get the server not found error in Silverlight.

    Any ideas on what might be the root of the problem. Could it be xp vs 2003, ie6 vs ie7, something different in the latest version of Silverlight.

    Thanks,

    Dave

    10/16/2008 1:02 PM
  • Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Hi Tim,

    Like Dave in the previous comment I'm experiencing the exact same problem with a personal project.
    I have a SL enabled WCF service with 4 methods in them. With 1 method I keep getting the "The remote server returned an error: NotFound" message. I totally stripped the service renamed it, created a new WCF service with the same methods, added a clientaccespolicy.xml file but nothing helped.
    I used Nikhils toolbar to log the http requests, but it looks like the request never leaves the browser. At least it doesn't show up in the toolbar probably because SL crashes before it can update.
    I haven't been able to solve it yet (been working on it for a number of hours already...).

    If you want to, I can send you my project with a description of how to reproduce the error. Just drop me a message if you need it.

    Rob

    10/25/2008 5:59 AM
  • Otpi said:
    Gravatar
    # Please Help: Silverlight cross domain services and a helpful tool


    Hi,

    I have been reading this very closely and really appreciate the effort on this important topic.

    With Silverlight 2.0, I am trying to access a WCF Service hosted as a Console App. The error thrown at the Silverlight project(inside the generated proxy) clearly states that I might be missing the clientaccesspolicy.xml.

    As soon as I launch the Silverlight website, I see that VS Development server kicks in (like http://localhost:8989). And I am hosting WCF on say http://localhost:9876.

    Can guys please let me know what to do in this development scenario?

    Where to place the cdomain policy file.

    Thanks





    11/11/2008 10:55 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Otpi: See blogs.msdn.com/.../...elf-hosted-web-services.aspx

    11/11/2008 2:53 PM
  • Rakesh said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    When we tried to call a webservice from silverlight 2.0, we are getting the following error

    The remote server returned an error: NotFound

    System.Net.WebException: The remote server returned an error: NotFound ---> System.Net.WebException: The remote server returned an error: NotFound

    at System.Net.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)

    at System.Net.BrowserHttpWebRequest.<>c__DisplayClass5.b__4(Object sendState)

    at System.Net.AsyncHelper.<>c__DisplayClass2.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.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelAsyncRequest.CompleteGetResponse(IAsyncResult result)

    Thanks

    11/13/2008 6:18 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Rakesh: have you looked at the traffic to see what is failing and any particular errors in the response content?

    11/13/2008 8:11 AM
  • Rakesh said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Hi timheuer,
    We tried with Web development helper tool and the status shown in 200 and the response content contains result from XML also. This error is coming only in one or two systems and only in IE. FireFox it is working fine.

    Thanks

    11/17/2008 3:32 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Rakesh: Is this a service that I could look at? Also are the problematic machines IE6?

    11/17/2008 5:46 AM
  • Rakesh said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Hi timheuer,

    Sorry we cannot give the link without client's permission.
    we are getting this in 2 machines. one use IE6(in XP) and other one IE7(in Vista).This error comming only in client's server not in our server an localhost.

    Thanks

    11/17/2008 9:30 PM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Rakesh: it will be hard for me to be definitive without being able to troubleshoot unfortunately.

    11/18/2008 2:17 AM
  • Saif  said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    hi tim, actually i am not able to get teh data from wcf service in silverlight 2, i have debug with firebug and i am getting the error 500 on accessing the service like http://localhost/LeaveBusinessLayer/service.svc, the page is getting clientproxy file properly, problem is occuring in next step when it tries to access the service, service is working well for other non silverlight pages(asp.net).
    my clientproxy file is this:













    and by this code i am accessing it :
    public Page()
    {

    InitializeComponent();
    svc.GetQuartersCompleted += new EventHandler(svc_GetQuartersCompleted);
    svc.GetQuartersAsync();
    }


    web.config desc:





    maxReceivedMessageSize="2147483647">





    http://localhost/LeaveBusinessLayer/Service.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
    contract="BusinessLayer.IService" name="BasicHttpBinding_IService" />


    12/19/2008 5:34 AM
  • Saif  said:
    Gravatar
    # getting 500 response on valid wcf service


    hi tim, actually i am not able to get teh data from wcf service in silverlight 2, i have debug with firebug and i am getting the error 500 on accessing the service like http://localhost/LeaveBusinessLayer/service.svc, the page is getting clientproxy file properly, problem is occuring in next step when it tries to access the service, service is working well for other non silverlight pages(asp.net).
    my clientproxy file is this:
    "










    "
    and by this code i am accessing it :
    public Page()
    {

    InitializeComponent();
    svc.GetQuartersCompleted += new EventHandler(svc_GetQuartersCompleted);
    svc.GetQuartersAsync();
    }


    web.config desc:


    maxReceivedMessageSize="2147483647">
    http://localhost/LeaveBusinessLayer/Service.svc"
    binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IService"
    contract="BusinessLayer.IService" name="BasicHttpBinding_IService" />

    12/19/2008 5:49 AM
  • timheuer said:
    Gravatar
    # re: Silverlight cross domain services and a helpful tool


    Saif: if you are getting a 500 error, then something is occuring on the server. Examine the response data and see what the error is...it is something in the service that is erroring out.

    12/19/2008 7:24 AM

Your Reply.

  Comment Form  

Fields denoted with a "*" are required.

*Your name:
Subject:
Your blog:
Your email:  (will not be displayed)
*Your message:

 
Please add 1 and 8 and type the answer here: