0

任何人都可以帮助以下代码吗?

我正在尝试通过 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);  

        }
    }
}
4

1 回答 1

0

在没有从异常中看到堆栈跟踪的情况下,我不得不猜测。

可能性 1

可能是这样ObjectContextnull因此,这条线会抛出你得到的异常。

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.GetMediaURLBasedOnMonthYearis notnull并且它的Value属性 is not null。情况可能并非如此。

推荐

我建议在这些行上放置一个断点并查看调试器中变量的样子以跟踪null引用。从那里,您可以开始找出它为什么是null这样,或者让它不是,或者修复您的代码,以便它适当地处理null引用。

于 2012-09-12T12:47:42.240 回答