• (30 Mins)A Simple WCF Test


    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:

    image

    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:

    Results

  • 相关阅读:
    krpano--控制热点跳转到场景的指定视角
    bzoj 4237: 稻草人 -- CDQ分治
    bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
    bzoj 3545/3551: [ONTAK2010]Peaks -- 主席树,最小生成树,倍增
    bzoj 4627: [BeiJing2016]回转寿司 -- 权值线段树
    bzoj 1901: Zju2112 Dynamic Rankings -- 主席树,树状数组,哈希
    bzoj 3252: 攻略 -- 长链剖分+贪心
    bzoj 5055: 膜法师 -- 树状数组
    bzoj 1006: [HNOI2008]神奇的国度 -- 弦图(最大势算法)
    bzoj 1176: [Balkan2007]Mokia&&2683: 简单题 -- cdq分治
  • 原文地址:https://www.cnblogs.com/KnightsWarrior/p/30MinsWCFTest.html
Copyright © 2020-2023  润新知