public static void Test() { string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); var endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); ReplaceSerializerOperationBehavior.ReplaceSerializer(endpoint); host.Open(); Console.WriteLine("Host opened"); ChannelFactory <ITest> factory = new ChannelFactory <ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress)); ReplaceSerializerOperationBehavior.ReplaceSerializer(factory.Endpoint); ITest proxy = factory.CreateChannel(); Console.WriteLine(proxy.Echo("Hello")); Console.WriteLine(proxy.Echo(123.456)); Console.WriteLine(proxy.Echo(new Uri("http://tempuri.org"))); Console.WriteLine(proxy.Echo(new Person { Name = "John Doe", Age = 33 })); ((IClientChannel)proxy).Close(); factory.Close(); Console.Write("Press ENTER to close the host"); Console.ReadLine(); host.Close(); }
public static T GetProxy <T>(string bindingName, string urlAddress) { NetTcpBinding binding = new NetTcpBinding(); TimeSpan ts = new TimeSpan(0, 10, 0); binding.CloseTimeout = ts; binding.ListenBacklog = 500; binding.MaxBufferPoolSize = 1048576; binding.MaxBufferSize = 10485760; binding.MaxConnections = 500; binding.MaxReceivedMessageSize = 10485760; binding.Name = bindingName; binding.OpenTimeout = ts; XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas(); quotas.MaxArrayLength = int.MaxValue; quotas.MaxBytesPerRead = int.MaxValue; quotas.MaxDepth = int.MaxValue; quotas.MaxNameTableCharCount = int.MaxValue; quotas.MaxStringContentLength = int.MaxValue; binding.ReaderQuotas = quotas; binding.ReceiveTimeout = ts; binding.Security.Mode = SecurityMode.None; binding.SendTimeout = ts; EndpointAddress address = new EndpointAddress(urlAddress); ChannelFactory <T> channelFactory = new ChannelFactory <T>(binding, address); ReplaceSerializerOperationBehavior.ReplaceSerializer(channelFactory.Endpoint); T channel = channelFactory.CreateChannel(); return(channel); }
public static ServiceHost GetServiceHost <C, I>(string bindingName, string baseAddress) { ServiceHost serviceHost; NetTcpBinding binding = new NetTcpBinding(); TimeSpan ts = new TimeSpan(0, 10, 0); binding.CloseTimeout = ts; binding.ListenBacklog = 500; binding.MaxBufferPoolSize = 1048576; binding.MaxBufferSize = 10485760; binding.MaxConnections = 500; binding.MaxReceivedMessageSize = 10485760; binding.Name = bindingName; binding.OpenTimeout = ts; XmlDictionaryReaderQuotas quotas = new XmlDictionaryReaderQuotas(); quotas.MaxArrayLength = int.MaxValue; quotas.MaxBytesPerRead = int.MaxValue; quotas.MaxDepth = int.MaxValue; quotas.MaxNameTableCharCount = int.MaxValue; quotas.MaxStringContentLength = int.MaxValue; binding.ReaderQuotas = quotas; binding.ReceiveTimeout = ts; binding.Security.Mode = SecurityMode.None; binding.SendTimeout = ts; Uri uriBaseAddress = new Uri(baseAddress); serviceHost = new ServiceHost(typeof(C), uriBaseAddress); ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior(); metadataBehavior.HttpGetEnabled = false; serviceHost.Description.Behaviors.Add(metadataBehavior); serviceHost.AddServiceEndpoint(typeof(IMetadataExchange), binding, "mex"); //serviceHost.AddServiceEndpoint(typeof(I), binding, uriBaseAddress); var endpoint = serviceHost.AddServiceEndpoint(typeof(I), binding, uriBaseAddress); ReplaceSerializerOperationBehavior.ReplaceSerializer(endpoint); return(serviceHost); }