zoukankan      html  css  js  c++  java
  • Hello WCF WCF应用起步

    1. 构建服务层

        新建一个空白解决方案,增加一个项目用来存放WCF服务-HelloWCF.Service

        确保该项目引用System.Model

        1) 构建服务契约

        新建一个接口

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

    namespace HelloWCF.Service
    {
        [ServiceContract]
        interface IHelloWorldService
        {
            [OperationContract]
            String GetMessage(String name);
        }
    }

        2)根据服务契约实现服务

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

    namespace HelloWCF.Service
    {
        public class HelloWorldService : IHelloWorldService
        {
            public String GetMessage(String name)
            {
                return "Hello world from " + name + "!";
            }
        }
    }

     2. 寄宿服务于ASP.NET开发服务器

        右击解决方案,新建网站,选择一个空的网站

        给网站添加服务项目的引用

        打开网站属性页

        将使用动态端口设为false

        设置端口号, eg:8080

        给网站增加svc文件

        HelloWorldService.svc

       

    <%@ServiceHost Service="HelloWCF.Service.HelloWorldService"%>

     3. 新建客户端

       新建一个控制台项目用于客户端

       生成代理和配置文件

         在控制台使用svcutil.exe

         cd C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin

         C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin>svcutil.exe http://localhost:8080/HostDevServer/HelloWorldService.svc?wsdl /out:HelloWorldServiceRef.cs /config:app.config

       定制客户端应用程序

          生成文件:HelloWorldServiceRef.cs和app.config添加到Client项目

          给项目添加System.ServiceModel引用

          修改Program.cs,代码如下:

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

    namespace HelloWorldClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                HelloWorldServiceClient client = new HelloWorldServiceClient();
                Console.WriteLine(client.GetMessage("Mike Liu"));
                Console.ReadKey();
            }
        }
    }

       运行客户端应用程序

          Ctrl+F5启动HostDevServer

          右键Client项目,设为启动,Ctrl+F5启动

       运行结果:

           Hello World from David Gu

       把服务项目设为自动启动

           右键解决方案->属性->通用属性->启动项目->多启动项目

           HostDevServer: 开始执行不调试

           Client: 开始执行不调试

       启动:

           HostDevServer先启动,然后会自动启动Client

     

     

    技术改变世界
  • 相关阅读:
    面向对象三 组合 特殊成员
    面向对象二 成员 嵌套
    面向对象
    模块和包
    异常处理
    os模块和序列化模块
    常用模块一
    常用模块 re模块
    内置函数二
    Algs4-2.2.14归并有序的队列
  • 原文地址:https://www.cnblogs.com/davidgu/p/2384898.html
Copyright © 2011-2022 走看看