title: System.Security.SecurityException: Security error while trying to consume WCF Data service in Silverlight 5 application
body: <p>I am trying to consume a WCF data service in a Silverlight 5 application/.NET 4.0 application. I can see the data in the web browser as Atom feeds when i query the service manually. </p>
  
  <p>However when i try to query the data via standard approaches i am getting the following error:</p>
  
  <blockquote>
    <p>{System.Security.SecurityException: Security error.
       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)}</p>
  </blockquote>
  
  <p><strong>Inner exception is null.</strong> So no meaningfull information is available as yet.</p>
  
  <h2>My Code</h2>
  
  <pre><code>public partial class ProjectWindow : ChildWindow
  {
  XYZEntitis.XYZEntitis db = new XYZEntitis.XYZEntitis(new Uri("http://google.com/wcfdataservice1.svc/"));
  
  
      DataServiceCollection&lt;XYZEntitis.Label&gt; dscCats = new DataServiceCollection&lt;XYZEntitis.Label&gt;();
      CollectionViewSource customerAddressViewSource;
      ObservableCollection&lt;XYZEntitis.Label&gt; labelsBindingCollection = new ObservableCollection&lt;XYZEntitis.Label&gt;();
  public ProjectWindow()
      {
          InitializeComponent();
          btnBack.IsEnabled = false;
          //Load Project categories
          BindProjectCategories();
      }
  public void BindProjectCategories()
          {
              #region Code
              //#############################################
              //Approach # 01 : Raises Security Exception
              //#############################################
              //DataServiceQuery&lt;XYZEntitis.Label&gt; query = db.Labels;
              //query.BeginExecute(
              //    s =&gt;
              //    {
              //        var state = s.AsyncState as DataServiceQuery&lt;XYZEntitis.Label&gt;;
              //        var data = new ObservableCollection&lt;XYZEntitis.Label&gt;();
              //        foreach (var entity in state.EndExecute(s))
              //            data.Add(entity);
              //        cbCategoryName.ItemsSource = data;
              //    }, query);
              #endregion
              //#############################################
              //Approach No # 2 : 0 Records are returned
              //#############################################
              var cats = from cat in db.Labels select cat;
              //cats.ToList();
              dscCats.LoadCompleted += this.dscCats_LoadCompleted;
              dscCats.LoadAsync(cats);
  
              //#############################################
              //Approach # 3 : Security Exception
              //#############################################
              //var query = from cat in db.Labels select cat;
              //((DataServiceQuery&lt;XYZEntitis.Label&gt;)query).BeginExecute(OnCategoriesFecthComplete, query);
          }
  private void OnCategoriesFecthComplete(IAsyncResult ar)
      {
          var query = ar.AsyncState as DataServiceQuery&lt;XYZEntitis.Label&gt;;
  
          // Get the response of the query.
          var response = query.EndExecute(ar);
          foreach (XYZEntitis.Label label in response)
          {
              labelsBindingCollection.Add(label);
          }
          cbCategoryName.ItemsSource = labelsBindingCollection;
      }
  private void dscCats_LoadCompleted(object sender, LoadCompletedEventArgs e)
      {
          if (dscCats.Continuation != null)
          {
              dscCats.LoadNextPartialSetAsync();
          }
          else
          {
  
              cbCategoryName.ItemsSource = dscCats;
              #region code
  
              //var data = (DataServiceCollection&lt;XYZEntitis.Label&gt;)sender;
              //cbCategoryName.ItemsSource = data;
              //cbCategoryName.DisplayMemberPath = "Name";   
  
              //customerAddressViewSource =
              //       (CollectionViewSource)this.Resources["customerAddressViewSource"];
              //customerAddressViewSource.Source = dscCats;
              #endregion
          }
  
      }
  </code></pre>
  
  <p>}</p>
  
  <h2>Complete Stack Trace (Stack Trace 1):</h2>
  
  <blockquote>
    <p>System.InvalidOperationException was unhandled by user code
      Message=An error occurred while processing this request.
      StackTrace:
           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 ThreatModeler.Silverlight.ProjectWindow.OnCategoriesFecthComplete(IAsyncResult ar)
           at System.Data.Services.Client.BaseAsyncResult.HandleCompleted()
           at System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult asyncResult)
           at System.Data.Services.Client.BaseAsyncResult.&lt;>c_<em>DisplayClass1.b</em>_0(IAsyncResult asyncResult)
           at System.Net.Browser.ClientHttpWebRequest.&lt;>c_<em>DisplayClass1a.b</em>_18(Object state2)
           at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
           at System.Threading.ThreadPoolWorkQueue.Dispatch()
           at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()
      InnerException: System.Security.SecurityException
           Message=Security error.
           StackTrace:
                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: </p>
  </blockquote>
  
  <h2>Complete Stack Trace (Stack Trace #02)</h2>
  
  <blockquote>
    <p>{System.InvalidOperationException: An error occurred while processing
    this request. ---> System.Security.SecurityException: Security error. 
    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)    --- End of inner exception stack trace ---    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
    ThreatModeler.Silverlight.ProjectWindow.b_<em>0(IAsyncResult
    s)    at System.Data.Services.Client.BaseAsyncResult.HandleCompleted()
    at
    System.Data.Services.Client.QueryResult.AsyncEndGetResponse(IAsyncResult
    asyncResult)    at
    System.Data.Services.Client.BaseAsyncResult.&lt;>c</em>_DisplayClass1.b_<em>0(IAsyncResult
    asyncResult)    at
    System.Net.Browser.ClientHttpWebRequest.&lt;>c</em>_DisplayClass1a.b__18(Object
    state2)    at
    System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object
    state)    at System.Threading.ExecutionContext.Run(ExecutionContext
    executionContext, ContextCallback callback, Object state, Boolean
    preserveSyncCtx)    at
    System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
    at System.Threading.ThreadPoolWorkQueue.Dispatch()    at
    System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()}</p>
  </blockquote>