• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTky

Related Code
Generic Entity Framework 4.0 Base Repository
Loading Strategy for Entity Framework 4.0
Generic Entity Framework 4.0 Base Repository with Paging
A Default Entity Framework 4.0 ObjectContext Factory
WCF Service factory for using Microsoft Unity 2.0 to create an instance of a Service
Generic linq-to-sql repository
UnitOfWork Action filter for ASP.NET MVC and nHibernate

Keep Entity Framework ObjectContext in a WCF Service OperationContext

This code can be used to keep a ObjectContext live during a WCF Service OperationContext.

1
1.36k 0 0 0 0 0

The following code can be used within a WCF Service to share a ObjectContext between other objects, for example between Repositories so they can reuse the same Unity of Work and Identity Map during a WCF Operation.

public interface IObjectContext : IDisposable
{
    IObjectSet<T> CreateObjectSet<T>() where T : class;

    void SaveChanges();
}


public class WcfObjectContextAdapter : IObjectContext
{
    public IObjectSet<T> CreateObjectSet<T>() where T : class
    {
        return Context.CreateObjectSet<T>();
    }


    public void SaveChanges()
    {
        Context.SaveChanges();
    }


    public void Dispose()
    {
        Context.Dispose();
    }


    public WcfObjectContextAdapter(IObjectContextFactory objectContextFactory)
    {
        if (objectContextFactory == null)
            throw new ArgumentNullException("sessionFactory");

        if (Context == null)
            Context = objectContextFactory.Create();
    }


    public ObjectContext Context
    {
        get
        {
            if (WcfOperationContainerExtension.Current == null)
                return null;

            return WcfOperationContainerExtension.Current.Context;
        }
        set
        {
            if (CurrentOperationContext == null)
                return;

            CurrentOperationContext.Extensions.Add(new WcfOperationContainerExtension(value));
            CurrentOperationContext.OperationCompleted += CurrentOperationContext_OperationCompleted;
        }
    }


    private void CurrentOperationContext_OperationCompleted(object sender, EventArgs e)
    {
        var context = WcfOperationContainerExtension.Current.Context;
        context.Dispose();
    }


    private OperationContext CurrentOperationContext
    {
        get { return OperationContext.Current; }
    }


    private class WcfOperationContainerExtension : IExtension<OperationContext>
    {
        public ObjectContext Context { get; set; }

        public WcfOperationContainerExtension(ObjectContext context)
        {
            Context = context;
        }

        public void Attach(OperationContext owner) { }
        public void Detach(OperationContext owner) { }

        public static WcfOperationContainerExtension Current
        {
            get
            {
                if (OperationContext.Current == null)
                    return null;

                return OperationContext.Current.Extensions.Find<WcfOperationContainerExtension>();
            }
        }
    }
}


Can be used together with the BaseRepository


Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

ef4.0

Mark 'ef4.0' tag as 'like'

Mark 'ef4.0' tag as 'ignore'

objectcontext

Mark 'objectcontext' tag as 'like'

Mark 'objectcontext' tag as 'ignore'

repository

Mark 'repository' tag as 'like'

Mark 'repository' tag as 'ignore'

wcf

Mark 'wcf' tag as 'like'

Mark 'wcf' tag as 'ignore'


 @fredrikn "I'm the master"
3.11k
July 14, 2010 7:49 AM
edited July 15, 2010 11:18 PM

Fork

 Keep Entity Framework ObjectContext in a WCF Service OperationContext -  @fredrikn Wednesday 14, 2010 7:49 AM


0 Feedback


You must log in before you can give any feedback


0 Discussion(s)

Newest Oldest

You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010