从ASP MVC应用程序调用WCF服务

时间:2023-02-06 20:31:25

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

场景:asp mvc应用程序:WebSite。 WCF服务:服务,使用DoSmth()方法。我从WebSite呼叫服务。

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

调用Service.DoSmth()的正确方法是什么?我可以在我需要使用它时创建它,如下所示:

    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.

或者我可以将Service字段添加到控制器类,并在控制器的构造函数中创建服务。

    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. .

另一个问题是,在哪里调用service.DoSmth()?我已经读过有关mvc模式的内容,我认为,由于模式的正确方法是从模型中调用它,因为模型应该完成工作,而控制器只是一个“管理器”,但我看到很多例子,人们在哪里仅使用像容器一样的模型,将数据从控制器传递到视图。有人可以为我澄清一下。 。

1 个解决方案

#1


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.

在许多情况下,您希望重用相同的WCF客户端代理,因为此连接方法可以产生最佳性能。如果使用具有高初始安全协商成本的安全功能,则重用相同的代理会特别有用。注意:在使用之前,您确实需要检查客户端代理的状态。

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

如果重用相同的客户端代理不是一个选项,那么考虑使用使用缓存的ChannelFactory代理。

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

以下链接提供了有关最佳实践的良好信息和一些指导: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

#1


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.

在许多情况下,您希望重用相同的WCF客户端代理,因为此连接方法可以产生最佳性能。如果使用具有高初始安全协商成本的安全功能,则重用相同的代理会特别有用。注意:在使用之前,您确实需要检查客户端代理的状态。

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

如果重用相同的客户端代理不是一个选项,那么考虑使用使用缓存的ChannelFactory代理。

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

以下链接提供了有关最佳实践的良好信息和一些指导: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