110,534
社区成员
发帖
与我相关
我的任务
分享
// ------服务端WCF实现代码 CSDN 代码折叠的功能都没,fuck。--------------------------------------
NetTcpBinding netTcpBinding = new NetTcpBinding(SecurityMode.None, true)
{
MaxBufferPoolSize = 2147483647,//2g
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
SendTimeout = new TimeSpan(0, 0, 30),
ReceiveTimeout = new TimeSpan(20, 0, 10),
ReliableSession =
{
Enabled = true,
InactivityTimeout = new TimeSpan(20, 0, 10)
}
};
try
{
_host = new ServiceHost(typeof(Service.PositionService));
ServiceThrottlingBehavior throttlingBehavior =
_host.Description.Behaviors.Find<ServiceThrottlingBehavior>();
if (throttlingBehavior == null)
{
throttlingBehavior = new ServiceThrottlingBehavior { MaxConcurrentCalls = 3000, MaxConcurrentSessions = 3000 };
_host.Description.Behaviors.Add(throttlingBehavior);
}
else
{
throttlingBehavior.MaxConcurrentCalls = 3000;
throttlingBehavior.MaxConcurrentSessions = 3000;
}
//_host.Description.Endpoints
string strAddress = string.Format(@"net.tcp://{0}:" + Common.ConstParameters.WcfPort + "/PositionServices", _serverIp);
_host.AddServiceEndpoint(typeof(Service.IPositionService), netTcpBinding, strAddress);
_host.Open();
//-------客户端WCF初始化代码-------------------------------------------------------------------
private int InitWcfService(string ip)
{
try
{
NetTcpBinding netbinding = new NetTcpBinding(SecurityMode.None, true)
{
MaxBufferPoolSize = 2147483647,
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647,
SendTimeout = new TimeSpan(0, 0, 30),
ReceiveTimeout = new TimeSpan(20, 0, 0),
ReliableSession = { Enabled = true, InactivityTimeout = new TimeSpan(20, 0, 10) },
};
PositionService = ChannelFactory<IPositionService>.CreateChannel
(netbinding, new EndpointAddress("net.tcp://" + ip + ":" + Common.ConstParameters.WcfPort + "/PositionServices"));
}
catch (Exception ex)
{
return 0;
}
return 1;
}
//--------服务端接口实现代码------------------------------------------------------------------------
private void MainForm_Load(object sender, EventArgs e)
{
InitServerOperation();
//!--For Test
for (int i = 0; i < 2800; i++)
{
TaxiInfo taxiInfo = new TaxiInfo
{
PhoneNumber = i.ToString().PadLeft(12, '0')
};
TaxiList.Add(i.ToString(), taxiInfo);
}
}
//-------------
public List<Model.TaxiInfo> GetAllTaxiInfos()
{
lock (TaxiList.SyncRoot)
{
List<TaxiInfo> taxiInfos = new List<TaxiInfo>();
foreach (TaxiInfo taxiInfo in TaxiList.Values)
{
taxiInfos.Add(taxiInfo);
}
return taxiInfos;
}
}
//---客户端调用测试方法------------------------------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Stopwatch myWatch = new System.Diagnostics.Stopwatch();
myWatch.Start();
List<TaxiInfo> taxiInfos = PositionService.GetAllTaxiInfos();
myWatch.Stop();
Console.WriteLine("耗时:" + myWatch.ElapsedMilliseconds + "ms");
}
public List<TaxiInfo> GetAllTaxiInfos()
{
for (int i = 0; i < 30000; i++)
{
var taxi = new TaxiInfo { PhoneNumber = i.ToString().PadLeft(12, '0') };
_taxis.Add(taxi);
}
return _taxis;
}