InvokeOperation C#和Silverlight

时间:2012-09-12 12:34:57

标签: c# silverlight

任何人都可以使用以下代码吗?

我正试图通过RIA Silverlight将值从服务器传递到客户端,但不断获取NullReferenceException

我已经删除了我尝试过的所有其他尝试,并且刚刚发布了最后一次尝试。

服务器端代码

 namespace Web.UI.SilverlightDomainServices 
 { 
    // Implements application logic using the SilverlightDBEntities context. 
    // TODO: Add your application logic to these methods or in additional methods. 
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access 
    // Also consider adding roles to restrict access as appropriate. 
    // [RequiresAuthentication] 
    [EnableClientAccess()] 
    public class VideoAdvertDomainService : LinqToEntitiesDomainService<SilverlightDBEntities> 
    { 
        // TODO: 
        // Consider constraining the results of your query method.  If you need additional input you can 
        // add parameters to this method or create additional query methods with different names. 
        // To support paging you will need to add ordering to the 'at_AdvertVideoAdvertisement' query. 


        string strMonthYear = DateTime.Now.ToString("MMMM-yyyy"); 

        [Invoke] 
        public List<string> GetMediaURLBasedOnMonthYear(string strMonthYear) 
        { 
            return (from p in this.ObjectContext.at_AdvertVideoAdvertisement 
                    where p.AdvertMediaMonthYear == strMonthYear 
                    select p.AdvertMediaURL).ToList(); 
        } 

        public IQueryable<at_AdvertVideoAdvertisement> GetAt_AdvertVideoAdvertisement() 
        { 
            return this.ObjectContext.at_AdvertVideoAdvertisement; 
        } 
    } 
} 

客户端代码

namespace Web.Silverlight  
{  
    public partial class MainPage : UserControl  
    {  
        public MainPage()  
        {  
            InitializeComponent();  
            Loaded += new RoutedEventHandler(MainPage_Loaded);  
        }  

        private VideoAdvertDomainContext ctx = new VideoAdvertDomainContext();  

        private void MainPage_Loaded(object sender, RoutedEventArgs e)  
        {  
            string strMonthYear = DateTime.Now.ToString("MMMM-yyyy");  

            VideoAdvertDomainContext DomainContext = new VideoAdvertDomainContext();  
            InvokeOperation iv = DomainContext.GetMediaURLBasedOnMonthYear("September-2012");  
            iv.Value.ToString();  
            PlaylistItem item = new PlaylistItem();  
            item.MediaSource = new Uri(iv.Value.ToString());  
            item.DeliveryMethod = Microsoft.SilverlightMediaFramework.Plugins.Primitives.DeliveryMethods.AdaptiveStreaming;  
            MP.Playlist.Add(item);  

        }
    }
}

1 个答案:

答案 0 :(得分:0)

没有看到异常的堆栈跟踪,我不得不猜测。

可能性1

ObjectContext可能是null,因此,这一行会抛出你得到的异常。

return (from p in this.ObjectContext.at_AdvertVideoAdvertisement 
        where p.AdvertMediaMonthYear == strMonthYear 
        select p.AdvertMediaURL).ToList();

可能性2

this.ObjectContext.at_AdvertVideoAdvertisement的内容是否有null

如果是这样,p可能是null,这会导致查询抛出异常。

可能性3

我怀疑违规行是:

iv.Value.ToString();

这一行什么都不做,但是你也会在一个有用的上下文中重复这几行,所以也许第一个声明是错误的。但是,这假设InvokeOperation返回的VideoAdvertDomainContext.GetMediaURLBasedOnMonthYear值不是null且其Value属性不是null。情况可能并非如此。

建议

我建议在这些行上放置一个断点,并查看调试器中的变量,以跟踪null引用。从那里开始,您可以开始计算它为null的原因,并将其设置为不是,或修复您的代码,以便它适当地处理null引用。