Generating Prime Numbers with WCF using Visual Web Developer Express

Visual Web Developer
Create a new project with template “WCF Service Application” in Visual Web Developer Express.
Rename name IService.cs and Service.svc to IPrimeNumber.cs and PrimeNumber.cs

IPrimeNumber.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace PrimeNumberLib
{
    [ServiceContract]
    public interface IPrimeNumber
    {
        [OperationContract]
        bool isPrime(int n);

        [OperationContract]
        List<long> ListOfPrimes(int n);
    }
}

PrimeNumber.svc.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace PrimeNumberLib
{
    public class PrimeNumber : IPrimeNumber
    {
        public bool isPrime(int n)
        {
            if (n == 1)
                return false;
            else if (n < 4)
                return true;
            else if (n % 2 == 0)
                return false;
            else if (n < 9)
                return true;
            else if (n % 3 == 0)
                return false;
            else
            {
                int r = (int)Math.Floor(Math.Sqrt(n));
                int f = 5;
                while (f <= r)
                {
                    if (n % f == 0)
                        return false;
                    if (n % (f + 2) == 0)
                        return false;
                    f = f + 6;
                }
            }
            return true;
        }

        public List<long> ListOfPrimes(int n)
        {
            List<long> temp = new List<long>();
            int i = 2;

            for (; i <= n; i++)
            {
                if (isPrime(i))
                    temp.Add(i);
            }
            return temp;
        }
    }
}

Web.config

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true">
      <serviceActivations>
        <add relativeAddress="PrimeNumber.svc" service="PrimeNumberLib.PrimeNumber" />
      </serviceActivations>
    </serviceHostingEnvironment>
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>

The highlighted is the modification needed to tell IIS to look for PrimeNumber service

Right click on PrimeNumber.svc and select “View in Browser” to start IIS Express

Create PrimeNumber Proxy
Run

SvcUtil.exe /language:cs /out:GeneratedProxy.cs /config:App.config http://localhost:53031/PrimeNumber.svc

My path

"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SvcUtil.exe" /language:cs /out:GeneratedProxy.cs /config:App.config http://localhost:53031/PrimeNumber.svc

2 files are generated.
1) GeneratedProxy.cs
2) App.config

Create PrimeNumber Client Test
Create new project in Visual C# with template “Console Application”. Be sure to add “System.ServiceModel”, “GeneratedProxy.cs”, and “App.Config”.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrimeNumberTest
{
    class Program
    {
        static void Main(string[] args)
        {
            PrimeNumberClient c = new PrimeNumberClient();
            long[] l = c.ListOfPrimes(1000);
            foreach (long prime in l)
                Console.WriteLine(prime);
            c.Close();
        }
    }
}

Generating Prime Numbers with WCF

Create PrimeNumber library
Create new project in Visual C# with template “Class Library”. Be sure to add “System.ServiceModel” to References. PrimeNumberLib.dll is produced.
IPrimeNumber.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace PrimeNumberLib
{
    [ServiceContract]
    public interface IPrimeNumber
    {
        [OperationContract]
        bool isPrime(int n);

        [OperationContract]
        List<long> ListOfPrimes(int n);
    }
}

PrimeNumber.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrimeNumberLib
{
    public class PrimeNumber : IPrimeNumber
    {
        public bool isPrime(int n)
        {
            if (n == 1)
                return false;
            else if (n < 4)
                return true;
            else if (n % 2 == 0)
                return false;
            else if (n < 9)
                return true;
            else if (n % 3 == 0)
                return false;
            else
            {
                int r = (int)Math.Floor(Math.Sqrt(n));
                int f = 5;
                while (f <= r)
                {
                    if (n % f == 0)
                        return false;
                    if (n % (f + 2) == 0)
                        return false;
                    f = f + 6;
                }
            }
            return true;
        }

        public List<long> ListOfPrimes(int n)
        {
            List<long> temp = new List<long>();
            int i = 2;

            for (; i <= n; i++)
            {
                if (isPrime(i))
                    temp.Add(i);
            }
            return temp;
        }
    }
}

Create PrimeNumber Host (Web Server)
Create new project in Visual C# with template “Console Application”. Be sure to add “System.ServiceModel” and “PrimeNumberLib.dll” to References. http://localhost:8000/PrimeNumber/ should show a webpage on how to test the service.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using PrimeNumberLib;

namespace PrimeNumberHost
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseAddress = new Uri("http://localhost:8000/PrimeNumber/");

            ServiceHost selfHost = new ServiceHost(typeof(PrimeNumber), baseAddress);
            try
            {
                selfHost.AddServiceEndpoint(typeof(IPrimeNumber), new WSHttpBinding(), "PrimeNumberService");

                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                selfHost.Open();
                Console.WriteLine("The service is ready.");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                selfHost.Close();
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }
    }
}

Create PrimeNumber Proxy
Run

SvcUtil.exe /language:cs /out:GeneratedProxy.cs /config:App.config http://localhost:8000/PrimeNumber/

My path

"C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SvcUtil.exe" /language:cs /out:GeneratedProxy.cs /config:App.config http://localhost:8000/PrimeNumber/

2 files are generated.
1) GeneratedProxy.cs
2) App.config

Create PrimeNumber Client Test
Create new project in Visual C# with template “Console Application”. Be sure to add “System.ServiceModel”, “GeneratedProxy.cs”, and “App.Config”. Be sure that “PrimeNumber Host” is running.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PrimeNumberTest
{
    class Program
    {
        static void Main(string[] args)
        {
            PrimeNumberClient c = new PrimeNumberClient();
            long[] l = c.ListOfPrimes(1000);
            foreach (long prime in l)
                Console.WriteLine(prime);
            c.Close();
        }
    }
}