static void WcfTest() { int threadCount = 4; int port = 9999; int count = 10000; int errorCount = 0; WcfHost <IHello, Hello> wcfHost = new WcfHost <IHello, Hello>(); wcfHost.StartHost(); IHello client = WcfClient.GetService <IHello>("http://127.0.0.1:14725"); client.SayHello("Hello"); Stopwatch watch = new Stopwatch(); List <Task> tasks = new List <Task>(); watch.Start(); LoopHelper.Loop(threadCount, () => { tasks.Add(Task.Run(() => { LoopHelper.Loop(count, index => { var msg = client.SayHello("Hello" + index); //Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss:ffffff")}:{msg}"); }); })); }); Task.WaitAll(tasks.ToArray()); watch.Stop(); Console.WriteLine($"并发数:{threadCount},运行:{count}次,每次耗时:{(double)watch.ElapsedMilliseconds / count}ms"); }
static void WcfTest() { int count = int.MaxValue; WcfHost <IHello, Hello> wcfHost = new WcfHost <IHello, Hello>(); wcfHost.StartHost(); IHello client = WcfClient.GetService <IHello>("http://127.0.0.1:14725"); client.SayHello("Hello"); Stopwatch watch = new Stopwatch(); watch.Start(); LoopHelper.Loop(1, () => { Task.Run(() => { LoopHelper.Loop(count, index => { var msg = client.SayHello("Hello" + index); Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss:ffffff")}:{msg}"); }); }).Wait(); }); watch.Stop(); Console.WriteLine($"每次耗时:{(double)watch.ElapsedMilliseconds / count}ms"); }
private void SetupWcf() { // you can have many WCF services here // ... IBulkRules syncService = new BulkRulesService(...); _bulkRulesWcfHost = new WcfHost(syncService, "BulkRulesService"); _bulkRulesWcfHost.Start(); }
static void Main(string[] args) { WcfHost <MyService, IMyService> wcfHost = new WcfHost <MyService, IMyService>("http://localhost:14725", "http://localhost:14725/mex"); wcfHost.HandleHostOpened = new Action <object, EventArgs>((obj, tar) => { Console.WriteLine("服务已启动!"); }); wcfHost.StartHost(); while (Console.ReadLine() != "quit") { } }
private void SetupWcf() { _logHelper.StartTimerFor("Wcf services"); IRule ruleService = new RuleService(_nesper); _ruleWcfHost = new WcfHost(ruleService, "RuleService"); _ruleWcfHost.Start(); _rulesMgr = new BulkRulesManager(_nesper); IBulkRules syncService = new BulkRulesService(_rulesMgr); _bulkRulesWcfHost = new WcfHost(syncService, "BulkRulesService"); _bulkRulesWcfHost.Start(); _logHelper.StopAndLogTime(); }
static void Main(string[] args) { //创建Wcf服务对象,泛型参数Service为实现类,IService为服务接口 //第一个参数baseUrl为服务基地址(必须为真实地址) //第二个参数httpGetUrl为服务引用地址(必须为真实地址),也就是客户端添加服务引用时用的地址 WcfHost <Service, IService> wcfHost = new WcfHost <Service, IService>("http://localhost:14725", "http://localhost:14725/mex"); //当Wcf服务开启后执行的事件 wcfHost.HandleHostOpened = new Action <object, EventArgs>((obj, tar) => { Console.WriteLine("服务已启动!"); }); //开始Wcf服务 wcfHost.StartHost(); while (Console.ReadLine() != "quit") { } }
public void NormalGroupAddTest() { XmlDocumentGroup xmlDocumentGroup = XmlHelper.GetXmlDocumentGroup(@".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.xml"); UploadDocumentGroup uploadDocumentGroup = XmlHelper.GetUploadDocumentGroup(xmlDocumentGroup, @".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.zip"); using (ShimsContext.Create()) { ShimDateTime.NowGet = () => new DateTime(2000, 1, 1); CrawlerLog log = new CrawlerLog(); DocumentGroup documentGroup = new DocumentGroup(); IRepository fakeRepository = new StubIRepository { GetIdentifierString = (identifier) => { return string.Empty; }, GetOrCreateCrawlerIdString = (crawlerName) => { return 1; }, AddDocumentGroupDocumentGroup = (newDocumentGroup) => { documentGroup = newDocumentGroup; }, AddNewLogCrawlerLog = (crawlerLog) => { log = crawlerLog; } }; WcfHost eu = new WcfHost(fakeRepository); var msgActual = eu.UploadFile(uploadDocumentGroup); // ReturnMessage Assert.AreEqual("Ok", msgActual, "Service return message."); // AddDocumentGroup Assert.AreEqual(xmlDocumentGroup.Identifier, documentGroup.Identifier, "Identifier"); Assert.AreEqual(1, documentGroup.CrawlerId, "CrawlerId"); Assert.AreEqual(0, documentGroup.Operation, "Operation"); Assert.AreEqual(xmlDocumentGroup.Document.Count, documentGroup.Documents.Count, "DocumentsCount"); // LogAsserts Assert.AreEqual(xmlDocumentGroup.Identifier, log.Identifier, "IdentifierLog"); Assert.AreEqual(true, log.IsSuccess, "IsSuccess"); Assert.AreEqual(new DateTime(2000, 1, 1), log.LogDate, "LogDate"); Assert.AreEqual(xmlDocumentGroup.Crawler, log.CrawlerName, "CrawlerNameLog"); Assert.AreEqual(0, log.Operation, "OperationLog"); } }
/// <summary> /// 注册服务 /// 注:指定端口 /// </summary> /// <typeparam name="IService">服务接口</typeparam> /// <typeparam name="Service">服务实现</typeparam> public void RegisterService <IService, Service>(int port) where Service : BaseWcfMSService, IBaseWcfMSService, IService where IService : IBaseWcfMSService { string ipAddress = string.IsNullOrEmpty(_ipAddress) ? IpHelper.GetLocalIp() : _ipAddress; string serviceName = typeof(IService).Name; string cacheKey = BuildCacheKey(serviceName); string serviceUrl = $"http://{ipAddress}:{port}/{serviceName}"; WcfHost <IService, Service> newHost = null; if (!_openSecurity) { newHost = new WcfHost <IService, Service>(serviceUrl); } else { if (string.IsNullOrEmpty(_userName) || string.IsNullOrEmpty(_password)) { newHost = new WcfHost <IService, Service>(serviceUrl, true); } else { newHost = new WcfHost <IService, Service>(serviceUrl, true) { UserName = _userName, Password = _password } }; } bool succcess = newHost.StartHost(); if (!succcess) { throw new Exception($"注册服务:{serviceName}到:{serviceUrl}失败!"); } HttpRuntime.Cache[Guid.NewGuid().ToString()] = newHost; //更新服务地址缓存 var urls = _redisCache.GetCache <List <string> >(cacheKey); if (urls == null) { urls = new List <string>(); } if (!urls.Contains(serviceUrl)) { urls.Add(serviceUrl); } _redisCache.SetCache(cacheKey, urls); //更新该项目所有服务 string allServicesNamesCacheKey = BuildCacheKey("allServices"); List <string> serviceNames = _redisCache.GetCache <List <string> >(allServicesNamesCacheKey); if (serviceNames == null) { serviceNames = new List <string>(); } if (!serviceNames.Contains(serviceName)) { serviceNames.Add(serviceName); } _redisCache.SetCache(allServicesNamesCacheKey, serviceNames); } #endregion }
public void StartListening() { WcfHost.Open(new TimeSpan(0, 0, 10)); }
public void IdentifierGroupDeleteTestNull() { XmlDocumentGroup xmlDocumentGroup = XmlHelper.GetXmlDocumentGroup(@".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.xml"); xmlDocumentGroup.Operation = Operation.Del; UploadDocumentGroup uploadDocumentGroup = XmlHelper.GetUploadDocumentGroup(xmlDocumentGroup, @".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.zip"); uploadDocumentGroup.Data = null; using (ShimsContext.Create()) { ShimDateTime.NowGet = () => new DateTime(2000, 1, 1); string ident = string.Empty; CrawlerLog log = new CrawlerLog(); DocumentGroup documentGroup = new DocumentGroup(); IRepository fakeRepository = new StubIRepository { GetIdentifierString = (identifier) => { return string.Empty; }, GetOrCreateCrawlerIdString = (crawlerName) => { return 1; }, AddDocumentGroupDocumentGroup = (newDocumentGroup) => { documentGroup = newDocumentGroup; }, DeleteDocumentGroupString = (identifier) => { ident = identifier; return 1; }, AddNewLogCrawlerLog = (crawlerLog) => { log = crawlerLog; } }; WcfHost eu = new WcfHost(fakeRepository); var msgActual = eu.UploadFile(uploadDocumentGroup); string msgExpected = "Error: Validation Exception - No document idenfier: " + xmlDocumentGroup.Identifier + " .You can't use Operation: Del"; // ReturnMessage Assert.AreEqual(msgExpected, msgActual, "Service return message."); // LogAsserts Assert.AreEqual(xmlDocumentGroup.Identifier, log.Identifier, "IdentifierLog"); Assert.AreEqual(false, log.IsSuccess, "IsSuccess"); Assert.AreEqual(new DateTime(2000, 1, 1), log.LogDate, "LogDate"); Assert.AreEqual(xmlDocumentGroup.Crawler, log.CrawlerName, "CrawlerNameLog"); Assert.AreEqual(2, log.Operation, "OperationLog"); } }
public void ZipXmlValidationTestNull() { XmlDocumentGroup xmlDocumentGroup = XmlHelper.GetXmlDocumentGroup(@".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.xml"); UploadDocumentGroup uploadDocumentGroup = XmlHelper.GetUploadDocumentGroup(xmlDocumentGroup, @".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.zip"); uploadDocumentGroup.Data = null; using (ShimsContext.Create()) { ShimDateTime.NowGet = () => new DateTime(2000, 1, 1); CrawlerLog log = new CrawlerLog(); IRepository fakeRepository = new StubIRepository { GetIdentifierString = (identifier) => { return string.Empty; }, AddNewLogCrawlerLog = (crawlerLog) => { log = crawlerLog; } }; WcfHost eu = new WcfHost(fakeRepository); var msgActual = eu.UploadFile(uploadDocumentGroup); string msgExpected = "Error: Validation Exception - ZipData length is zero or ZipData is NULL, document idenfier: " + xmlDocumentGroup.Identifier + " and Operation: Add"; Assert.AreEqual(msgExpected, msgActual, "Service return message."); Assert.AreEqual(false, log.IsSuccess, "IsSuccess"); Assert.AreEqual(new DateTime(2000, 1, 1), log.LogDate, "LogDate"); } }
public void FormatXmlValidationTestInvalid() { XmlDocumentGroup xmlDocumentGroup = XmlHelper.GetXmlDocumentGroup(@".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.xml"); xmlDocumentGroup.Format = "application/badapplication"; UploadDocumentGroup uploadDocumentGroup = XmlHelper.GetUploadDocumentGroup(xmlDocumentGroup, @".\DataContent\NormalContent\cc54a49bf188f994899cb1c954bb795f.zip"); using (ShimsContext.Create()) { ShimDateTime.NowGet = () => new DateTime(2000, 1, 1); CrawlerLog log = new CrawlerLog(); IRepository fakeRepository = new StubIRepository { GetIdentifierString = (identifier) => { return string.Empty; }, AddNewLogCrawlerLog = (crawlerLog) => { log = crawlerLog; } }; WcfHost eu = new WcfHost(fakeRepository); var msgActual = eu.UploadFile(uploadDocumentGroup); string msgExpected = "Error: Validation Exception - The 'format' attribute has an invalid value according to its data type.\r\n"; Assert.AreEqual(msgExpected, msgActual, "Service return message."); Assert.AreEqual(false, log.IsSuccess, "IsSuccess"); Assert.AreEqual(new DateTime(2000, 1, 1), log.LogDate, "LogDate"); } }
public void close() { WcfHost.stopHost(); }