InitializeService 不再被调用,无法找出原因

发布于 2024-12-13 12:37:17 字数 1501 浏览 1 评论 0原文

我有一个 odata wcf 服务,看起来没有调用 InitializeService...

代码如下:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyDataService : DataService<MyContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;
    }

    // see http://romiller.com/2010/07/19/ef-ctp4-tips-tricks-wcf-data-service-on-dbcontext/
    protected override AzureAppContext CreateDataSource()
    {
        var ctx = base.CreateDataSource();

        // Disable proxy object creation.
        ctx.Configuration.ProxyCreationEnabled = false;
        return ctx;
    }
}

运行时,我收到一个异常报告,如下所示:

服务器在处理请求时遇到错误。例外情况 消息是“在数据上下文类型‘MyContext’上,有一个顶部 IQueryable 属性“MyEntities”,其元素类型不是实体 类型。确保 IQueryable 属性是实体类型或 指定数据上下文类型的 IgnoreProperties 属性 忽略这个属性。'。有关更多详细信息,请参阅服务器日志

在本例中,MyEntities 是一个 EntityFramework Code First DBSet。

如果我将 [IgnoreProperties("MyEntities")] 放在上下文中,则会在第二个属性集上引发错误。

我认为关键是:如果我在 InitializeService 方法中放置断点,那么它看起来不会被调用。

真的不知道现在发生了什么......

I've got an odata wcf service and it looks like InitializeService is not being called...

The code looks like:

[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class MyDataService : DataService<MyContext>
{
    // This method is called only once to initialize service-wide policies.
    public static void InitializeService(DataServiceConfiguration config)
    {
        // TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
        // Examples:
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        config.UseVerboseErrors = true;
    }

    // see http://romiller.com/2010/07/19/ef-ctp4-tips-tricks-wcf-data-service-on-dbcontext/
    protected override AzureAppContext CreateDataSource()
    {
        var ctx = base.CreateDataSource();

        // Disable proxy object creation.
        ctx.Configuration.ProxyCreationEnabled = false;
        return ctx;
    }
}

When this runs I get an exception reported like:

The server encountered an error processing the request. The exception
message is 'On data context type 'MyContext', there is a top
IQueryable property 'MyEntities' whose element type is not an entity
type. Make sure that the IQueryable property is of entity type or
specify the IgnoreProperties attribute on the data context type to
ignore this property.'. See server logs for more details

In this case, MyEntities is an EntityFramework Code First DBSet.

If I place [IgnoreProperties("MyEntities")] on the context, then the error is thrown on the second property set instead.

Key I think: If I put a breakpoint in the InitializeService method then it doesn't look like it's being called.

Really not sure what is happening right now...

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

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

发布评论

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

评论(1

口干舌燥 2024-12-20 12:37:17

肖恩帮助确定了上述评论中的正确答案。

基本上发生的事情是,当我导入项目时,stylecop 将我的键名称从 EntityID 调整为 EntityId。

在 SQL EF 层中,这没问题 - 因为我为键设置了映射。然而,在 oData 服务中,它正在寻找以“ID”结尾的名称(区分大小写),当它没有找到它们时,它会在没有解释的情况下抛出 - 不方便调试!

Sean helped identify the correct answer in the comments above.

Basically what had happened was that when I imported the project, then stylecop adjusted my key names from EntityID to EntityId.

Within the SQL EF layer this was OK - because I had mappings set up for the keys. However, within the oData Service it was looking for names ending in "ID" (case sensitive) and when it didn't find them it barfed without explanation - not making it easy to debug!

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