zoukankan      html  css  js  c++  java
  • 一个简单的WCF程序创建过程

    1.创建一个空白的解决方案.

    2.创建服务主机
      创建WEB宿主做服务主机,用来侦听客户端提交的请求,并创建用于处理那些请求的服务类的实例.

    <1>代码掩藏文件方式的服务类.
       创建成功后如下图所示意.

    App_Code下含有一个带事例代码的Service.cs文件.(本过程直接采用现有代码).Service.svc文件相当于WebService.asmx(本过程此处不做修改).Web.config中添加一行
    1<serviceMetadata httpGetEnabled="true"/>
    完整的Web.config文件内容如下.
     1<?xml version="1.0"?>
     2<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
     3  <system.serviceModel>
     4    <services>
     5      <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
     6      <service name="WCFServiceLibrary.service1" behaviorConfiguration="returnFaults">
     7                <endpoint contract="WCFServiceLibrary.IService1" binding="wsHttpBinding"/>
     8      </service>
     9    </services>
    10    <behaviors>
    11      <serviceBehaviors>
    12        <behavior name="returnFaults" >
    13          <serviceDebug includeExceptionDetailInFaults="true" />
    14          <serviceMetadata httpGetEnabled="true" />
    15        </behavior>
    16      </serviceBehaviors>
    17    </behaviors>
    18  </system.serviceModel>
    19  <system.web>
    20    <compilation debug="true"/>
    21  </system.web>
    22</configuration>

    <2>引用预编译程序集的服务类.

    添加对服务类的引用.


    采用此种方式,可以删除App_Code下的Service.cs文件.修改Service.svc文件内容如下:

    1<% @ServiceHost Language=C# Debug="true" Service="WCFServiceLibrary.service1" %>

    同时Web.confing同步修改如下:

     1<?xml version="1.0"?>
     2<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
     3    <system.serviceModel>
     4        <services>
     5            <!-- Before deployment, you should remove the returnFaults behavior configuration to avoid disclosing information in exception messages -->
     6            <service name="WCFServiceLibrary.service1" behaviorConfiguration="returnFaults">
     7                <endpoint contract="WCFServiceLibrary.IService1" binding="wsHttpBinding"/>
     8            </service>
     9        </services>
    10        <behaviors>
    11            <serviceBehaviors>
    12                <behavior name="returnFaults">
    13                    <serviceDebug includeExceptionDetailInFaults="true"/>
    14                    <serviceMetadata httpGetEnabled="true"/>
    15                </behavior>
    16            </serviceBehaviors>
    17        </behaviors>
    18    </system.serviceModel>
    19    <system.web>
    20        <compilation debug="true">
    21            <assemblies>
    22                <add assembly="System.Security, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    23                <add assembly="Microsoft.Transactions.Bridge, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    24                <add assembly="SMDiagnostics, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    25                <add assembly="System.IdentityModel.Selectors, Version=3.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    26                <add assembly="System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    27                <add assembly="System.Web.RegularExpressions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    28                <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    29                <add assembly="System.Messaging, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    30                <add assembly="System.ServiceProcess, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/></assemblies></compilation>
    31    </system.web>
    32</configuration>

    3.客户端配置和实现.
     创建控制台程序作为测试客户端程序.
     
     添加服务引用后如下.
     
    客户端做简单的如下编程.

     1using System;
     2using System.Collections.Generic;
     3using System.Text; 
     4
     5namespace WCFCon
     6{
     7    class Program
     8    {
     9        static void Main(string[] args)
    10        {
    11            localhost.MyServiceClient wcf = new WCFCon.localhost.MyServiceClient();
    12            Console.WriteLine("请输入数据:");
    13            string str = Console.ReadLine();
    14            Console.WriteLine("你输入的数据是:{0}", wcf.MyOperation1(str));
    15            localhost.DataContract1 da = new WCFCon.localhost.DataContract1();
    16            Console.WriteLine("请输入FirstName:");
    17            da.FirstName = Console.ReadLine();
    18            Console.WriteLine("请输入LastName:");
    19            da.LastName = Console.ReadLine();
    20            Console.WriteLine("FirstName+LastName={0}", wcf.MyOperation2(da));
    21        }

    22    }

    23}
    运行结果输出如下.

    至此,一个简单的WCF创建演示过程结束.其它详细请参阅MSDN.
  • 相关阅读:
    docker 入门(docker 镜像 、容器、仓库)
    windows 安装 docker
    关于go mod 的使用和goland 配置 go mod
    mac 安装docker
    vm 将宿主机文件夹 映射至 虚拟机
    centos 关于yum无法使用
    mac 安装 swoole 可能会出现的错误
    BZOJ3378:[USACO]MooFest 狂欢节(树状数组)
    BZOJ3110:[ZJOI2013]K大数查询(整体二分)
    BZOJ4170:极光(CDQ分治)
  • 原文地址:https://www.cnblogs.com/jiangshaofen/p/1019396.html
Copyright © 2011-2022 走看看