CommunicationException with Silverlight and a cross domain call

You are calling your services from Silverlight but the services are accessed via a different URI than the Silverlight application is being hosted. Errors galore, right? 

Silverlight honors the protection that helps mitigate a sites scripts from being called if someone simply copied your app to their server. 

The error you get is this: 

An error occurred while trying to make a request to URI ‘http://localhost.:2935/Services/UserService.svc’. 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. This error may also be caused by using internal types in the web service proxy without using the InternalsVisibleToAttribute attribute. Please see the inner exception for more details. 

This error is very easily fixed. 

Create a file in you web root. Must be the root called clientaccesspolicy.xml . It must be named exactly and must live in the root. 

File: clientaccesspolicy.xml

Now, copy this into the 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 path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-domain-access>
</access-policy>

Save it, rebuild and run. Voila, you should now be up and running. If you need more info, check this helpful link. 

Happy Coding!

Fiddler and Silverlight with the localhost, how to debug WCF failures!

If you use Silverlight and you are trying to look at why service calls are failing, you will want to use Fiddler to see what is actually going on. Is the request going through, is the server returning an error. Since Silverlight always gives the same crappy error, Endpoint not found or something similair, then looking at the actual raw request responses is pretty helpful.

So you turn on Fiddler, but no requests appear? Well, some strange issues, with a long explanation, about using localhost don’t get logged into Fiddler. The trick is to rename your service references to localhost. Yes, simply add the .

Just make sure you type that URL the same way into your browser, otherwise you get a cross domain call issue. That is the subject for the next quick post.

Happy coding!

Silverlight and WPF Boolean Not Converter…To be or NOT to be

So you have an IsReadOnly flag on your viewmodel, but you want to set the IsEnabled property on control. A conundrum for a binding, right.

Well, simple solution to an annoying problem. Use the converter below on your binding and it becomes opposite day.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
 
namespace Seekford{
   
public class BooleanNotConverter : IValueConverter
    {
       
//easy peezy.
 
       
/// <summary>
       
/// Converts a value.
       
/// </summary>
       
/// <param name="value">The value produced by the binding source.</param>
       
/// <param name="targetType">The type of the binding target property.</param>
       
/// <param name="parameter">The converter parameter to use.</param>
       
/// <param name="culture">The culture to use in the converter.</param>
       
/// <returns>
       
/// A converted value. If the method returns null, the valid null value is used.
       
/// </returns>
       
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           
return !(bool)value;
        }
 
       
/// <summary>
       
/// Converts a value.
       
/// </summary>
       
/// <param name="value">The value that is produced by the binding target.</param>
       
/// <param name="targetType">The type to convert to.</param>
       
/// <param name="parameter">The converter parameter to use.</param>
       
/// <param name="culture">The culture to use in the converter.</param>
       
/// <returns>
       
/// A converted value. If the method returns null, the valid null value is used.
       
/// </returns>
       
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
           
return !(bool)value;
        }
 
        
    }
}

 

Happy Coding!