zoukankan      html  css  js  c++  java
  • 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>
    

      

  • 相关阅读:
    Visifire正式版(v1.1)发布
    [转]PSP机能强大!已能模拟运行WINDOWS系统?
    在Silverlight+WCF中应用以角色为基础的安全模式(一)基础篇之角色为基础的安全模式简介 Virus
    C#的加密解密算法,包括Silverlight的MD5算法 Virus
    MMORPG programming in Silverlight Tutorial (10)Implement the sprite’s 2D animation (Part IV)
    Game Script: Rescue Bill Gates
    MMORPG programming in Silverlight Tutorial (9)KeyFrame Animation
    MMORPG programming in Silverlight Tutorial (5)Implement the sprite’s 2D animation (Part II)
    MMORPG programming in Silverlight Tutorial (7)Perfect animation
    MMORPG programming in Silverlight Tutorial (3)Animate the object (Part III)
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/3543212.html
Copyright © 2011-2022 走看看