Beginning the journey of WCF, and look forward to future.

本文介绍了Windows通信基础(WCF)的基础知识,通过示例代码详细展示了如何不使用配置文件搭建简单的WCF服务,包括定义服务契约、实现服务、设置宿主及客户端连接,并进一步讲解了如何利用配置文件简化部署过程。
Recently, I have learned something about WCF techniques, and of course, I found it was excellent that I decided to learn deeper about it.

I hope someone interest it can communicate with me, or just post your idea or advices freely. The below is a sample of my testing of WCF for beginner( before go on, your need to learning the ABC concepts of WCF, the ABC is Address, Binding, Contract. The three factors needed by any Endpoint).

this sample is written without using config file, in order to simplify the demo and more easy to understand.

Contract --  Host -- Cilent

the contract is the basic thing to communicate within Host and Cilent.
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;

namespace  SayHello
{
    [ServiceContract]
    
public   interface  ISay
    {
        [OperationContract]
        
string  SayHello( string  yourName);
    }
    
public   class  Say:ISay
    {
        
#region  ISay Members

        
public   string  SayHello( string  yourName)
        {
            
return   string .Format( " Hello, {0}. Now is {1}. " , yourName, DateTime.Now);
        }

        
#endregion
    }
}

and then, you can construct your host now.

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;
using  SayHello;

namespace  HostConsole
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Console.WriteLine(
" Welcome to WCF " );
            
using  (ServiceHost host  =   new  ServiceHost( typeof (Say),  new  Uri( " net.tcp://localhost:8000/ " )))
            {
                host.AddServiceEndpoint(
typeof (ISay),  new  NetTcpBinding(),  " SayHello " );
                host.Open();
                Console.ReadLine();
            }
        }
    }
}

Please note that I'm using Tcp binding. The next is the Client.

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;
using  SayHello;

namespace  ClientConsole
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Console.WriteLine(
" Please input your name " );
            
string  yourName  =  Console.ReadLine();
            ChannelFactory
< ISay >  cf  =   new  ChannelFactory < ISay > ( new  NetTcpBinding(),  new  EndpointAddress( " net.tcp://localhost:8000/SayHello " ));
            ISay say 
=  cf.CreateChannel();
            Console.WriteLine(say.SayHello(yourName));
            Console.ReadLine();
        }
    }
}

the Client uses the ChannelFactory to create an Endpoint and connect to Host. The metadate is ISay an interface defined by me.

and the next chapter, I will use config file to config WCF instead of custom programming.

the Contract is the same, and the Host has a little changes.
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;
using  SayHello;

namespace  HostConsole
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Console.WriteLine(
" Welcome to WCF " );
            
using  (ServiceHost host  =   new  ServiceHost( typeof (Say)))
            {
                host.Open();
                Console.ReadLine();
            }
        }
    }
}

Please notice that hte host is using type of Say. You need add a new App.config file like,
<? xml version="1.0" encoding="utf-8"  ?>
< configuration >
    
< system.serviceModel >
        
< services >
            
< service  name ="SayHello.Say"  behaviorConfiguration ="MEXGET" >
                
< host >
                    
< baseAddresses >
                        
< add  baseAddress  ="http://localhost:8000/" />
                    
</ baseAddresses >
                
</ host >
                
< endpoint  address ="SayHello"  binding ="basicHttpBinding"  contract ="SayHello.ISay" ></ endpoint >
                
< endpoint  address  ="mex"  binding ="mexHttpBinding"  contract ="IMetadataExchange" ></ endpoint >
            
</ service >
        
</ services >
        
< behaviors >
            
< serviceBehaviors >
                
< behavior  name ="MEXGET" >
                    
< serviceMetadata  httpGetEnabled ="true" />
                
</ behavior >
            
</ serviceBehaviors >
        
</ behaviors >
    
</ system.serviceModel >
</ configuration >

In this config file, notice the service's name is [Namesapce].[ClassName] format, and it has a behavior which will enabled the httpGet for the client to get metadata, the "mex" is the Endpoint specified for geting metadata.

the Client is more easy to write, but need to add web reference using the URI mentioned above http://localhost:8000/.
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.ServiceModel;
using  SayHello;

namespace  ClientConsole
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            Console.WriteLine(
" Please input your name " );
            
string  yourName  =  Console.ReadLine();

            
// ChannelFactory<ISay> cf = new ChannelFactory<ISay>(new BasicHttpBinding(), new EndpointAddress(" http://localhost :8000/SayHello1"));
            
// ISay say = cf.CreateChannel();

            SaySR.SayClient say 
=   new  ClientConsole.SaySR.SayClient();
            
            
            Console.WriteLine(say.SayHello(yourName));
            Console.ReadLine();
        }
    }
}

转载于:https://www.cnblogs.com/jerryhong/archive/2009/08/14/1545863.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值