zoukankan      html  css  js  c++  java
  • WCF简单使用(分别部署在控制台和IIS上)

     

    WCF部署到控制台

    1.下面通过一个简单的服务示例来认识WCF
    1.新建项目,名称IBLL,解决方案名称WcfDemo,模板选择类库
    2.修改Class1.cs文件名称为 IUserInfoService.cs
    3.添加引用 System.ServiceModel
    4.修改IUserInfoService.cs代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace IBLL
    {
        [ServiceContract]
        public interface IUserInfoService
        {
            [OperationContract]
            int Add(int a, int b);
        }
    }
    复制代码

    我们定义了一个IUserInfoService接口,注意在接口上申明了ServiceContract特性,即服务契约,表明该接口是一个服务。方法上声明了OperationContract特性,表示该方法是IUserInfoService的一个服务方法,客户端可远程调用该方法

    5.新建项目,名称BLL,模板选择类库,修改Class1.cs文件名称为 UserInfoService.cs,引用项目IBLL,代码如下:

    复制代码
    using IBLL;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace BLL
    {
        public class UserInfoService : IUserInfoService
        {
            public int Add(int a, int b)
            {
                return a + b;
            }
        }
    }
    复制代码

    OK,到此我们的服务代码已经编写完成,下面我们必须为服务提供一个运行的宿主,通过该宿主程序来启动我们的服务。
    6.在同一解决方案下新建一个项目,名称为WcfHost,类型为控制台应用程序

    7.WcfHost项目中添加引用,引用项目IBLL,然后再添加引用:System.ServiceModel

    8.修改Program.cs代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.ServiceModel;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WcfHost
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(BLL.UserInfoService)))
                {
                    host.Open();
                    Console.WriteLine("服务已启动");
                    Console.ReadKey();
                    host.Close();
                }
            }
        }
    }
    复制代码

    以上,我们已经实现了服务以及为服务提供了一个运行宿主,即契约部分已经完成,下面我们为服务指定地址及绑定
    9.修改app.config内容如下:

    复制代码
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          
          <service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"><!--服务的对象-->
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8000/"/><!--服务的IP和端口号-->
              </baseAddresses>
            </host>
            <endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint><!--contract:服务契约-->
          </service>   
        </services>
        
        <behaviors>
          <serviceBehaviors>
            <behavior name="behaviorConfiguration">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    复制代码

    10.设置WcfHost项目为启动项目,启动调试。控制台上显示服务已启动后,打开浏览器输入服务地址:http://localhost:8000/ ,浏览器中会打开我们的服务页面,这表示我们的服务已经启动成功,客户端可通过该地址访问我们的服务了。
    下面,我们将创建一个客户端来访问我们的服务
    11.在同一解决方案下新建一个项目,名称为WcfClient,类型为控制台应用程序,添加服务引用地址:http://localhost:8000/,客户端代码如下:

    复制代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace WcfClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                ServiceReference1.UserInfoServiceClient client = new ServiceReference1.UserInfoServiceClient();
                Console.WriteLine(client.Add(3, 4));
                Console.ReadKey();
            }
        }
    }
    复制代码

    12.启动WcfHost,启动WcfClient,(记得找到bin/debug找.exe以管理员运行

    WCF部署到IIS

    1-5步服务代码已经编写相同,下面我们必须为服务提供一个IIS宿主,那么当IIS启动起来后,我们的Wcf服务就起来了。

    6.为WCF创建.svc文件(新建类,后缀改为.svc即可),仅仅包含一个ServiceHost指令,该指令具有一个必须的Service属性(该属性指明了相应的WCF服务的类型)和一些可选的属性,该.svc文件应放在Services项目的根目录下

    <%@ ServiceHost Service="BLL.UserInfoService" %>

    7.在根目录下创建一个Web.Config,将WCF的相应配置添加到配置文件中,与寄宿在上面控制台方式不同的是,在添加的终结点中无需指定地址,因为.svc所在的地址就是服务端地址,代码如下:(该配置放在configuration根节点下)

    复制代码
      <system.serviceModel><!--部署Wcf服务-->
        <services>
          <service name="BLL.UserInfoService" behaviorConfiguration="behaviorConfiguration"><!--服务的对象-->
          <endpoint address="" binding="basicHttpBinding" contract="IBLL.IUserInfoService"></endpoint><!--contract:服务契约-->
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="behaviorConfiguration">
              <serviceMetadata httpGetEnabled="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    复制代码

    好了,WCF服务端配置好了,下面配置客户端

    8.客户端配置,添加服务引用,地址填.svc所在的地址,如:http://localhost:8707/UserInfoService.svc,命名空间:WcfServiceReference

    添加完成后,会在服务端的Web.Config中自动添加下面代码

    复制代码
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding_IUserInfoService" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint address="http://localhost:8707/UserInfoService.svc"
            binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IUserInfoService"
            contract="WcfServiceReference.IUserInfoService" name="BasicHttpBinding_IUserInfoService" />
        </client>
      </system.serviceModel>
    复制代码

    9.在客户端调用服务端方法(在Home控制器下添加该方法)

    复制代码
            public ActionResult Add()
            {
                WcfServiceReference.UserInfoServiceClient client = new WcfServiceReference.UserInfoServiceClient();
                int sum = client.Add(3, 4);
                return Content(sum.ToString());
            }
    复制代码

     

     
    分类: .net
  • 相关阅读:
    给大家分享两款正在使用的reflector插件
    Win32汇编项目总结——猎杀潜航
    SQL Server2008 数据库误删除数据的恢复方法分享
    DataGridView中使DataGridViewComboBox可编辑
    将SQL数据库还原到某个时间点
    SQL Server 2005对海量数据处理
    SQL Server 2005对海量数据处理(1)
    ILDASM的使用
    QT简介以及配置
    Reflector插件介绍
  • 原文地址:https://www.cnblogs.com/ping-zhang/p/7999487.html
Copyright © 2011-2022 走看看