Below are the classes and interface to host a service
ICalculator.cs
CalculatorService.cs
using System;
Host.cs
using Interface;
Below are the classes and configuration to access the above hosted service.
App.config
<xml version="1.0" encoding="utf-8" ?>
Client.cs
using Interface;
ICalculator.cs
using System;
using System.ServiceModel;
namespace Interface
{
[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double num1, double num2);
[OperationContract]
double Subtract(double num1, double num2);
[OperationContract]
double Multiply(double num1, double num2);
[OperationContract]
double Devide(double num1, double num2);
}
}
CalculatorService.cs
using System;
namespace Service
{
public class CalculatorService : Interface.ICalculator
{
public double Add(double num1, double num2)
{
return num1 + num2;
}
public double Subtract(double num1, double num2)
{
return num1 - num2;
}
public double Multiply(double num1, double num2)
{
return num1 * num2;
}
public double Devide(double num1, double num2)
{
return num1 / num2;
}
}
}
Host.cs
using Interface;
using Service;
using System;
using System.ServiceModel;
namespace ConsoleHost
{
class Host
{
static void Main(string[] args)
{
//ServiceHost
host = new ServiceHost(typeof(CalculatorService));
ServiceHost host = new ServiceHost(typeof(CalculatorService), new Uri("http://localhost:8888/test"));
host.AddServiceEndpoint(typeof(ICalculator), new NetTcpBinding(), "net.tcp://localhost:8889/test");
//host.AddServiceEndpoint(typeof(ICalculator),
new WSHttpBinding(), "http://localhost:8888/test1");
host.AddDefaultEndpoints();
host.Open();
Console.WriteLine("Listening at
...");
int i = 0;
foreach (var ep in host.Description.Endpoints)
{
Console.WriteLine("({0}) A:
{1}, \tB: {2}, \tC: {3}", i, ep.Address,
ep.Binding.Name, ep.Contract.Name);
i++;
}
Console.WriteLine("Service is
running ... Press any key to stop service.");
Console.ReadKey();
host.Close();
}
}
}Below are the classes and configuration to access the above hosted service.
App.config
<xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<client>
<endpoint name="basicEP" address="http://localhost:8888/test" binding="basicHttpBinding" contract="Interface.ICalculator" />
<endpoint name="netTcpEP" address="net.tcp://localhost:8889/test" binding="netTcpBinding" contract="Interface.ICalculator" />
</client>
</system.serviceModel>
</configuration>Client.cs
using Interface;
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
namespace ConsoleClient
{
class Client
{
static void Main(string[] args)
{
/*
ChannelFactory
factory = new ChannelFactory();
factory.Endpoint.Address = new
EndpointAddress("http://localhost:8888/test");
factory.Endpoint.Binding = new
BasicHttpBinding();
factory.Endpoint.Contract =
ContractDescription.GetContract(typeof(ICalculator));
*/
ChannelFactory<ICalculator> factory = new ChannelFactory<ICalculator>("netTcpEP"); //provide endpoint name
defined in App.config
ICalculator proxy = factory.CreateChannel();
Console.WriteLine(proxy.Add(12, 22));
Console.ReadKey();
}
}
}
No comments:
Post a Comment