none
Replace tcp binding with http binding in c# wcf service RRS feed

  • Question

  • I am newbie to wcf and I have a c# wcf service with tcp binding as shown. Need to replace the tcp binding with http binding. Please provide info on what type of http binding should be used (basichttp, wshttp, wsdualhttp). Also please point to code resources for creating http binding in c#.

        IPHostEntry ipHostInfo = Dns.GetHostByName(Dns.GetHostName());
                GetAvailablePortNumber();
                Log.Instance.Information("Currently Listening at address : " + ipHostInfo.AddressList[0].ToString() + "  on Port :" + textBoxPort);
                //Define base addresses so all endPoints can go under it
    
                Uri tcpAdrs = new Uri("net.tcp://" + ipHostInfo.AddressList[0].ToString() + ":" +
                    textBoxPort + "/service/");
    
                Uri httpAdrs = new Uri("http://" + ipHostInfo.AddressList[0].ToString() + ":" +
                    (int.Parse(textBoxPort.ToString()) + 1).ToString() + "/service/");
    
                Uri[] baseAdresses = { tcpAdrs, httpAdrs };
    
                host = new ServiceHost(typeof(RemoteDevice.ServiceLibrary.ServiceImplementation.DeviceInteractService), baseAdresses);
    
                
    
                NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true);
                //Updated: to enable file transefer of 64 MB
                tcpBinding.MaxBufferPoolSize = (int)67108864;
                tcpBinding.MaxBufferSize = 67108864;
                tcpBinding.MaxReceivedMessageSize = (int)67108864;
                tcpBinding.TransferMode = TransferMode.Buffered;
                tcpBinding.ReaderQuotas.MaxArrayLength = 67108864;
                tcpBinding.ReaderQuotas.MaxBytesPerRead = 67108864;
                tcpBinding.ReaderQuotas.MaxStringContentLength = 67108864;
    
    
                tcpBinding.MaxConnections = 100;
                //To maxmize MaxConnections you have to assign another port for mex endpoint
    
                //and configure ServiceThrottling as well
                ServiceThrottlingBehavior throttle;
                throttle = host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
                if (throttle == null)
                {
                    throttle = new ServiceThrottlingBehavior();
                    throttle.MaxConcurrentCalls = 100;
                    throttle.MaxConcurrentSessions = 50;
                    host.Description.Behaviors.Add(throttle);
                }
    
    
                //Enable reliable session and keep the connection alive for 20 hours.
                tcpBinding.ReceiveTimeout = new TimeSpan(20, 0, 0);
                tcpBinding.ReliableSession.Enabled = true;
                tcpBinding.ReliableSession.InactivityTimeout = new TimeSpan(20, 0, 10);
    
                host.AddServiceEndpoint(typeof(RemoteDevice.ServiceLibrary.ServiceContracts.IDeviceInteractService), tcpBinding, "tcp");
    
                //Define Metadata endPoint, So we can publish information about the service
                ServiceMetadataBehavior mBehave = new ServiceMetadataBehavior();
                host.Description.Behaviors.Add(mBehave);
    
                host.AddServiceEndpoint(typeof(IMetadataExchange),
                    MetadataExchangeBindings.CreateMexTcpBinding(),
                    "net.tcp://" + ipHostInfo.AddressList[0].ToString() + ":" +
                    (int.Parse(textBoxPort.ToString()) - 1).ToString() + "/service/mex");
    
                try
                {
                    host.Open();
                }
                catch (Exception ex)
                {
                    Log.Instance.Exception(ex, ex.StackTrace);
                    Console.WriteLine("Error: Facing issues in establishing the connection");
                }


    Wednesday, January 22, 2014 5:52 AM

Answers

  • Hi,

    I saw that you were using the following:

     NetTcpBinding tcpBinding = new NetTcpBinding(SecurityMode.None, true);

    So if you do not want to use the security, you can use the basicHttpBinding. By default the security for the basicHttpBinding is none, but for the wsHttpBinding, it needs to set security, if you do not set it, then it will throw error.

    And the address for the basicHttpBinding is http://...... or https://.....

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by ykbharat Thursday, January 23, 2014 7:11 AM
    Thursday, January 23, 2014 5:28 AM
    Moderator