任何人都可以使用以下代码吗?
我正试图通过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);
}
}
}
答案 0 :(得分:0)
没有看到异常的堆栈跟踪,我不得不猜测。
ObjectContext
可能是null
,因此,这一行会抛出你得到的异常。
return (from p in this.ObjectContext.at_AdvertVideoAdvertisement
where p.AdvertMediaMonthYear == strMonthYear
select p.AdvertMediaURL).ToList();
this.ObjectContext.at_AdvertVideoAdvertisement
的内容是否有null
?
如果是这样,p
可能是null
,这会导致查询抛出异常。
我怀疑违规行是:
iv.Value.ToString();
这一行什么都不做,但是你也会在一个有用的上下文中重复这几行,所以也许第一个声明是错误的。但是,这假设InvokeOperation
返回的VideoAdvertDomainContext.GetMediaURLBasedOnMonthYear
值不是null
且其Value
属性不是null
。情况可能并非如此。
我建议在这些行上放置一个断点,并查看调试器中的变量,以跟踪null
引用。从那里开始,您可以开始计算它为null
的原因,并将其设置为不是,或修复您的代码,以便它适当地处理null
引用。