Below are the classes and interface to host a service
ICalc.cs
Calc.cs
Service.cs
Below is the class which have the code to access the service
Client.cs
ICalc.cs
using System;
using System.ServiceModel;
namespace ConsoleService
{
[ServiceContract]
interface ICalc
{
[OperationContract(AsyncPattern = true)]
IAsyncResult BeginAdd(int a, int b, AsyncCallback callback, object asyncState);
int EndAdd(IAsyncResult result);
[OperationContract]
int Sub(int a, int b);
}
}
Calc.cs
using System;
namespace ConsoleService
{
class Calc : ICalc
{
Func<int, int, int> add = (n1, n2) => n1 + n2;
public IAsyncResult BeginAdd(int a, int b, AsyncCallback callback, object asyncState)
{
return add.BeginInvoke(a, b, callback, asyncState);
}
public int EndAdd(IAsyncResult result)
{
return add.EndInvoke(result);
}
public int Sub(int a, int b)
{
return a - b;
}
}
}
Service.cs
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ConsoleService
{
class Service
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Calc), new Uri("http://localhost:8888/async"));
host.Description.Behaviors.Add(new ServiceMetadataBehavior() {
HttpGetEnabled = true });
host.Open();
Console.Title = "Async
Service";
Console.ReadKey();
}
}
}Below is the class which have the code to access the service
Client.cs
using System;
namespace ConsoleClient
{
class Client
{
static void Main(string[] args)
{
ServiceAsync.CalcClient client = new ServiceAsync.CalcClient();
Console.WriteLine(client.Add(2, 5));
Console.ReadKey();
}
}
}
No comments:
Post a Comment