locked
Passing an Entity Framework Context object to WCF RRS feed

  • Question

  • I need to pass a Context object from EF into a WCF method.

    Normally, I create the Context object in the WCF method and dispose of it right before the end of the method call which works just fine for most of my methods.

    However, I need to pass the Context object (specifically the DBContext) over from the MVC controller to my specific WCF method because I have caching enabled for some lookup tables. I need this specific Context object passed over (the one I set in the Application_Start method of the Global.asax file) rather than what I do in the sentence above because I use this specific object for the SqlDependency. If I try and create the DBContext object brand new, I can't use the SqlDependency becuase I will get an error informing me that the SqlDependency needs to be enabled before the database call.

    The problem is that I'm getting the following error (shortened for brevity) when I try and start my WCF Test Client tool which I know has something to do with not properly declaring a KnownType attribute (ie the DBContext object). Note that the WCF project compiles just fine. I need some help with this specific part since I have never used a KnownType in my WCF service. They have all been simple types (int, string, etc).

    Note that I put the KnownType attribute in my DataContract after I got the below error.

    Error: Cannot obtain Metadata from http://localhost:8732/Design_Time_Addresses/YeagerTechWcfService/YeagerTechWcfService/mex If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:8732/Design_Time_Addresses/YeagerTechWcfService/YeagerTechWcfService/mex Metadata contains a reference that cannot be resolved:

    I have the following OperationContract code in my WCF service:

    [OperationContract]
            IEnumerable<Category> GetCategories(YeagerTechEntities DbContext);

    I have the following DataContract code in my WCF service:

    namespace YeagerTechModel
    {
        [Serializable]
        [DataContract(IsReference = true)]
        [KnownType(typeof(YeagerTechEntities))]
        public partial class Category
        {
            public Category()
            {
                this.Projects = new HashSet<Project>();
            }
     
            [DataMember]
            public short CategoryID { get; set; }
            [DataMember]
            public string Description { get; set; }
     
            [DataMember]
            public virtual ICollection<Project> Projects { get; set; }
        }
     
    }
    Finally, the following is my WCF method:
    public IEnumerable<YeagerTechModel.Category> GetCategories(YeagerTechEntities DbContext)
            {
                //YeagerTechEntities DbContext = new YeagerTechEntities();
     
                DbContext.Configuration.ProxyCreationEnabled = false;
     
                IEnumerable<YeagerTechModel.Category> category = DbContext.Categories.Where(p => p.CategoryID > 0).AsCached("Categories").ToList();
                //IEnumerable<YeagerTechModel.Category> category = DbContext.Categories.Where(p => p.CategoryID > 0);
     
                CloseConnection(DbContext);
     
                return category;
            }

    Bill Yeager
    • Edited by Bill_Yeager Sunday, December 11, 2011 6:25 AM
    Sunday, December 11, 2011 6:17 AM

Answers

All replies

  • I am simply going to wait until EF will have a caching property built into it in a later version. It's a wonder to me why MS did not think of having something like this in place since the 4.0 or 4.1 frameworks came out.

    Hopefully, they will implement this soon in an upcoming framework. I know NHibernate has something of a SQL Dependency cache built into it.


    Bill Yeager
    Sunday, December 11, 2011 6:32 PM
  • Hi Bill,

    Welcome!

    I find this post here: http://social.msdn.microsoft.com/Forums/is/adonetefx/thread/c8f63304-bde1-4bb9-9682-2468750f9ff6

    Not sure if it helps. I'd like to let product team to hear your voice, thanks for understanding.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Alan_chen Tuesday, December 20, 2011 2:12 AM
    Monday, December 12, 2011 12:48 PM