コード例 #1
0
ファイル: Form1.cs プロジェクト: xiaopotian1990/WCFDemo
        private void button1_Click(object sender, EventArgs e)
        {
            //客户端访问有多种方式,此处只显示一种
            //利用ChannelFactory的CreateChannel方法创建一个IData的代理对象,其中参数“DataService”就是刚才在App.config中定义的endpoint的名称
            var proxy = new ChannelFactory<Services.ICalculator>("DataService").CreateChannel();

            int result = proxy.Add(1, 2);
            double result2 = proxy.Add(1.1, 2.2);

            //用完后一定要关闭,因为服务端有最大连接数,不关闭会在一定时间内一直占着有效连接
            ((IChannel)proxy).Close();
        }
コード例 #2
0
		public void BufferedConnection ()
		{
			var host = new ServiceHost (typeof (Foo));
			var bindingsvc = new CustomBinding (new BinaryMessageEncodingBindingElement (), new TcpTransportBindingElement ());
			host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "net.tcp://localhost:37564/");
			host.Open (TimeSpan.FromSeconds (5));
			try {
				var bindingcli = new NetTcpBinding () { TransactionFlow = false };
				bindingcli.Security.Mode = SecurityMode.None;
				var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
				Assert.AreEqual (5, cli.Add (1, 4));
				Assert.AreEqual ("monkey science", cli.Join ("monkey", "science"));
			} finally {
				host.Close (TimeSpan.FromSeconds (5));
				var t = new TcpListener (37564);
				t.Start ();
				t.Stop ();
			}
			Assert.IsTrue (Foo.AddCalled, "#1");
			Assert.IsTrue (Foo.JoinCalled, "#2");
		}
コード例 #3
0
		// FIXME: To enable this, we must fix the "request-reply TCP channel must close the connection" issue.
		// It is strange, but the standalone test just works.
		public void SimpleRequestReplyStreamed () // sample[svc|cli]5.exe
		{
			ServiceHost host = new ServiceHost (typeof (Foo));
			NetTcpBinding bindingS = new NetTcpBinding ();
			bindingS.TransferMode = TransferMode.Streamed;
			bindingS.Security.Mode = SecurityMode.None;
			bindingS.ReceiveTimeout = TimeSpan.FromSeconds (5);
			bindingS.OpenTimeout = TimeSpan.FromSeconds (20);
			host.AddServiceEndpoint (typeof (IFoo),
				bindingS, new Uri ("net.tcp://localhost:37564"));
			host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
			host.Open ();

			try {
				for (int i = 0; i < 2; i++) {
					var bindingC = new NetTcpBinding ();
					bindingS.TransferMode = TransferMode.Streamed;
					bindingC.Security.Mode = SecurityMode.None;
					IFooChannel proxy = new ChannelFactory<IFooChannel> (bindingC, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
					proxy.Open ();
					try {
						Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
						Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
					} finally {
						proxy.Close ();
					}
				}
			} finally {
				host.Close ();
			}
		}
コード例 #4
0
		public void SimpleDuplexBuffered () // sample[svc|cli]4.exe
		{
			ServiceHost host = new ServiceHost (typeof (Foo));
			var bindingS = new CustomBinding (new BindingElement [] {
				new BinaryMessageEncodingBindingElement (),
				new TcpTransportBindingElement () });
			bindingS.ReceiveTimeout = TimeSpan.FromSeconds (5);
			bindingS.OpenTimeout = TimeSpan.FromSeconds (20);
			host.AddServiceEndpoint (typeof (IFoo),
				bindingS, new Uri ("net.tcp://localhost:37564"));
			host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
			host.Open ();

			try {
				for (int i = 0; i < 2; i++) {
					var bindingC = new NetTcpBinding ();
					bindingC.Security.Mode = SecurityMode.None;
					IFooChannel proxy = new ChannelFactory<IFooChannel> (bindingC, new EndpointAddress ("net.tcp://localhost:37564/")).CreateChannel ();
					proxy.Open ();
					try {
						Assert.AreEqual ("TEST FOR ECHO", proxy.Echo ("TEST FOR ECHO"), "#1");
						var sid = proxy.SessionId;
						Assert.AreEqual (3000, proxy.Add (1000, 2000), "#2");
						Assert.AreEqual (sid, proxy.SessionId, "#3");
					} finally {
						proxy.Close ();
					}
				}
			} catch (Exception ex) {
				Console.Error.WriteLine (ex);
			} finally {
				host.Close ();
			}
		}
コード例 #5
0
		public void ConnectionHttpTransport ()
		{
			var host = new ServiceHost (typeof (Foo));
			var bindingsvc = new CustomBinding (new BindingElement [] {
				new BinaryMessageEncodingBindingElement (),
				new HttpTransportBindingElement () });
			int port = NetworkHelpers.FindFreePort ();
			host.AddServiceEndpoint (typeof (IFoo), bindingsvc, "http://localhost:" + port + "/");
			host.Description.Behaviors.Find<ServiceBehaviorAttribute> ().IncludeExceptionDetailInFaults = true;
			host.Open (TimeSpan.FromSeconds (5));
			try {
				for (int i = 0; i < 2; i++) {
					var bindingcli = new CustomBinding (new BindingElement [] {
						new BinaryMessageEncodingBindingElement (),
						new HttpTransportBindingElement () });
					var cli = new ChannelFactory<IFooClient> (bindingcli, new EndpointAddress ("http://localhost:" + port + "/")).CreateChannel ();
					Assert.AreEqual ("test for echo", cli.Echo ("TEST FOR ECHO"), "#1");
					var sid = cli.SessionId;
					Assert.AreEqual (3000, cli.Add (1000, 2000), "#2");
					Assert.AreEqual (sid, cli.SessionId, "#3");
					cli.Close ();
				}
			} finally {
				host.Close (TimeSpan.FromSeconds (5));
				var t = new TcpListener (port);
				t.Start ();
				t.Stop ();
			}
		}