• 创建简单的WCF服务(转)


    1:基础知识介绍:

          和传统的分布式通信框架一样,WCF本质上提供一个跨进程、跨机器以致跨网络的服务调用。其中主要包括契约(Contract),绑定(Binding),地址(Address)。

         WCF的服务不能孤立地存在,需要寄宿于一个运行着的进程中,我们把承载WCF服务的进程称为宿主,为服务指定宿主的过程称为服务寄宿(Service Hosting)。在我们的计算服务应用中,采用了两种服务寄宿方式:通过自我寄宿(Self-Hosting)的方式创 建一个控制台应用作为服务的宿主(寄宿进程为Hosting.exe);通过IIS寄宿方式将服务寄宿于IIS中(寄宿进程为IIS的工作进行 W3wp.exe)。客户端通过另一个控制台应用模拟(进程为Client.exe)。下面主要是介绍自我寄宿方式创建服务宿主。

       2:创建标准的第一个WCF程序。

        首先创建一个空的解决方案:WCFCalculator

      

      右键点击解决方案选择Add->New Project并选择Console Application模板并选择名为项目名为Host(服务器端)

    同理:右键点击解决方案选择Add->New Project并选择Console Application模板并选择名为项目名为Client(客户端)

    右击项目Host,添加WCF Service,命名为Calculator

    r

    添加后,产生Calculator.cs,ICalculator.cs 以及app.config三个文件。

    在ICalculator.cs中写服务契约:

    namespace Host
    {
        
    // NOTE: If you change the interface name "ICalculator" here, you must also update the reference to "ICalculator" in App.config.
        [ServiceContract]
        
    public interface ICalculator
        {
            [OperationContract]
            
    double Add(double x, double y);//代表外部可以访问的服务
        }
    }


    然后Calculator.cs:

    namespace Host
    {
        
    // NOTE: If you change the class name "Calculator" here, you must also update the reference to "Calculator" in App.config.
        public class Calculator : ICalculator
        {
            
    public double Add(double x,double y)
            {
                
    return x + y;
            }
        }
    }

    在App.config中,将默认的baseAddress改成简短的baseAddress="http://localhost:8732/Calculator/"
    然后通过自我寄宿的方式寄宿服务
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;

    namespace Host
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    using (ServiceHost host = new ServiceHost(typeof(Host.Calculator)))
                {
                    host.Open();
                    Console.Write(
    "服务已启动");
                    Console.ReadKey();
                    host.Close();
                }
            }
        }


    编译并生成Host.exe文件。这样服务已经成功寄宿在宿主端。
    在WCFCalculator\Host\bin\Debug目录下启动Host.exe文件。【在添加service Refenrence过程中Host.EXE不能关闭】
    在创建的Client项目中,右键点击Client项目并选择Add Service Reference...
    在Address的TextBox里面输入服务器的地址http://localhost:8732/Calculator/,并点击Go,建立CalculatorWCF服务引用;

     这样,就可以在客户端进行WCF服务访问。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    namespace Client
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                CalculatorWCF.CalculatorClient client 
    = new Client.CalculatorWCF.CalculatorClient();
                
    double result = client.Add(1.03.0);
                
                Console.WriteLine(
    "调用WCF服务后的结果为:"+result.ToString());
                Console.ReadKey();
            }
        }
    }

    这样客户端就可以访问服务器端的服务。运行结果:


    转自:http://www.cnblogs.com/ttltry-air/archive/2010/09/03/1817327.html

  • 相关阅读:
    angularjs基础——控制器
    angularjs基础——变量绑定
    mysql 小数处理
    centos无法联网解决方法
    mysql 按 in 顺序排序
    html5 file 自定义文件过滤
    淘宝、天猫装修工具
    MapGis如何实现WebGIS分布式大数据存储的
    CentOS
    PHP与Python哪个做网站产品好?
  • 原文地址:https://www.cnblogs.com/scottckt/p/1818267.html
Copyright © 2020-2023  润新知