wcf 发送 linq 到 sql DataContext
假设我有一个 linq to sql 类。我想通过wcf返回对象数据上下文。我已经使数据库成为单向的。我将 WCF 工作 getString() 只是为了证明,如果我注释掉有关 ToolboxDataContext 的所有内容,则 wcf 服务可以工作。
public interface Database() { [OperationContract] ToolboxDataContext getCtx(); [OperationContract] string getString(); } public class test: Database public ToolboxDataContext getCtx() { ToolboxDataContext ctx = new ToolboxDataContext (); return ctx; } public string getString() { return "WCF WORKS"; } [DataContract] public class Testing //my svc file { [DataMember] public ToolboxDataContext ctx; [DataMember ] public string Id; }
Suppose I have a linq to sql class. I want to return the object datacontext throught wcf. I have already made the database unidirectional.I put the WCF works getString() just to demonstrate that the wcf service works if I comment out everything about the ToolboxDataContext.
public interface Database() { [OperationContract] ToolboxDataContext getCtx(); [OperationContract] string getString(); } public class test: Database public ToolboxDataContext getCtx() { ToolboxDataContext ctx = new ToolboxDataContext (); return ctx; } public string getString() { return "WCF WORKS"; } [DataContract] public class Testing //my svc file { [DataMember] public ToolboxDataContext ctx; [DataMember ] public string Id; }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的 ToolboxDataContext 是实体框架 DataContext 吗?假设是......
我不确定通过线路发送整个数据上下文对象是否真的是您想要做的。它可能不会从接收它的客户端“工作”。就像,您将无法对其运行 Linq 语句并访问数据库任何内容...您可能只想返回数据实体(而不是整个上下文),如图所示 此处 (MSDN),或者考虑使用 WCF 数据服务?
我想一个好问题是;客户端将如何处理 DataContext?
Is your
ToolboxDataContext
an Entity Framework DataContext? Assuming it is...I'm not sure sending the entire data context object over the wire is really what you want to do. It likely won't "work" from the client that receives it. Like, you won't be able to run a Linq statement against it and hit the database pr anything... You might just want to return the data entities (not the entire context) as shown here (MSDN), or perhaps consider using WCF Data Services instead?
I guess a good question is; what is the client going to do with the DataContext?
这似乎不是一个真正的问题(你的问题是什么,你在问什么?)
但是假设这只是你想要反馈的一个想法 - 这是没有意义的。它相当于向客户端发送数据库连接。你不能序列化 &反序列化连接然后重用它。
数据上下文是一个对象,因此一旦确保它用正确的属性修饰,您应该能够序列化它,但它封装了数据库连接。数据库连接无法序列化。从概念上讲,描述该连接的所有信息都可以序列化并通过 WCF 传递,但这可能会暴露系统中的安全缺陷。
使用 LINQ-to-SQL DataContext 的正确模式是实例化它,执行您需要的任何数据库操作,然后处理它。最好不要长时间持有它,并且绝对不要在您的服务之外发布它。
This doesn't appear to be a real question (what is your problem, and what are you asking?)
But assuming this is just an idea you have for which you want feedback - this doesn't make sense. It is the equivalent of sending a database connection to your client. You can't serialize & deserialize a connection and then reuse it.
The datacontext is an object and so you should be able to serialize it once you ensure it's decorated with the correct attributes, but it encapsulates a database connection. A database connection cannot be serialized. Conceptually all the information which describes that connection could be serialized and delivered over WCF, but that would potentially expose security flaws in your system.
The correct pattern for using a LINQ-to-SQL DataContext is to instantiate it, perform whatever database operations you require, and dispose of it. Best not to hold it for long periods and definitely don't publish it outside your service.