(30 Mins)A Simple WCF Test
2012-09-09 17:29 圣殿骑士 阅读(1758) 评论(0) 编辑 收藏 举报Task : Implement a WCF Service that contains a method that counts the number of words in a given text. The WCF Service will be released in 2 phases. For the phase 1 release, the WCF Service should satisfy the conditions in the Phase 1 Specifications. For the second release, the service should satisfy both the phase 1 and phase 2 specifications.
Phase 1 Specification:
•Definition of a word: In phase 1, a word is defined as a sequence of case-insensitive characters between ‘a’ and ‘z’ or between ‘A’ and Z. Any non-alphabetic character must be considered as a separator. The system however should be able to support different word formats (not just alphabetic), which may be defined in the next phase.
•Definition of word count : When counting words, the system should consider case-insensitive matching. For example, “THE” and “the” are considered to be the same word.
•For example : Given the text “THE quick brown fox jumped over|the-lazy{ broWn,moon”, the output of the wcf method should be something like
(“the”, 2), (“quick”,1), (“brown”,2), (“fox”,1), (“jumped”,1), (“over”,1), (“lazy”,1), (“moon”,1)
•The WCF Service will be used in an intranet settings.
•For the phase 1 release, the service will be used to process short text (only a few kilobytes).
Phase 2 Specification:
•Implement another method that returns the count of a specific word. If a word is missing from the input text, the return value should be zero. For example, given the text “THE quick brown fox jumped over|the-lazy{ brOwN moon”, searching for the word “brown” should return 2. Searching for the word “globalblue” on the other hand should return zero.
•Add support for Alphanumeric words.
•Add support for processing large texts ( a few megabytes)
Notes:
•When designing the solution, use your knowledge on good object oriented design practices as well as its implications on performance, code readability, testability and extensibility.
The solution as below:
ServiceLib => IHello.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; namespace ServiceLib { [ServiceContract(SessionMode = SessionMode.Required)] public interface IHello { [OperationContract(IsInitiating = true, IsTerminating = false)] Dictionary<string, int> GetDictionaryWords(string inputText, string pattern); [OperationContract(IsInitiating = false, IsTerminating = false)] int FindDictionaryWord(string inputText); } }
ServiceLib => Hello.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel; using System.Text.RegularExpressions; namespace ServiceLib { [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] public class Hello : IHello { Dictionary<string, int> wordsCount = new Dictionary<string, int>(); public Dictionary<string, int> GetDictionaryWords(string inputText, string pattern) { string[] words = null; words = Regex.Split(inputText, pattern, RegexOptions.IgnoreCase); for (int i = words.GetLowerBound(0); i <= words.GetUpperBound(0); i++) { string tempWords = words[i].ToString().ToLower(); if (wordsCount.ContainsKey(tempWords)) { wordsCount[tempWords] = wordsCount[tempWords] + 1; } else { wordsCount.Add(tempWords, 1); } } return wordsCount; } public int FindDictionaryWord(string inputText) { string tempWords = inputText.ToString().ToLower(); if (wordsCount.ContainsKey(tempWords)) { return wordsCount[tempWords]; } else { return 0; } } } }
ServicesHost =>Hello.svc
<%@ ServiceHost Language="C#" Debug="true" Service="ServiceLib.Hello" %>
ServicesHost =>Web.config
<?xml version="1.0"?> <configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="SessionManagementBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true"/> </behavior> </serviceBehaviors> </behaviors> <services> <service name="ServiceLib.Hello" behaviorConfiguration="SessionManagementBehavior"> <endpoint address="" binding="wsHttpBinding" contract="ServiceLib.IHello" bindingConfiguration="MtomBindingConfiguration"/> </service> </services> <bindings> <wsHttpBinding> <binding name="MtomBindingConfiguration" messageEncoding="Mtom" maxReceivedMessageSize="1073741824" receiveTimeout="00:10:00"> <!--maxArrayLength --> <readerQuotas maxArrayLength="1073741824" /> </binding> </wsHttpBinding> </bindings> </system.serviceModel> <system.web> <compilation debug="true"/> </system.web> </configuration>
Client => Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Client.AlphanumericServices; namespace Client { class Program { static void Main(string[] args) { AlphanumericServices.HelloClient serviceClient = new HelloClient(); string pattern = "[^\\w]+"; //string input = "THE quick brown fox jumped over|the-lazy{ broWn,moon"; //for (int i = 0; i < 10000; i++) //{ // input = input + " " + input; //} Console.WriteLine("Please input a string:"); string input = Console.ReadLine(); foreach (var pair in serviceClient.GetDictionaryWords(input.Trim(), pattern)) { Console.WriteLine("{0}, {1}", pair.Key, pair.Value); } string searchWord = Console.ReadLine(); Console.WriteLine(serviceClient.FindDictionaryWord(searchWord.Trim())); Console.ReadKey(); } } }
Client => app.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="MtomBindingConfiguration" messageEncoding="Mtom" sendTimeout="00:10:00"> <!--maxArrayLength--> <readerQuotas maxArrayLength="1073741824" /> </binding> </wsHttpBinding> </bindings> <client> <endpoint address="http://localhost:5119/Hello.svc" binding="wsHttpBinding" bindingConfiguration="MtomBindingConfiguration" contract="AlphanumericServices.IHello" name="WSHttpBinding_IHello"> <identity> <userPrincipalName value="VictorZeng-PC\Victor Zeng" /> </identity> </endpoint> </client> </system.serviceModel> </configuration>
The results as below:
作者:圣殿骑士
出处:http://www.cnblogs.com/KnightsWarrior/
关于作者:专注于微软平台项目架构、管理和企业解决方案。自认在面向对象, 面向服务以及微服务领域有一定的造诣,熟悉设计模式、TDD、极限编程、领域驱动、架构设计、敏捷开发和项目管理。现主要从事.NET/.NET Core, Go, JavaScript/TypeScript, Azure/AWS等云计算方面的项目开发、架构、管理和企业培训工作。如有问题或建议,请多多赐教!
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。如有问题,可以邮件:KnightsWarrior(at)msn(dot)com
微博:圣殿骑士微博 联系我,非常感谢。