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();
}
}
}