locked
WCF Services in an Application RRS feed

  • Question

  • User-974917549 posted

    I have grabbed some quick information tips about services basically. Well I need to design a server application with services placed in it and exposed such that my client application can call it and access its functionalities.

    Now I have started with WCF enabled application (though earlier I opted for RIA enabled architecture but didnt find the feasibility based on my requirements). So its a pure WCF application. I have designed some services and methods within it which can be used from a client application. I think i need to get a brief idea about a service method's behavior. Whenever I expose a service functionality which does a quite lot with the database at the server, I need to find out which is the optimum and reasonable way to keep the connection settings for the database transactions. Should I open a sqlconnection within a function and close it before leaving the function after I have done my desired functionality with the function? Which means I am encapsulating the method's functionality with an initial open and a final close commands. Is it the only way to do it? 

    Maybe such an approach would eat up much of the server's cpu time in performing the connection opening and closing functions. Or is there a way to keep the connection open to avoid such performance taking functionalities, while keeping in mind that it doesnt compromise on the security issues for the service?

    Please put on light and the various informations available over this topic as i would have a quite lot more of doubts and issues to be cleared on the go...


    Wednesday, June 9, 2010 5:30 AM

Answers

  • User-837620913 posted

    Connection Pooling occurs automatically based on the connection string and account that is accessing the database.  If you are not impersonating each user (you should not be), then connection pooling will work automatically:

    http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

    However, you need to follow proper data access best practices.  Opening your SqlConnection as late as possible and closing it as early as possible is a must.  With connection pooling, a new connection does not need to be made each time you call Open so don't worry about that.

    I would highly recommend either using the Data Access Application Block in Enterprise Library 5 (http://msdn.microsoft.com/en-us/library/ff632023.aspx).  It wraps up a lot of the data access best practices.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 9, 2010 7:34 AM
  • User-952121411 posted

    Should I open a sqlconnection within a function and close it before leaving the function after I have done my desired functionality with the function?
     

    Yes.  Services are stateless like the web.  There are some advanced ways to mimic state or session, but these are not default functionality characteristics.  They have no continuity between calls, so you must do all processing within the method that is required.  As referred to in the last post, connection pooling is a part of .NET that will help with performance in db calls. 

    To help you along, try some samples in WCF and watch a few of the videos.  WCF is a great way to go for services, and there is a ton of functionality within.

    Beginner's Guide to Windows Communication Foundation:

    http://msdn.microsoft.com/en-us/netframework/first-steps-with-wcf.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 9, 2010 2:10 PM

All replies

  • User-837620913 posted

    Connection Pooling occurs automatically based on the connection string and account that is accessing the database.  If you are not impersonating each user (you should not be), then connection pooling will work automatically:

    http://msdn.microsoft.com/en-us/library/8xx3tyca.aspx

    However, you need to follow proper data access best practices.  Opening your SqlConnection as late as possible and closing it as early as possible is a must.  With connection pooling, a new connection does not need to be made each time you call Open so don't worry about that.

    I would highly recommend either using the Data Access Application Block in Enterprise Library 5 (http://msdn.microsoft.com/en-us/library/ff632023.aspx).  It wraps up a lot of the data access best practices.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 9, 2010 7:34 AM
  • User-952121411 posted

    Should I open a sqlconnection within a function and close it before leaving the function after I have done my desired functionality with the function?
     

    Yes.  Services are stateless like the web.  There are some advanced ways to mimic state or session, but these are not default functionality characteristics.  They have no continuity between calls, so you must do all processing within the method that is required.  As referred to in the last post, connection pooling is a part of .NET that will help with performance in db calls. 

    To help you along, try some samples in WCF and watch a few of the videos.  WCF is a great way to go for services, and there is a ton of functionality within.

    Beginner's Guide to Windows Communication Foundation:

    http://msdn.microsoft.com/en-us/netframework/first-steps-with-wcf.aspx

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 9, 2010 2:10 PM
  • User-974917549 posted

    Thanks Darrell and Atconway for those crisp info

    Well is connection pooling really gonna serve its purpose for a service referenced function in the server? I am informed that connection pooling is done all by default in order to enable an application to use the same connection object created once which reduces the connection establishing "TIME" required. Naturally the performance as well as I guess. 

    So my question is..

    Is it ok to use pooling or disable it as the functions remains in a server as a service function? Which is better? Through service calls will pooling really improve the performance by atleast a bit? (REMEMBER its a service function)

    Is it gonna be proving good just like within an ordinary single web application? And if so, will it compromise the security of such functions?

    I need your valuable comments on this please

    :)

    Thursday, June 10, 2010 6:25 AM
  • User-974917549 posted

    And yes buddies besides the information about the connection pooling required or not, can you comment on the RIA services? I have seen a couple of videos from the Silverlight.Net but i restrained from it till I would hear some comments on them who have used it. I believe it makes life easier with it with the help of LINQ and the entity frameworks. I didnt understand terms quite deep but my requirement was to stick with the way of coding with SQL stored procedures. So that we were more aware and dependent on the hard code of SQL.

    I expect a wise advice from some one around   

    Thursday, June 10, 2010 6:46 AM
  • User-319574463 posted

    Connection pooling comes as part of the ADO.NET package.

    LINQ and / or Entity framework can be fine for generating TSQL seemlessly. I have seen LINQ developed in a matter of few minutes generate correct TSQL for paging and sorting with user selection of records. It is awesome!

    I would recommend however that your DBAs provide just read-only access to the base tables by TSQL and stick with stored procedures for all the CRUD operations.

    Monday, July 12, 2010 7:54 AM