wcf 学习2:IIS托管服务

注:添加服务,契约等其他项目不在介绍,上一篇文章已介绍

1:创建WCF应用程序 (宿主)Host3_28

2:修改svc文件 <%@ ServiceHost Language="C#" Service="WCFService.CalcultorService" %>

3:修改配置文件

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="metadataCalcultorService">
          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 -->
          <serviceMetadata httpGetEnabled="true" httpGetUrl="WCFService/CalcultorService/metadata3_28" />
          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->
          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <services>
      <service behaviorConfiguration="metadataCalcultorService" name="WCFService.CalcultorService">
        <host>
          <baseAddresses>
            <!--基地址-->
            <add baseAddress="http://127.0.0.1/"/>
          </baseAddresses>
        </host>
        <!--如果基地址不为空 此处填写绝对地址,否则填相对地址-->
        <endpoint address="WCFService/CalcultorService" binding="wsHttpBinding" contract="WCFContract.ICalculator" >
        </endpoint>
      </service>
    </services>
  </system.serviceModel>

4:发布 部署在IIs中

5:客户端引用 添加引用

http://localhost:(此处填写IIS配置的端口号)/CalcultorService.svc/WCFService/CalcultorService/metadata3_28

6:客户端调用

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

namespace Client3_26
{
    class Program
    {
        static void Main(string[] args)
        {
            using (CalcultorServiceClient pro = new CalcultorServiceClient())
            {
                Console.WriteLine(pro.Add(1, 1));
                Console.Read();
            }
        }
    }
}

原文地址:https://www.cnblogs.com/wzq806341010/p/2987752.html