在 ASP.NET 配置文件中存储接口类型

发布于 2024-07-30 18:39:07 字数 570 浏览 3 评论 0原文

在我的 ASP.NET 网站中,所有对数据层的调用都以接口形式返回实体,并且网站不需要知道具体类型是什么。 这工作正常,但是当我尝试将这些类型之一存储到用户配置文件中时遇到了问题。 我的接口实现了 ISerialized,如下所示:

public interface IInsured : IPerson, IEntity, ISerializable

并且数据层中的具体类型确实实现了 ISerializable。 web.config 中的我的 Profile 属性是:

<add name="ActiveInsured" type="FacadeInterfaces.IInsured" serializeAs="Binary" defaultValue="[null]"/>

它编译得很好,但我在 Profile.Save() 上收到运行时错误,指出接口无法序列化。 我也尝试过使用serializeAs =“Xml”。 我认为如果我的接口实现了 ISerialized 它就会起作用。

有人以前遇到过这个问题或者知道解决方法吗?

In my ASP.NET website all calls into the data layer return entities as interfaces and the website does not need to know what the concrete type is. This works fine, but I have run into a problem when trying to store one of those types into the user Profile. My interface implements ISerializable, like the following:

public interface IInsured : IPerson, IEntity, ISerializable

and the concrete type in the datalayer does implement ISerializable. My Profile property in web.config is:

<add name="ActiveInsured" type="FacadeInterfaces.IInsured" serializeAs="Binary" defaultValue="[null]"/>

It compiles just fine, but I get a runtime error on Profile.Save() saying that the interface cannot be serialized. I have also tried it with serializeAs="Xml". I thought that if my interface implemented ISerializable it would work.

Anybody had this problem before or know of a workaround?

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

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

发布评论

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

评论(1

秉烛思 2024-08-06 18:39:07

在配置文件属性的上下文中,您必须显式指定要序列化的类型。

您无法实例化接口,因此 ASP.net 无法实例化接口并发生您提到的异常。

In the context of a profile property, you must specify the type to be serialized explicitly.

You cannot instantiate an interface, thus ASP.net cannot instantiate an interface and the exception you mention occurs.

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