0

Scenario: asp mvc application: WebSite. WCF service: Service, with DoSmth() method. I call Service from the WebSite.

What is the proper way to call Service.DoSmth()? I can create it all time I need to use it, like this:

    using (var service = new ServiceClient()) {
        service.DoSmth();    
    }

Or I can add Service field to the controller class and create the Service in controller's constructor.

    private Service service;
    public MyController() {
        service = new ServiceClient();
    }

I want to know, whats the difference, regarding to sessions, because, if we create the Service in constructor, we create one long session. So what about session time-outs or something like this? And what are the benefits from the other points of views?

Another question is, where to call service.DoSmth()? I've read about mvc pattern, and I think, that the proper way due the pattern is to call it from models, because models should do the work, and controller is only a "manager", but I saw many examples, where people use models only like containers, to pass data from controller to view. So can somebody clarify it for me. .

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Michael
  • 2,356
  • 3
  • 21
  • 24

1 Answers1

0

In many cases, you want to reuse the same WCF Client Proxy, as this connection method yields the best performance. Reusing the same proxy can be particularly beneficial if you use security features, which have a high initial security negotiation cost. Note: you surely need to check the state of the client proxy before using.

In the event that reusing the same client proxy is not an option, then consider using a ChannelFactory proxy that uses caching.

The following links provide good information and some guidance regarding best practices: http://blogs.msdn.com/b/wenlong/archive/2007/10/27/performance-improvement-of-wcf-client-proxy-creation-and-best-practices.aspx
http://msdn.microsoft.com/en-us/library/aa738757.aspx

Seymour
  • 7,043
  • 12
  • 44
  • 51