using System; using System.Data; using System.Data.SqlClient; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Threading; // Service Interface namespace FishService.Contract { [ServiceContract] public interface IService { [OperationContract] string Ping(string name); [OperationContract] ArrayList getFish(string type); } } // Actual Service namespace FishService.Server { [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)] class ServiceImplementation : FishService.Contract.IService { public string Ping(string name) { Console.WriteLine("SERVER recieved: {0}", name); return "2" + name; } public ArrayList getFish(string type) { ArrayList response = new ArrayList(); try { string cs = "Data Source=MASTER\\MASTER;Initial Catalog=TylerHood;Integrated Security=True;Pooling=False"; SqlConnection cn = new SqlConnection(cs); SqlCommand cm = cn.CreateCommand(); cm.CommandText = @"SELECT InStock FROM [dbo].[Stock] WHERE FishName = 'Cod';"; // string str = "SELECT InStock FROM [dbo].[Stock] WHERE FishName = " + "\'" + type + "\'" + " SqlDataAdapter da = new SqlDataAdapter(); da.SelectCommand = cm; DataSet ds = new DataSet(); ds.Clear(); cn.Open(); da.Fill(ds, "Stock"); DataTable dt = ds.Tables["Stock"]; foreach (DataRow dr in dt.Rows) { response.Add(dr["InStock"].ToString()); } cn.Close(); } catch (Exception e) { Console.WriteLine("Error:" + e.Message); } finally { } return response; } } public class Program { private static AutoResetEvent stopFlag = new AutoResetEvent(false); public static void Main() { ServiceHost svh = new ServiceHost(typeof(ServiceImplementation)); svh.AddServiceEndpoint( typeof(FishService.Contract.IService), new NetTcpBinding(), "net.tcp://localhost:8000"); svh.Open(); stopFlag.WaitOne(); svh.Close(); } public static void Stop() { stopFlag.Set(); } } } // end of namespace fishservice.server //Client namespace FishStockClient { class Program { static void Main() { System.Threading.Thread hs = new System.Threading.Thread (FishService.Server.Program.Main); hs.IsBackground = true; hs.Start(); Thread.Sleep(1000); ChannelFactory<FishService.Contract.IService> scf; scf = new ChannelFactory<FishService.Contract.IService>(new NetTcpBinding(), "net.tcp://localhost:8000"); FishService.Contract.IService s; s = scf.CreateChannel(); while (true) { Console.Write("Type of Fish: "); string fish = Console.ReadLine(); if (fish == "") break; ArrayList la = new ArrayList(); la = s.getFish(fish); foreach (var item in la) { Console.WriteLine( item.ToString() ); } } //--- (s as ICommunicationObject).Close(); FishService.Server.Program.Stop(); hs.Join(); } } }// end of Fishstockclient
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)