zoukankan      html  css  js  c++  java
  • WCF入门详解

    具体文章地址如下:

    要解决的问题:

        WCF入门,创建一个DBServices程序。

    解决方法:

    1. 打开VS,创建一个空白的solution。然后依次创建下面三个项目:
    • Contracts:类库项目,添加System.ServiceModel的引用。
    • Services:类库项目,添加System.ServiceModel和对Contracts项目的引用。
    • Host:控制台项目,添加System.ServiceModel和对Contracts、Services项目的引用。
    • Client:控制台项目,添加System.ServiceModel和对Contracts、Services项目的引用。

    在项目上右键,Add Reference->选择.NET标签页,再找到System.ServiceModel 添加。

    效果如下图所示,并将Host项目设为启动项目。

    2.在Contracts项目中DBServices定义服务契约接口。

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace Contracts
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“DBServices”。
        public class DBServices : IDBServices
        {
            public void DoWork()
            {
                Console.WriteLine("WCF OK!");
            }
        }
    }

    3.在Contracts项目中实现IDBServices的接口。

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Serialization;
    using System.ServiceModel;
    using System.Text;
    
    namespace Contracts
    {
        // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IDBServices”。
        [ServiceContract]
        public interface IDBServices
        {
            [OperationContract]
            void DoWork();
        }
    }

    4.若不使用WCFService Configuration Editor生成配置文件,则在HOST中代码如下。

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Contracts;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    namespace Hosting
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(DBServices), new Uri("http://localhost:8732/DBServices/")))
                {
    
                    host.AddServiceEndpoint(typeof(IDBServices), new BasicHttpBinding(), "SVC");
                    if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                    {
                        ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                        behavior.HttpGetEnabled = true;
    
                        behavior.HttpGetUrl = new Uri("http://localhost:8732/DBServices/");
    
                        host.Description.Behaviors.Add(behavior);
    
    
                    }
    
    
                    host.Open();
                    Console.WriteLine("Start Your Service.");
                    Console.ReadKey();
                    host.Close();
                   
                    //host.Open();
                    //Console.WriteLine("Start Your Service.");
                    //Console.ReadKey();
                    //host.Close();
                    
    
                }
            }
        }
    }

    5.启动Host中的服务,也就是运行Host项目。出现下图效果,暂时先别关闭这个窗口。

    6.在你的浏览器地址栏中,输入http://localhost:8732/DBServices/出现效果如下:

    7.点击页面中的链接,出现WSDL格式的XML文件:

    8.经过上面这一系列,说明你的服务端已经OK了,现在进行客户端的配置。

    接着,在Client项目上右键,Add Service Reference. 接着如下图:(你前面的Host项目请保持运行状态)

    9,在Client端添加调用的Host服务的代码。代码里面IDBServices接口命名空间你可以在Client项目的Service Reference中找到,也就是你前一步输入的Namespace。

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    using System.ServiceModel;
    using System.ServiceModel.Description;
    using System.Threading;
    namespace Client
    {
        class Program
        {
            static void Main(string[] args)
            {
                //ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(DBServices.IDBServices)),
                //new BasicHttpBinding(), new EndpointAddress("http://localhost:8732/DBServices/"));
                //using (ChannelFactory<DBServices.IDBServices> factory = new ChannelFactory<DBServices.IDBServices>(httpEndpoint))
                //{
    
                //    DBServices.IDBServices service = factory.CreateChannel();
                //    service.DoWork();
    
                //}
                using (DBServices.DBServicesClient Client = new DBServices.DBServicesClient())
                {
                    while (true)
                    {
                        Client.DoWork();
                        Thread.Sleep(1000);
                    }
                }
            }
        }
    }

    以上两种调用方法都可以。

    10. OK,现在你再运行你的Client项目。控制台下面每秒会多出一行WCFOK,至此一个最简单的WCF程序完成了。

    11.如果第四部中在HOST主机上使用WCF Services Configration Edit,那么HOST中的代码将简单很多,如下:

    View Code
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Contracts;
    using System.ServiceModel;
    using System.ServiceModel.Description;
    namespace Hosting
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ServiceHost host = new ServiceHost(typeof(DBServices)))
                {
    
                    //host.AddServiceEndpoint(typeof(IDBServices), new BasicHttpBinding(), "SVC");
                    //if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                    //{
                    //    ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                    //    behavior.HttpGetEnabled = true;
    
                    //    behavior.HttpGetUrl = new Uri("http://localhost:8732/DBServices/");
    
                    //    host.Description.Behaviors.Add(behavior);
    
    
                    //}
    
    
                    //host.Open();
                    //Console.WriteLine("Start Your Service.");
                    //Console.ReadKey();
                    //host.Close();
    
                    host.Open();
                    Console.WriteLine("Start Your Service.");
                    Console.ReadKey();
                    host.Close();
                    
    
                }
            }
        }
    }

    下一章将介绍WCF Services Configuration Edit 的使用方法

  • 相关阅读:
    iOS 后台运行
    内存管理
    ios -晋级之路 如何去掉tableView多余行的横线
    ios 晋级之路- block深入
    iOS 晋级之路- 调用相机和相册的功能
    iOS开发 --定位
    学习笔记 ios开发 手势
    排序法系列-快速排序法
    计算文本的高度BoundingRectWithSize:options:context
    NSClassFromString的用法
  • 原文地址:https://www.cnblogs.com/nocoding/p/2881466.html
Copyright © 2011-2022 走看看