开发者

WCF, SubSonic SimpleRepository and multithreading

I'm currently in the process of writing a WCF REST service with SubSonic SimpleRepository and the WCF REST Contrib library. The service is going to be hosted on IIS 7. The database is going to be MS SQL Server. I could not really find a good example which is using this combination of technologies on the internet. The service should expose a database in a RESTful manner but also contains some business rules (e.g. registering users, saving statistics) so I can't use ADO.net Data Services. Because there are going to be a lot of clients accessing the service at the same time it's important that the service can serve several clients at the same time.

For good performance I want to use the following service behaviour:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession, ConcurrencyMode=ConcurrencyMode.Multiple)]

There are no instance variables in the Service class and as I understand it, those attribute values cause new a instance of the service class to be created for every call (not per session because it is called over https).

I used the following simple class for creating the SimpleRepository once (singleton):

public class DB
{
    private static SimpleRepository _Repository;
    public static SimpleRepository Repository
    {
        get
        {
            if (_Repository == null)
            {
                _Repository = new SimpleRepository("5Drive", SimpleRepositoryOptions.RunMigrations);
            }
            return _Repository;
        }
    }
}

Here is an example method of my service:

Interface:

    [OperationContract]
    [WebGet(UriTemplate = "vehicles")]
    [WebDispatchFormatter]
    Vehicles GetVehicles();

Implementation:

    public Vehicles GetVehicles()
    {
        // get all vehicles for the logged in user
        var vehicles = from v in DB.Repository.All<Vehicle>()
                       where v.UserID == GetUserID()
                       orderby v.Name
                       select v;
   开发者_开发技巧     return new Vehicles(vehicles);
    }

So, here is my question: can I do it this way? Is SimpleRepository thread-safe? Do I have to create a SimpleRepository in every method instead of using a singleton? What about performance when I do that? I looked at the SimpleRepository source code and I think that the migration code is not thread safe because the "private readonly List migrated;" is not synchronized but I might be wrong.

Maybe I'm missing something and I can just ignore multi-threading and configure IIS differently?

Thanks for your help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜