WCF简单案例

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>

  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值