1,定义接口层,引用System.ServiceModel
namespace Contracts
{
[ServiceContract(Name = "CalculatorService", Namespace = "http://www.test.com/")]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y);
[OperationContract]
double Subtract(double x, double y);
[OperationContract]
double Multiply(double x, double y);
[OperationContract]
double Divide(double x, double y);
}
}
2,实现接口层,增加接口层引用
namespace Services
{
public class CalculatorService : ICalculator
{
public double Add(double x, double y)
{
return x + y;
}
public double Subtract(double x, double y)
{
return x - y;
}
public double Multiply(double x, double y)
{
return x * y;
}
public double Divide(double x, double y)
{
return x / y;
}
}
}
3,创建wcf项目,增加配置信息
<system.serviceModel>
<services>
<service name="Services.CalculatorService" behaviorConfiguration="SingleBehavior">
<endpoint binding="basicHttpBinding" bindingConfiguration="SingleBinding" contract="Contracts.ICalculator"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SingleBehavior">
<serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="SingleBinding" closeTimeout="00:10:00"
openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
Service.svc中指定Service
<%@ ServiceHost Language="C#" Debug="true" Service="Services.CalculatorService" %>
4,创建服务端
namespace Client
{
class Program
{
static void Main(string[] args)
{
var c=new Client();
var testvalue= c.Add(1, 2);
Console.WriteLine(testvalue);
}
}
public class Client : ClientBase<ICalculator>, ICalculator
{
public double Add(double x, double y)
{
return this.Channel.Add(x, y);
}
public double Subtract(double x, double y)
{
return this.Channel.Subtract(x, y);
}
public double Multiply(double x, double y)
{
return this.Channel.Multiply(x, y);
}
public double Divide(double x, double y)
{
return this.Channel.Divide(x, y);
}
}
}
配置文件编写
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<client>
<endpoint address="http://wcf.test.com/Service.svc" binding="basicHttpBinding" bindingConfiguration="SingleBinding" contract="Contracts.ICalculator" name="SingleBinding" />
</client>
<bindings>
<basicHttpBinding>
<binding name="SingleBinding" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" sendTimeout="00:10:00" receiveTimeout="00:10:00" >
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>