nav-left cat-right
cat-right

The “ValidateXaml” task failed unexpec...

So, you found that code example that helps you solve the problem you have been beating your head against the wall for. You most likely downloaded a .zip file and extracted its contents onto your local machine. You then open the solution in your favorite flavor of Visual Studio. When you attempt to compile the sample, you get a compile-time that reads something like :

Error    3    The “ValidateXaml” task failed unexpectedly.
System.IO.FileLoadException: Could not load file or assembly ‘file:///C:\MyDirectory\MyApplication\MyApplication\Bin\Debug\Assembly.dll’ or one of its dependencies. Operation is not supported. (Exception from HRESULT: 0×80131515)
File name: ‘file:///C:\MyDirectory\MyApplication\MyApplication\Bin\Debug\Assembly.dll’ System.NotSupportedException: An attempt was made to load an assembly from a network location which would have caused the assembly to be sandboxed in previous versions of the .NET Framework. This release of the .NET Framework does not enable CAS policy by default, so this load may be dangerous. If this load is not intended to sandbox the assembly, please enable the loadFromRemoteSources switch. See http://go.microsoft.com/fwlink/?LinkId=155569 for more information.

at System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks)
at System.Reflection.RuntimeAssembly.InternalLoadFrom(String assemblyFile, Evidence securityEvidence, Byte[] hashValue, AssemblyHashAlgorithm hashAlgorithm, Boolean forIntrospection, Boolean suppressSecurityChecks, StackCrawlMark& stackMark)
at System.Reflection.Assembly.LoadFrom(String assemblyFile)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.XamlValidator.Execute(ITask task)
at Microsoft.Silverlight.Build.Tasks.ValidateXaml.Execute()
at Microsoft.Build.BackEnd.TaskExecutionHost.Microsoft.Build.BackEnd.ITaskExecutionHost.Execute()
at Microsoft.Build.BackEnd.TaskBuilder.ExecuteInstantiatedTask(ITaskExecutionHost taskExecutionHost, TaskLoggingContext taskLoggingContext, TaskHost taskHost, ItemBucket bucket, TaskExecutionMode howToExecuteTask, Boolean& taskResult)

Whoa! This looked a bit intimidating the first time I saw it. The good news is, it’s an easy fix. The key information related to this error begins with the text “System.NotSupportedException“. What this basically goes on to say is that the offending component  has been blocked by Windows for security reasons. To overcome this, open Windows Explorer and navigate to the directory where the offending component exists. One there right click on the component and choose “Properties”. For the sake of a visualization, this is what this looks like:

Properties

When you click the “Properties” option, a dialog window will appear. Towards the bottom of this dialog, you will see the “security” information which says: “This file came from another computer and might be blocked to help protect this computer”. To the right of this text is a button that says “Unblock”. This is the button we are looking for as highlighted here:

Security

Simply click the “Unblock” button and rebuild your application. You may have to do this several times if there are multiple assemblies that were downloaded along with the code sample. Regardless, I hope you have found this post valuable. If you would like to learn more about developing applications with Silverlight, I highly recommend Silverlight in Action by Pete Brown. The first version of this book series was written by Chad Campbell (me) and John Stockton.

Type ‘MyType’ is an invalid collection...

Recently I was adding some new features to DivotDog that use JSON. This JSON was intended to be a serialized version of a custom collection that was needed. In an effort to take advantage of WCF’s baked-in serialization features, I created a collection that looks something like the following:


[DataContract]
public class MyTypeCollection : CollectionBase
{
public MyTypeCollection()
{ }


public int Add(MyType item)
{ return List.Add(item); }


public void Insert(int index, MyType item)
{ List.Insert(index, item); }


public void Remove(MyType item)
{ List.Remove(item); }


public bool Contains(MyType item)
{ return List.Contains(item); }


public int IndexOf(MyType item)
{ return List.IndexOf(item); }


public void CopyTo(MyType[] array, int index)
{ List.CopyTo(array, index); }
}

While everything compiled fine, when I navigated to MyService.svc in my browser window, I received an error that stated:

Type ‘MyTypeCollection’ is an invalid collection type since it has DataContractAttribute attribute

To overcome this issue, I simply replaced the DataContract attribute on MyTypeCollection with an attribute called CollectionDataContract. This attribute basically enables you to easily serialize a custom collection. More information can be found here.

Silverlight – HTTP request to [Url] was abor...

If you are developing a Silverlight application that connects to a WCF service, you may have received the following error:

The HTTP request to ‘http://localhost:2997/SomeService.svc’ was aborted.  This may be due to the local channel being closed while the request was still in progress.  If this behavior is not desired, then update your code so that it does not close the channel while request operations are still in progress.

This error generally happens when a request on the server takes longer than allowed. To remedy this, you can set four properties across two .config files. I will first explain the four properties. Then, I will detail the two configuration files involved.

Configuration Properties

The four properties that you need to set are called: openTimeout, closeTimeout, receiveTimeout, sendTimeout. These properties are defined as follows:

  1. openTimeout – A TimeSpan that details how long an operation can remain open.
  2. closeTimeout – A TimeSpan that signals how long an operation has to close.
  3. receiveTimeout – A TimeSpan that represents how long a receive operation has to complete.
  4. sendTimeout – A TimeSpan that details how long a send operation has to complete.

More specifics can be found here. Regardless, because each of these properties are TimeSpan elements, you can set the timeouts to whatever granularity you find acceptable. If you are not interested in going into that level of detail, you can set them all to the same value. If you are experiencing the error that is the subject of this post, set each of these values to higher value than the default. The default value is a measly one minute. If you’re operation takes longer than one minute, you may choose to up the default values to five minutes as a test, or even higher if need be. An example configuration that sets the timeouts to five minutes  is shown here:

<binding name="CustomBinding_MyService" closeTimeout="00:05:00" openTimeout="00:5:00" receiveTimeout="00:05:00" sendTimeout="00:05:00">
  <binaryMessageEncoding />
  <httpTransport>
    <extendedProtectionPolicy policyEnforcement="Never" />
  </httpTransport>
</binding>

As shown above, the four properties detailed earlier can be configured on a Binding element in your config file.  In the world of Silverlight though, it is important to set these property values in two configuration files.

The Two Configuration Files

Silverlight applications will often times have a web.config file on the server-side. On the client-side there is a ServiceReferences.clientConfig file that will also need to be updated. One common mistake is just adding the timeout configurations on the server-side web.config file and not adding them on the client side. Because of this, please make sure that you set the configuration values on both the server, and client side. And for the sake of sanity, it is recommended that you set them to the same value :) .

If you would like to learn more about Silverlight, I recommend the book Silverlight in Action. I hope you found this post valuable.

« Previous Entries Next Entries »