代碼如下,請教各位前輩,為何 tcpBinding.TransferMode =TransferMode.Streamed;則啟動不了,彈出對話框,提示如下:
“協定需要會話,但是系結"NetTcpBinding"不支持它或者因配置不正確而無法支持它。
isServerRun = true;
//獲取本機IP
string ip = GetIP();
string tcpaddress = string.Format("net.tcp://{0}:{1}/", ip, ServerPort);
//設定netTCP協議
NetTcpBinding tcpBinding = new NetTcpBinding();
tcpBinding.MaxBufferPoolSize = 2147483647;
tcpBinding.MaxReceivedMessageSize = 2147483647;
tcpBinding.MaxBufferSize = 2147483647;
tcpBinding.ReceiveTimeout = TimeSpan.MaxValue;
tcpBinding.SendTimeout = TimeSpan.MaxValue;
//提供安全傳輸
tcpBinding.Security.Mode = SecurityMode.None;
tcpBinding.TransferMode = TransferMode.Buffered;
//需要提供證書
tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
//添加多個服務終結點
//使用指定的協定、系結和終結點地址將服務終結點添加到承載服務中
//netTcp協議終結點
//定義服務主機
ServiceHost host = new ServiceHost(typeof(MainServer), new Uri(tcpaddress));//typeof為型別別
host.AddServiceEndpoint(typeof(IMainServer), tcpBinding, tcpaddress);//typeof為介面型別
#region 添加行為
////元資料發布行為
ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
behavior.HttpGetEnabled = false;
//設定到Host中
host.Description.Behaviors.Add(behavior);
#endregion
//設定資料交換型別
host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
//啟動服務
host.Open();
SetToolStripStatusLabelMessage(this.toolStripStatusLabel1, string.Format("服務啟動成功,正在運行...{0}", tcpaddress), Color.Green);
}
catch (Exception ex)
{
SetToolStripStatusLabelMessage(this.toolStripStatusLabel1, "服務啟動失敗", Color.Red);
MessageBox.Show(ex.Message, "服務啟動失敗");
Environment.Exit(0);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/12982.html
標籤:C#
