locked
WCF data service not returning my Sproc information?? RRS feed

  • Question

  • I've been trying to get this working for a while now and there must be some small thing I'm missing here, I can pull table information just fine...just not from my Sproc.  I tested the service and I get information back from my Sproc, so I know that is good (see below)

    <?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
    - <GetUser xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices"> 
    - <element p2:type="crv4Model.GetUser_Result" xmlns:p2="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"> 
     <UserID p2:type="Edm.Int32">5004</UserID> 
     <UserName>ealton</UserName> 
     <FirstName>Ed</FirstName> 
     <LastName>Alton</LastName> 
     <Role>E</Role> 
     <LastLogin p2:type="Edm.DateTime">2010-11-09T15:18:22.227</LastLogin> 
     </element> 
     </GetUser>
    

    Attached are my code pages:

    UserVM.cs

    using System;
    using System.Collections.ObjectModel;
    using System.Data.Services.Client;
    using System.Collections.Generic;
    using System.Linq;
    using System.ComponentModel;
    using BAHelloWorld.wcfDStry;
    
    namespace BAHelloWorld.ViewModel
    {
      public class UserVM : ViewModelBase
      {
        crv4Entities _context = new crv4Entities(new Uri("wcfDS_reprtsUsers.svc", UriKind.Relative));
        public DataServiceCollection<Users> UserList { get; private set; }
    
        
        Int32 _userid;
        public Int32 UserID
        {
          get{return _userid;}
          set
          {
            _userid = value;
            NotifyPropertyChanged("UserID");
          }
         
         }
    
        string _firstName;
        public string FirstName
         {
          get{return _firstName;}
          set
          {
            _firstName = value;
            NotifyPropertyChanged("FirstName");
          }
         
         }
    
        string _lastName;
        public string LastName
        {
          get { return _lastName; }
          set
          {
            _lastName = value;
            NotifyPropertyChanged("LastName");
          }
    
        }
    
        char _role;
        public char Role
        {
          get { return _role; }
          set
          {
            _role = value;
            NotifyPropertyChanged("Role");
          }
    
        }
    
        DateTime _lastLogin;
        public DateTime LastLogin
        {
          get { return _lastLogin; }
          set
          {
            _lastLogin = value;
            NotifyPropertyChanged("LastLogin");
          }
    
        }
    
    
        public UserVM()
        {
          
          UserList = new DataServiceCollection<Users>();
          
          UserList.LoadCompleted += new EventHandler<LoadCompletedEventArgs>(LoadCompleted);
          this.PropertyChanged += (s, ea) =>
            {
              //if (ea.PropertyName == "UserID")
              //{
              //  GetUsersAsync();
              //  //UserList = ea.Result;
              //}
            };
          
           GetUsersAsync();
        }
    
        public void GetUsersAsync()
        {
          
          
           string UserName = "'ealton'";
        string CaseSense = "'N'";
        var query = context.CreateQuery<Users>("GetUser").AddQueryOption("UserName", UserName).AddQueryOption("CaseSense", CaseSense);
          UserList.LoadAsync(query);
          
        }
    
        void LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
          if (e.Error != null)
          {
            ErrorWindow.CreateNew(e.Error);      }
          else // UserList should be filled now
          {
            Int32 mylistcount = UserList.Count; //count is always 0 and UserList seems to never be holding any data
          }
    
        }
    
       
        void item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
          Users usr = (Users)sender;
          context.UpdateObject(usr);
        }
    
        public ObservableCollection<Users> GetUser //My Sproc name
        {
          get
          {
            return UserList; 
          }
        }
      }
    }
    

    My Service cod "wcfDS_reprtsUsers.svc.cs"

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Data.Services;
    using System.Data.Services.Common;
    using System.Linq;
    using System.ServiceModel.Web;
    using System.Web;
    using System.Web.Services;
    
    
    namespace BAHelloWorld.Web
    {
    
      public class wcfDS_reprtsUsers : DataService<crv4Entities>
      {
    
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
    
          //Full CRUD operations
          config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
    
          //Read Only access
          config.SetServiceOperationAccessRule("*", ServiceOperationRights.AllRead);
    
          // Set the data service version to V2 to support paging.
          config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    
          config.MaxResultsPerCollection = 1;    
    
        }
        
        
        [WebGet] 
        public IQueryable<GetUser_Result> GetUser( string CaseSense, string UserName)
        {
     
          return this.CurrentDataSource.GetUser(UserName, CaseSense).AsQueryable();
    
        }
      
    
      }
    }
    

    Stepping through my code when it hits the "LoadCompleted" section of my UserVM.cs code

    if (e.Error != null) 
          { 
            ErrorWindow.Create(e.Error); 
          } 
          else // UserList should be filled now 
          { 
            Int32 mylistcount = UserList.Count; 
          } 
    
    

    I hover over the event arg "e" and I see it's throwing one exception saying: e.QueryOperationResponse.TotalCount threw an {System.SystemException} = {System.InvalidOperationException: The XmlReader must be on a node of type Element instead of a node of type None.
       at System.Xml.Linq.XElement.Load(XmlReader reader, LoadOptions options)
       at System.Xml.Linq.XElement.Load(XmlReader reader)
       at Syst...

    The e.Error value is still null however and doesn't enter the IF porition of code.

    Wednesday, November 17, 2010 10:16 PM

All replies

  • Hi,

    It seems (and without the $metadata output I can't really confirm that), that the GetUser service operation return IEnumerable<GetUser_Result>, where the GetUser_Result is a complex type and not an entity type. Our client currently only supports reading responses from service operations which return either a single entity or a set of entities. The best workaround is to use some kind of XML library (XLinq for example) and read the response manually. Or make the service operation return an entity or set of entities.

    Thanks,


    Vitek Karas [MSFT]
    Wednesday, November 17, 2010 10:52 PM
    Moderator
  • Is there a thread or link that does this or shows this?  I cant imagine anyone not wanting to be able to use WCF data servies with complex types and stored procs as most application conversions into Silverlight will nodoubtedly contain many stored procs, yet I dont see anything on the internet that shows this, other than using standard ado.net.

     

    Friday, November 19, 2010 7:40 PM
  • Ok So I decided to create a custom entity matching my Stored proc result and use Function import to return this entity. But when I create the custom entity called "NewCustomer"  in EDM i get an error when compiling

     

    Error 3027: No mapping specified for the following EntitySet/AssociationSet - NewCustomers

     

    How do I map the entity set "NewCustomers" for the custom entitiy NewCustomer?

     

    Even after I create the function import I still have the error.

    I am using EF4 and I matched the custom entity properties to my Stored proc exactly (name and type).

     

    Thanks for your input. I will like to avoid parsing XML etc for using complex types!

     

     

    Tuesday, February 8, 2011 12:37 PM