using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
namespace Jia.WCFService.Contract
{
[ServiceContract]
public interface IDivide
{
[OperationContract]
int Divide(int x, int y);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Jia.WCFService.Contract;
namespace Jia.WCFService.Service
{
public class DivideService:IDivide
{
public int Divide(int x, int y)
{
return x/y;
}
}
}
Jia.WCFService.JiaHosting / Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Jia.WCFService.Service;
namespace Jia.WCFService.JiaHosting
{
class Program
{
static void Main(string[] args)
{
using(ServiceHost serviceHost = new ServiceHost(typeof(DivideService)) )
{
serviceHost.Open();
Console.WriteLine("serviceHost is listening via {0} ", serviceHost.Description.Name.ToString());
Console.Read();
}
}
}
}
Jia.WCFService.JiaHosting / App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="DivideServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="DivideServiceBehavior" name="Jia.WCFService.Service.DivideService">
<endpoint address="" binding="basicHttpBinding" contract="Jia.WCFService.Contract.IDivide">
</endpoint>
<endpoint address="" binding="netTcpBinding" contract="Jia.WCFService.Contract.IDivide">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:9900/Divide"/>
<add baseAddress="net.tcp://localhost:9901/Divide"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Jia.WCFService.Client / DivideClient.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Jia.WCFService.Contract;
namespace Jia.WCFService.Client
{
class DivideClient:ClientBase<IDivide>,IDivide
{
public DivideClient():base()
{
}
public DivideClient(string endpointConfigurationName)
:base(endpointConfigurationName)
{
}
public DivideClient(Binding binding, EndpointAddress address)
:base(binding,address)
{
}
public int Divide(int x,int y)
{
return this.Channel.Divide(x, y);
}
}
}
Jia.WCFService.Client / Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using Jia.WCFService.Contract;
namespace Jia.WCFService.Client
{
class Program
{
static void Main(string[] args)
{
try
{
InvokeServiceViaConfiguration();
}
catch (System.Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Read();
}
static void InvokeServiceViaConfiguration()
{
using(DivideClient client_http = new DivideClient("ViaHttp"))
{
using(DivideClient client_tcp = new DivideClient("ViaTcp"))
{
try
{
Console.WriteLine("x / y = {2} where x = {0} and y = {1}",18,6,client_http.Divide(18,6));
Console.Write("x / y = {2} where x = {0} and y = {1}", 18, 6, client_tcp.Divide(18, 6));
}
catch (System.Exception e)
{
}
}
}
}
}
}
Jia.WCFService.Client / App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint address="http://localhost:9900/Divide" binding="basicHttpBinding" contract="Jia.WCFService.Contract.IDivide" name="ViaHttp"></endpoint>
<endpoint address="net.tcp://localhost:9901/Divide" binding="netTcpBinding" contract="Jia.WCFService.Contract.IDivide" name="ViaTcp"></endpoint>
</client>
</system.serviceModel>
</configuration>