如何在wcf中配置一个封闭的连接 [英] how to dispose a closed connection in wcf

查看:59
本文介绍了如何在wcf中配置一个封闭的连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用wcf的一些示例,其中我启动主机和一些客户端。在服务中我记录服务对象的构造和销毁。



我可以看到,每当我连接一个新客户端时,都会创建一个新的服务对象。但每当我关闭客户端时,Service-object都不会被破坏。



有没有办法处置未使用的服务实例?



我的主人:



I'm trying some samples with wcf where i start the host and some clients. Inside the Service I log construction and destruction of the service objects.

I can see, whenever I connect a new client, a new Service-object is created. But whenever I close the client, the Service-object is NOT destructed.

Is there a way to dispose unused Service-instances ?

my Host:

namespace wcf_Test.Host
{
	public class Params
	{
		public string conn="net.pipe://localhost/myPipe";
		public NetNamedPipeBinding binding=new NetNamedPipeBinding();
	}

	class Program
	{
		public static void Main(string[] args)
		{
			Params cPar=new Params();
 			using (var host = new ServiceHost(typeof (MyService), new Uri(cPar.conn)))
            {
           
           		host.AddServiceEndpoint(typeof (Itest), cPar.binding,"");
          		host.Open();
          		Console.WriteLine("Service running. Press ENTER to stop.");
                        Console.ReadLine();
                ((IDisposable)host).Dispose();
            }
 			 Console.ReadLine();
 			 Console.WriteLine("This is the end");
            }
		}
	}
}





这是我的服务:





here is my Service:

namespace wcf_Test.Service
{
	[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
	public class MyService: Itest
	{
		public IMyCallback callback;
		public Timer timer;

		private int a=1;
		private static int sID=0;
		private int ID=0;

		
		
		public MyService()
		{
			ID=++sID;
			Console.WriteLine("Contructor ID=  {0}",ID);
			callback = OperationContext.Current.GetCallbackChannel<IMyCallback>();	
			
			timer = new Timer(1000);
            timer.Elapsed += OnTimerElapsed;
            timer.Enabled = true;
        }
		~MyService()
		{
			Console.WriteLine("Destructor ID= {0}",ID);	
		}
 
        void OnTimerElapsed(object sender, ElapsedEventArgs e)
        {
            callback.OnCallback();
        }
			
		
		public string getText()
		{		
			return string.Format("Hello from {0}, call number: {1}",ID, a++);
		}
		public void write (string text)
		{
			Console.WriteLine(text);
		}
		public void init()
		{}
	}
}





我试图插入截图但是没有选项可以这样做,所以这里是输出添加代码后的主机应用程序:



服务运行。按ENTER键停止。

Contructor ID = 1

Contructor ID = 2



//我开始了在这里关闭了两个客户...



这是结束

析构函数ID = 2

析构函数ID = 1





因为您可以看到服务对象的实例在应用程序关闭之前保持活动状态。我在按下键之前关闭了两个客户端!



I tried to insert a screenshot but there's no option to do so, so here is the Output of the Host application after i added your code:

Service running. Press ENTER to stop.
Contructor ID= 1
Contructor ID= 2

//I started and closed two clients here...

This is the end
Destructor ID= 2
Destructor ID= 1


as you can see the instances of the service-object remain active till the application is closed. I closed both clients before pressing the key!

推荐答案

当你使用使用语句时,所有的可以在这里使用的对象(在中使用块)必须实现 IDisposable [ ^ ]界面。因此,当您创建块时,.NET框架会自动调用Dispose()函数并释放附加到创建的变量和对象的任何资源(特别是中使用语句中使用的资源) )。



所以在你的代码中,一旦控件点击,主变量就会自动调用 Dispose()代码块的结束括号(} )。为了给你一个概述,你的代码类似于这段代码,



When you are using the using statement, all of the objects that can be used here (inside the using block) must implement the IDisposable[^] interface. So when you create the block, .NET framework automatically calls the Dispose() function and releases any resource attached to the variables and objects created (specially the one used in the using statement).

So in your code, host variable would get Dispose() called automatically, once the control hits the closing braces (}) of the code block. To give you an overview, your code is similar to this code,

// Create new instance
var host = new ServiceHost(typeof (MyService), new Uri(cPar.conn));

// Use it as connection
host.AddServiceEndpoint(typeof (Itest), cPar.binding,"");
host.Open();
Console.WriteLine("Service running. Press ENTER to stop.");
Console.ReadLine();

// Cast to IDisposable and dispose
((IDisposable)host).Dispose();





未执行的一件事是关闭() 自动,你必须自己打电话来关闭连接。此外, Dispose()不会调用析构函数,它只是释放其他对象和方法的资源;一旦对象被处理 .NET框架将调用析构函数 [ ^ ] 自动 ,你不应该调用析构函数。有关详细信息,请阅读此MSDN资源,关于使用声明 [ ^ ]和所有其他资源。



The one thing that doesn't get performed is Close() automatically, which you have to call yourself to close the connection. Also, Dispose() does not call the destructor, it just releases the resources for other objects and methods to use; once object is disposed .NET framework would call the destructor[^] automatically, you should not call destructors. For more on this please read this MSDN resource about using statement[^] and all other resources attached.


这篇关于如何在wcf中配置一个封闭的连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆