一个WCF的HelloWorld程序

要解决的问题:

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

解决方法:

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

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

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

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

IHello
namespace Contracts
{

    [ServiceContract]
   public interface IHello
    {

        [OperationContract]
        void Hello();
        
    }
}

 

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

HelloWorld实现IHello
namespace Services
{
    public class HelloWorld : IHello
    {
        public void Hello()
        {
            Console.WriteLine(" Hello world");
 
        }
}

 

4.在Host项目为WCF的服务提供运行环境。

HostApp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Services;
using Contracts;
using System.ServiceModel.Description;

namespace Host
{
    /// <summary>
    /// This is a Host Console application
    /// </summary>
    public class HostApp
    {
        static void Main(string[] args)
        {
            /**
             * Create a host to provide service.
             * ServiceType is HelloWorld
             * BaseAddress is in localhost
             */
            ServiceHost host = new ServiceHost(typeof(HelloWorld), new Uri("http://localhost:8080/HelloService"));
            
            /**
             * Add an serviceEndpoint to this host
             * Implemented Contract is IHello
             * Binding pattern is BasicHttpBinding
             * Address  'SVC' is a relative address
             */
            host.AddServiceEndpoint(typeof(IHello), new BasicHttpBinding(),"SVC");
            if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
            {
                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                behavior.HttpGetEnabled = true;

                behavior.HttpGetUrl = new Uri("http://localhost:8080/HelloService");

                host.Description.Behaviors.Add(behavior);


            }

            /**
             * Once you add an endpoint to ServiceHost,it can create a EndpointListener for each ServiceEndpoint
             * ServiceDescription is involved in the creating process
             */
            host.Open();
            Console.WriteLine("Start Your Service.");
            Console.ReadKey();
            host.Close();
        }
    }
}

 

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

 

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

 

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

 

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

另开一个solution,新建一个名为Client的控制台程序(什么应用程序都行)。添加对System.ServiceModel的引用。

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

 

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

Client调用Hello服务
namespace Client
{
    public class ClientApp
    {
        static void Main(String[] args)
        {
           ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IHello)),
               new BasicHttpBinding(), new EndpointAddress("http://localhost:8080/HelloService/SVC"));
            using (ChannelFactory<IHello> factory = new ChannelFactory<IHello>(httpEndpoint))
             {
               
                IHello service = factory.CreateChannel();
                service.Hello();
              
            }

        }
    }
}

 

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

 

工作原理:

这个程序中的配置,都是用代码写的,实际中应该在config文件里写。虽然效果是一样的,但代码需要再编译,所以XML好点。

具体我还是下一篇再写吧,不然太多了。

 

 

posted @ 2012-08-26 10:21  阿凡迪  阅读(3850)  评论(13编辑  收藏  举报