那天有个小孩教我WCF【1/31】

栏目: ASP.NET · 发布时间: 6年前

内容简介:那天有个小孩教我WCF【1/31】

WCF整体系统学习,一周2016-11-11 -2016-11-17          开发环境vs2015  .net framework 4.0    触屏WPF教程暂时搁置

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

啥契约,来说都是一个接口,在C#中,接口拓展一个类的行为,添加一个统一风格,到处收儿子。

服务,数据,消息

我想花费31天,系统学习WCF,跟上脚步,修炼 内功

DEMO1

那天有个小孩教我WCF【1/31】

WCF的项目一般分为  客户端,服务端,启动服务端的工具(控制台,服务,winform,wpf等,简称Host),一般来说,最好是winform,因为简单,或者好看的话,用wpf,ayui开发客户端快而且好看。

用cs程序,可以做个wcf的服务管理器,也可以做个服务安装和卸载类似的管理工具。

增加IAYUIBuyService类库

主要引用,自己添加程序集引用

using System.Runtime.Serialization;

using System.ServiceModel;

OK,Contract这个单词是 契约的意思,就是你给客户端暴露的接口中,别人可以调用的方法名字。

新建接口

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

namespace IAYUIBuyService
{
    [ServiceContract]
    public interface IBuyService
    {
        [OperationContract]
        double GetPrice(int productId);
    }
}

在interface上,用ServiceContract标记,一个operation范围,

在 method上,对外暴露的操作用OperationContract标记,表示对外暴露。

元数据:就是特征数据,好比有个美女,脸上有个美人痣,那个痣可以是个 数据,是她的特征

那天有个小孩教我WCF【1/31】

提取元数据,先用手工  svcutil.exe

位置:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools

那下面有好几个版本,看你用的WCF版本了,我自己初学用4.0也就是8.0A的文件夹

VS2015,菜单栏:  工具-外部工具

那天有个小孩教我WCF【1/31】

下面勾选如下,点击确定

那天有个小孩教我WCF【1/31】

然后,编译那个类库

接着

那天有个小孩教我WCF【1/31】

点击那个工具,参数输入F:\SoftMgr\WCF\AYOrderSystem\IAYUIBuyService\bin\Debug\IAYUIBuyService.dll

那天有个小孩教我WCF【1/31】

那天有个小孩教我WCF【1/31】

我不喜欢很官方的介绍每个文件

第1个文件,wsdl文档,包含对xsd的引用,消息定义、端口类型和服务操作的信息,客户端知道后,根据这个生成服务端的代理文件,然后就可以智能提示地方式提示代码

第2个文件,方法特性的xsd Schema

第3个文件,简单刀net类型的标准XSD Schema

用notepad++打开  1文件

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <xsd:schema targetNamespace="http://tempuri.org/Imports">
      <xsd:import namespace="http://tempuri.org/" />
      <xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" />
    </xsd:schema>
  </wsdl:types>
  <wsdl:message name="IBuyService_GetPrice_InputMessage">
    <wsdl:part name="parameters" element="tns:GetPrice" />
  </wsdl:message>
  <wsdl:message name="IBuyService_GetPrice_OutputMessage">
    <wsdl:part name="parameters" element="tns:GetPriceResponse" />
  </wsdl:message>
  <wsdl:portType name="IBuyService">
    <wsdl:operation name="GetPrice">
      <wsdl:input wsaw:Action="http://tempuri.org/IBuyService/GetPrice" message="tns:IBuyService_GetPrice_InputMessage" />
      <wsdl:output wsaw:Action="http://tempuri.org/IBuyService/GetPriceResponse" message="tns:IBuyService_GetPrice_OutputMessage" />
    </wsdl:operation>
  </wsdl:portType>
  <wsdl:binding name="DefaultBinding_IBuyService" type="tns:IBuyService">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <wsdl:operation name="GetPrice">
      <soap:operation soapAction="http://tempuri.org/IBuyService/GetPrice" style="document" />
      <wsdl:input>
        <soap:body use="literal" />
      </wsdl:input>
      <wsdl:output>
        <soap:body use="literal" />
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>
</wsdl:definitions>

打开2文件

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://tempuri.org/" elementFormDefault="qualified" targetNamespace="http://tempuri.org/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="GetPrice">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" name="productId" type="xs:int" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="GetPriceResponse">
    <xs:complexType>
      <xs:sequence>
        <xs:element minOccurs="0" name="GetPriceResult" type="xs:double" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

打开3文件

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:tns="http://schemas.microsoft.com/2003/10/Serialization/" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://schemas.microsoft.com/2003/10/Serialization/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="anyType" nillable="true" type="xs:anyType" />
  <xs:element name="anyURI" nillable="true" type="xs:anyURI" />
  <xs:element name="base64Binary" nillable="true" type="xs:base64Binary" />
  <xs:element name="boolean" nillable="true" type="xs:boolean" />
  <xs:element name="byte" nillable="true" type="xs:byte" />
  <xs:element name="dateTime" nillable="true" type="xs:dateTime" />
  <xs:element name="decimal" nillable="true" type="xs:decimal" />
  <xs:element name="double" nillable="true" type="xs:double" />
  <xs:element name="float" nillable="true" type="xs:float" />
  <xs:element name="int" nillable="true" type="xs:int" />
  <xs:element name="long" nillable="true" type="xs:long" />
  <xs:element name="QName" nillable="true" type="xs:QName" />
  <xs:element name="short" nillable="true" type="xs:short" />
  <xs:element name="string" nillable="true" type="xs:string" />
  <xs:element name="unsignedByte" nillable="true" type="xs:unsignedByte" />
  <xs:element name="unsignedInt" nillable="true" type="xs:unsignedInt" />
  <xs:element name="unsignedLong" nillable="true" type="xs:unsignedLong" />
  <xs:element name="unsignedShort" nillable="true" type="xs:unsignedShort" />
  <xs:element name="char" nillable="true" type="tns:char" />
  <xs:simpleType name="char">
    <xs:restriction base="xs:int" />
  </xs:simpleType>
  <xs:element name="duration" nillable="true" type="tns:duration" />
  <xs:simpleType name="duration">
    <xs:restriction base="xs:duration">
      <xs:pattern value="\-?P(\d*D)?(T(\d*H)?(\d*M)?(\d*(\.\d*)?S)?)?" />
      <xs:minInclusive value="-P10675199DT2H48M5.4775808S" />
      <xs:maxInclusive value="P10675199DT2H48M5.4775807S" />
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="guid" nillable="true" type="tns:guid" />
  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[\da-fA-F]{8}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{4}-[\da-fA-F]{12}" />
    </xs:restriction>
  </xs:simpleType>
  <xs:attribute name="FactoryType" type="xs:QName" />
  <xs:attribute name="Id" type="xs:ID" />
  <xs:attribute name="Ref" type="xs:IDREF" />
</xs:schema>

文件1和文件2生成,都跟源码有关,查看关系

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

WSDL            =>         接口名称+方法名称+输入消息/输出消息

那天有个小孩教我WCF【1/31】

端口类型       =>        接口名字        operation就是方法名字

那天有个小孩教我WCF【1/31】

WCF就是根据这些特殊标记生成wsdl文档的。

服务实现

新建类库AYUIBuyService,新建类 BuyService

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

namespace AYUIBuyService
{
    /// <summary>
    /// AYUI产品购买服务
    /// 2016-11-13 23:08:32
    /// </summary>
    public class BuyService : IBuyService
    {
        public double GetPrice(int productId)
        {
            switch (productId)
            {
                case 1:
                    return 10;
                case 2:
                    return 20;
                case 3:
                    return 30;
                case 4:
                    return 40;
                default:
                    return 100;
            }
        }
    }
}

服务启动需要的服务,也算是管理服务,也叫宿主

新建 AYUIServiceHost 控制台吧

作为一个宿主,需要引用需要管理的  接口(服务)和对应的实现,还有那2个 命名空间的dll

using System.Runtime.Serialization;

using System.ServiceModel;

基础模板

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

namespace AYUIServiceHost
{
    class Program
    {

        static void Main(string[] args)
        {
            Console.WriteLine("AYUI服务启动中...");
            try
            {
                
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }



            Console.WriteLine("服务启动成功");
            Console.ReadLine();
        }
    }
}

此时运行还是错误的

那天有个小孩教我WCF【1/31】

添加配置文件 App.config

那天有个小孩教我WCF【1/31】

新建服务,然后点击浏览,找到那个实现层  dll

那天有个小孩教我WCF【1/31】

然后单击

那天有个小孩教我WCF【1/31】

点击下一步

那天有个小孩教我WCF【1/31】

点击下一步

那天有个小孩教我WCF【1/31】

这几种协议,我们后面讲区别,保持默认用Http

那天有个小孩教我WCF【1/31】

下一步,这一步是暴露给客户端,发现服务的地址,名字随便取的。端口号取没被占用的。

那天有个小孩教我WCF【1/31】

点击下一步

那天有个小孩教我WCF【1/31】

结果:

那天有个小孩教我WCF【1/31】

在WCF面试题中,ABC代表啥问的多,如果要客户端知道服务,需要Address,Binding,Contract即可,他们组成了endpoint,终结点。

打开App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <services>
            <service name="AYUIBuyService.BuyService">
                <endpoint address="http://localhost:1111/AYUIBuyService" binding="basicHttpBinding"
                    bindingConfiguration="" contract="IAYUIBuyService.IBuyService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

将basicHttpBinding改成wsHttpBinding ,注意大小写

然后,编译运行host,报错

那天有个小孩教我WCF【1/31】

如果你是管理员方式打开VS2015,就不会有这个问题了。我们可以直接,管理员方式打开Debug下的 AYUIServiceHost.exe

那天有个小孩教我WCF【1/31】

公开元数据,就是那些Contract标记的东西,生成了dll,然后客户端根据这些东西生成wsdl等东西

继续编辑app.config

那天有个小孩教我WCF【1/31】

新建服务行为 OpenMetaDataBehavior,单击添加

那天有个小孩教我WCF【1/31】

这里有很多行为

那天有个小孩教我WCF【1/31】

我们选中serviceMetadata

那天有个小孩教我WCF【1/31】

单击这个 列表项,设置如下,取个地址名字,让vs可以下载wsdl

那天有个小孩教我WCF【1/31】

此时我们创建了1个服务的行为。但是还没有指定。单击服务,然后指定服务的行为

那天有个小孩教我WCF【1/31】

接下来,app.config显示如下

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="OpenMetaDataBehavior">
                    <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:1111/AYUIService/MEX" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="OpenMetaDataBehavior" name="AYUIBuyService.BuyService">
                <endpoint address="http://localhost:1111/AYUIBuyService" binding="wsHttpBinding"
                    bindingConfiguration="" contract="IAYUIBuyService.IBuyService" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

服务行为,有个OpenMetaDataBehavior,取了名字

然后服务指定了这个行为。

我已经管理员方式打开了VS2015,然后F5运行Host,接着打开浏览器,输入 http://localhost:1111/AYUIService/MEX

这个地址,可以获得对外暴露的元数据。

那天有个小孩教我WCF【1/31】

客户端生成使用代码            ->       一开始创建的那个控制台,毫无疑问也可以是其他的 使用技术,例如非常屌的 WPF技术

WCF两种方式,一种自带 工具 生成代理文件,一种是 手动代码方式的代理

我们将客户端AYOrderSystem设为启动项目,然后进入AYUIServiceHost的Debug文件夹下,管理员方式运行host的exe

成功后,

右键客户端的引用文件夹,添加服务引用

输入地址http://localhost:1111/AYUIService/MEX, 点击 转到

修改客户端类的命名空间 为 OpenAYUIService

那天有个小孩教我WCF【1/31】

点击高级

修改集合类型,  Array变为LinkedList

那天有个小孩教我WCF【1/31】

点击确定 2次,查看客户端生成的代码,点击显示所有文件

那天有个小孩教我WCF【1/31】

那天有个小孩教我WCF【1/31】

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
    </startup>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IBuyService" />
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1111/AYUIBuyService" binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_IBuyService" contract="OpenAYUIService.IBuyService"
                name="WSHttpBinding_IBuyService">
                <identity>
                    <userPrincipalName value="DESKTOP-G213197\ay" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

打开客户端,编写调用服务端的代码

那天有个小孩教我WCF【1/31】

编写完DEMO如下

    static void Main(string[] args)
        {
            using (OpenAYUIService.BuyServiceClient client = new OpenAYUIService.BuyServiceClient())
            {
                Console.WriteLine(client.GetPrice(2));
            }
            Console.ReadLine();
        }

那天有个小孩教我WCF【1/31】

因为我服务端是另起的DEBUG下手动启动的,不是在VS启动的,vs只是生成。

到此我们完成了WCF的入门学习,后面慢慢加知识 学习。

服务端是随便部署到多台服务器的。或者单台,多个端口号OK了。

====================www.ayjs.net       杨洋    wpfui.com        ayui      ay  aaronyang=======请不要转载谢谢了。=========

推荐您阅读更多有关于“WCF,”的文章


以上所述就是小编给大家介绍的《那天有个小孩教我WCF【1/31】》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

你必须知道的213个C语言问题

你必须知道的213个C语言问题

范立锋、李世欣 / 人民邮电出版社 / 2010-6 / 45.00元

《你必须知道的213个C语言问题》精选了213个在C语言程序设计中经常遇到的问题,目的是帮助读者解决在C语言学习和开发中遇到的实际困难,提高读者学习和开发的效率。这些问题涵盖了C语言与软件开发、C语言基础、编译预处理、字符串、函数、键盘操作、文件、目录和磁盘、数组、指针和结构、DOS服务和BIOS服务、日期和时间、重定向I/O和进程命令、C语言开发常见错误及程序调试等内容,均是作者经过充分的调研,......一起来看看 《你必须知道的213个C语言问题》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换