## (30 Mins)A Simple WCF Test

2012-09-09 17:29 by 圣殿骑士, ... 阅读, ... 评论, 收藏, 编辑

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 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
{
}
}
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">
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ServiceLib.Hello" behaviorConfiguration="SessionManagementBehavior">
</service>
</services>
<bindings>
<wsHttpBinding>
<!--maxArrayLength -->
</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;
//}
foreach (var pair in serviceClient.GetDictionaryWords(input.Trim(), pattern))
{
Console.WriteLine("{0}, {1}",
pair.Key,
pair.Value);
}

Console.WriteLine(serviceClient.FindDictionaryWord(searchWord.Trim()));
}
}
}

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-->
</binding>
</wsHttpBinding>
</bindings>
<client>
</configuration>