在 ASP.NET 配置文件中存储接口类型
在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在配置文件属性的上下文中,您必须显式指定要序列化的类型。
您无法实例化接口,因此 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.