简述wcf应用

一.新建wcf

如下图:wcf可以简历俩种形式

1.库文件,就是一个类库文件,可以用windows服务或控制台开启。

2.服务应用程序,可以直接IIS上面发布。

二.库文件自动生成的类

接口类

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

using System.Text;

namespace WCFLib
{
    using System.ServiceModel;

    /*
     WCF服务的特点:
     * 1、在接口上必须贴上  [ServiceContract]标签来标记这个接口是一个wcf的服务
     * 2、在这个接口中的方法上贴上  [OperationContract] 标记这个方法是一个wcf的服务方法,可以被外界调用
     */
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
     
    }
}
View Code

实现类

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

namespace WCFLib
{
    /// <summary>
    /// WCF服务的实现类,它是用来处理wcf服务方法的逻辑代码的
    /// </summary>
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("你输入了: {0}", value);
        }
    }
}
View Code

配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- 部署服务库项目时,必须将配置文件的内容添加到 
  主机的 app.config 文件中。System.Configuration 不支持库的配置文件。-->
  <system.serviceModel>
    <!-- services节点负责存放所有的WCF服务的-->
    <services>
      <!-- name 一定是配置为当前服务实现类的全名称 契约 (合同)-->
      <service name="WCFLib.Service1">
        <host>
          <baseAddresses>
            <!-- 表示当前Service1这个服务的地址,被服务器来监听-->
            <add baseAddress = "http://localhost:8733/Design_Time_Addresses/WCFLib/Service1/" />
          </baseAddresses>
        </host>
        <!-- 终结点,也是供服务器来监听,
         address="" :表示当前监听的地址使用上面host配置节点中的baseAddress中的地址
         如果这个地址不为空,优先使用这个地址
           binding="basicHttpBinding" :表示客户端与wcf服务的通讯方式基于基本的http模式
          basicHttpsBinding: 表示表示客户端与wcf服务的通讯方式基于基本的https模式, 数据传输更加安全,但是速度要慢一些
          contract="WCFLib.IService1" :表示当前wcf服务Service1所对应的节点是谁
        -->
        <endpoint address="" binding="basicHttpBinding" contract="WCFLib.IService1">
        </endpoint>
       
        <!-- 元数据交换终结点供相应的服务用于向客户端做自我介绍。
        类似于Webservices的元数据,客户端将来生成的代理类就是按照这个元数据中的描述信息来生成的--> 
        <!-- 此终结点不使用安全绑定,应在部署前确保其安全或将其删除-->
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>


    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- 为避免泄漏元数据信息,
          请在部署前将以下值设置为 false -->
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
          <!-- 要接收故障异常详细信息以进行调试,
          请将以下值设置为 true。在部署前设置为 false 
            以避免泄漏异常信息-->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>
View Code

控制台托管的代码

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

namespace WCF服务的自托管
{
    using System.ServiceModel;
    using WCFLib;

    class Program
    {
        static void Main(string[] args)
        {
            //开启托管MenusMgr这个wcf的服务
            //如果ServiceHost() 的第二个参数不填写的话,自动去查找当前运行项目中的配置文件
            //所以要记得在当前项目的app.config中配置好wcf服务库的节点
            using (ServiceHost host = new ServiceHost(typeof(MenusMgr)))
            {
                //开启监听,等待客户端的连接
                host.Open();

                Console.WriteLine("服务已经开启");
                Console.ReadKey();
            }
        }
    }
}
View Code

三.客户端调用的方式

引用--添加服务引用--输入地址OK

四.引用好服务调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Client
{
    using Client.WcfServer;
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            MyServerClient my = new MyServerClient();
            string str = my.GetPerson("张三");
            this.TextBox1.Text = str;
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/zhuyapeng/p/6748503.html