OData / WCF 数据服务不适用于复杂类型

发布于 2024-11-29 00:00:44 字数 2734 浏览 1 评论 0原文

我对 OData 和 WCF 数据服务是全新的,所以这可能是一个简单的问题。我正在使用 VS Web Developer Express 2010,其中我在控制台应用程序中托管了一个非常简单的 WCF 数据服务。它从存储库(位于单独的 dll 项目中)返回一个简单“Study”类的 IQuerable 集合,该集合又从另一个 dll 中的数据库项目中检索“Study”类(因此解决方案中有 3 个项目)。

我在数据库项目中还有一个“实验”类,并且一项研究中可以有多个实验。当我从研究中排除实验类时,一切正常,并且我得到了返回的数据。当我将 List 集合添加到 Study 类时,就会出现问题,然后当我尝试运行该服务时,会出现运行时错误。在 Firebug 中,错误是“500 内部服务器错误”,浏览器中的消息是“请求错误”。服务器在处理请求时遇到错误。有关更多详细信息,请参阅服务器日志。

我有 IIS 7,而且刚刚安装了 IIS 7.5,但它对我来说还是全新的,所以我无法弄清楚该服务在哪里托管或在哪里查看服务器/Web 日志。 “C:\inetpub\logs\LogFiles\W3SVC1”中仅可见 IIS 7 日志。当我运行应用程序时,VS Web 服务器(Cassini)不会启动,因此这表明它托管在 IIS 7.5(?)中。

所以
- 如何返回子类/复杂对象?
- 我如何知道我的服务托管在哪里以及在哪里可以找到服务器日志?

这是主机应用程序:

using MyStudyRepository;
using MyStudyDB;

namespace MyStudyService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string serviceAddress = "http://localhost:998";
            Uri[] uriArray = { new Uri(serviceAddress) };
            Type serviceType = typeof(StudyDataService);

            using (var host = new DataServiceHost(serviceType,uriArray))
            {
                host.Open();
                Console.WriteLine("Press any key to stop service");
                Console.ReadKey();
            }
        }
    }

    public class StudyDataService : DataService<StudyRepository>
    {
        public static void InitializeService(IDataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        }
    }
}

这是存储库:

using MyStudyDB;

namespace MyStudyRepository
{
    public class StudyRepository
    {
        List<Study> _List = new List<Study>();

        //Add constructor to populate myStudies list on creation of class
        public StudyRepository()
        {
            for (int i = 1; i < 5; i++)
            {
                Study myStudy = new Study() { ID = i, StudyOwnerId = i, StudyName = "Study" + i.ToString() /*, Experiments = null */ };
                _List.Add(myStudy);
            }
        }

        public IQueryable<Study> Studies
        {
            get
            {
                return _List.AsQueryable<Study>();
            }
        }
    }
}

这是数据库:

namespace MyStudyDB
{
    public class Study
    {
        public int ID { get; set;}
        public int StudyOwnerId { get; set; }
        public string StudyName { get; set; }
        //public List<Experiment> Experiments { get; set; }
    }

    public class Experiment
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int StudyId { get; set; }
    }
}

I'm brand new to OData and WCF data services so this might be an easy problem. I'm using VS Web Developer Express 2010 where I have a very simple WCF Data Service hosted in a console app. It's returning an IQuerable collection of a simple 'Study' class from a repository (located in a separated dll project), which in turn retrieves 'Study' classes from a db project in another dll (so 3 projects in the solution).

I also have an 'Experiment' class in the db project and there can be multiple Experiments in a Study. When I exclude the Experiment class from the Study everything works and I get data coming back. The problem happens when I add a List collection to the Study class, then I get a runtime error when I try to run the service. In Firebug the error is '500 Internal Server Error', and the message in the browser is 'Request Error. The server encountered an error processing the request. See server logs for more details.'

I have IIS 7 and I also just installed IIS 7.5 but again it's brand new to me, so I can't figure out where the service is hosted or where to view the server / web logs. There are only IIS 7 logs visible in 'C:\inetpub\logs\LogFiles\W3SVC1'. The VS web server (Cassini) doesn't start when I run the app, so this suggests it's being hosted in IIS 7.5 (?).

So
- how do I return child classes / complex objects?
- how do I know where my service is hosted and where can I find the server logs?

Here's the host app:

using MyStudyRepository;
using MyStudyDB;

namespace MyStudyService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string serviceAddress = "http://localhost:998";
            Uri[] uriArray = { new Uri(serviceAddress) };
            Type serviceType = typeof(StudyDataService);

            using (var host = new DataServiceHost(serviceType,uriArray))
            {
                host.Open();
                Console.WriteLine("Press any key to stop service");
                Console.ReadKey();
            }
        }
    }

    public class StudyDataService : DataService<StudyRepository>
    {
        public static void InitializeService(IDataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        }
    }
}

Here's the repository:

using MyStudyDB;

namespace MyStudyRepository
{
    public class StudyRepository
    {
        List<Study> _List = new List<Study>();

        //Add constructor to populate myStudies list on creation of class
        public StudyRepository()
        {
            for (int i = 1; i < 5; i++)
            {
                Study myStudy = new Study() { ID = i, StudyOwnerId = i, StudyName = "Study" + i.ToString() /*, Experiments = null */ };
                _List.Add(myStudy);
            }
        }

        public IQueryable<Study> Studies
        {
            get
            {
                return _List.AsQueryable<Study>();
            }
        }
    }
}

And here's the DB:

namespace MyStudyDB
{
    public class Study
    {
        public int ID { get; set;}
        public int StudyOwnerId { get; set; }
        public string StudyName { get; set; }
        //public List<Experiment> Experiments { get; set; }
    }

    public class Experiment
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int StudyId { get; set; }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

治碍 2024-12-06 00:00:44

要调试 WCF 数据服务,请参阅此博客文章:

  • Experiment 类未被识别为实体类型,因为没有为其设置实体。 (实体集是存储库类上的 IQueryable 属性,但您没有)。因此,Experiment 类仅被识别为复杂类型。
  • 当前发布的 WCF 数据服务版本不支持 MultiValue,MultiValue 实际上是原始或复杂类型的集合。

所以你有两种方法来“解决”这个问题。通过在存储库类上添加 IQueryable 属性,确保 Experiment 实际上是一个实体。
或者使用最新的 CTP (http://blogs.msdn.com/b/astoriateam/archive/2011/06/30/announcing-wcf-data-services-june-2011-ctp-for-net4-amp-sl4)。 aspx) 确实支持多值。

To debug the WCF Data Service please refer to this blog post: http://blogs.msdn.com/b/phaniraj/archive/2008/06/18/debugging-ado-net-data-services.aspx
As to why the collection of Experiment doesn't work, there are two reasons:

  • The Experiment class is not recognized as an entity type because there's no entity set for it. (Entity set is the IQueryable property on your repository class, which you don't have). As a result the Experiment class is only recognized as a complex type.
  • The currently released version of WCF Data Services doesn't support MultiValues, MultiValue is effectively a collection of primitive or complex types.

So you have two way to "fix" this. Either make sure that Experiment is in fact an entity, by adding IQueryable property on your repository class.
Or use the latest CTP (http://blogs.msdn.com/b/astoriateam/archive/2011/06/30/announcing-wcf-data-services-june-2011-ctp-for-net4-amp-sl4.aspx) which does support MultiValues.

夜声 2024-12-06 00:00:44

谢谢!我猜它缺少类上的 DataServiceKey 属性,如下所示:

[DataServiceKey("ID")]
public class Study
{ 
    .....
} 

Thanks! And I guess it is missing the DataServiceKey attribute on the class as follows:

[DataServiceKey("ID")]
public class Study
{ 
    .....
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文