• WCF简单实例[改进版]


    一 实例结构

    1)solution如下:

     

    2)dependency如下:

     

    *  Contracts 其实就是interface,用来实现service和client的松耦合;

    *  Services 是真正的对contracts实现;

    *  Hosting 用来宿主 services,是其能够被client访问;

    *  client 用来访问wcf service;

    二 contracts

    ICalculator.cs文件如下:

     using System.ServiceModel;

     
    namespace Artech.WcfServices.Contracts
     {
         [ServiceContract(Name
    ="CalculatorService", Namespace="http://www.artech.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);      
        } 
    }


    三 services

    CalculatorService.cs文件如下:

    using Artech.WcfServices.Contracts;

    namespace Artech.WcfServices.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;
            }
        }
    }

    四 hosting

    app.config文件如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      
    <system.serviceModel>
        
    <behaviors>
          
    <serviceBehaviors>
            
    <behavior name="metadataBehavior">
              
    <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8888/calculatorservice/metadata" />
            
    </behavior>
          
    </serviceBehaviors>
        
    </behaviors>
        
    <services>
          
    <service behaviorConfiguration="metadataBehavior" name="Artech.WcfServices.Services.CalculatorService">
            
    <endpoint address="http://localhost:8888/calculatorservice" binding="wsHttpBinding"  contract="Artech.WcfServices.Contracts.ICalculator" />
          
    </service>
        
    </services>
      
    </system.serviceModel>
    </configuration>

    Hosting.cs文件如下:

    using System;
    using System.ServiceModel;

    using Artech.WcfServices.Contracts;
    using Artech.WcfServices.Services;

    namespace Artech.WcfServices.Hosting
    {
       
    class Program
       {

           
    static void Main(string[] args)
           {
               
    using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
               {                
                   host.Opened 
    += delegate
                    {
                        Console.WriteLine(
    "CalculaorService已经启动,按任意键终止服务!");
                    };

                    host.Open();
                    Console.Read();
                }
            }
        }
    }

    五 client

    app.config文件如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <system.serviceModel>
    <client>
    <endpoint address="http://localhost:8888/calculatorservice" binding="wsHttpBinding"
    contract
    ="Artech.WcfServices.Contracts.ICalculator" name="calculatorservice" />
    </client>
    </system.serviceModel>
    </configuration>

    client.cs文件如下:

    using System;
    using System.ServiceModel;

    using Artech.WcfServices.Contracts;

    namespace Artech.WcfServices.Client
    {
       
    class Program
       {
           
    static void Main(string[] args)
           {
               
    using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
                {
                    ICalculator proxy 
    = channelFactory.CreateChannel();
                    
    using (proxy as IDisposable)
                    {
                        Console.WriteLine(
    "x + y = {2} when x = {0} and y = {1}"12, proxy.Add(12));
                        Console.WriteLine(
    "x - y = {2} when x = {0} and y = {1}"12, proxy.Subtract(12));
                        Console.WriteLine(
    "x * y = {2} when x = {0} and y = {1}"12, proxy.Multiply(12));
                        Console.WriteLine(
    "x / y = {2} when x = {0} and y = {1}"12, proxy.Divide(12));
                    }
                }
               Console.ReadLine();
            }
        }
    }

    参考:http://www.cnblogs.com/artech/archive/2007/02/26/656901.html


    作者:iTech
    微信公众号: cicdops
    出处:http://itech.cnblogs.com/
    github:https://github.com/cicdops/cicdops

  • 相关阅读:
    angularjs 表单验证
    (原)Eclipse Tomcat配置(2014.12.27——By小赞)
    Eclipse SVN插件安装与使用(2014.12.27——by小赞)
    Eclipse 下载与安装(2014.12.26——by小赞)
    MySQL开启远程链接(2014.12.12)
    360手机助手使用问题
    WORD文档的长串数字如何粘贴到excel
    EJB 总结学习(1)
    EJB (not bound)
    JQ绑定事件(1.9已经废除了live()等绑定事件方法,on()方法是官方推荐的绑定事件的一个方法)
  • 原文地址:https://www.cnblogs.com/itech/p/1849841.html
Copyright © 2020-2023  润新知