wcf sending linq to sql DataContext
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; }
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?
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.
精彩评论