.Net Framework 3.0 WCF 自宿主例子

.Net Framework 3.0是基于.Net 2.0,加上WPF,WCF,WWF封装而成的。
其中WCF是Windows Communication Foundation。
其实WCF做的贡献就是,把现有的所有分布式技术(如Web Services(ASMX), .Net Remoting, Enterprise Services, WS-*, MSMQ消息队列)统一起来,让开发分布式应用更一致,更简单。

以通信(Communiation)范围而论,它可以跨进程、跨机器、跨子网、企业网乃至于Internet;以宿主程序而论,可以以ASP.NET,EXE,WPF,Windows Forms,NT Service,COM+作为宿主(Host)。WCF可以支持的协议包括TCP,HTTP,跨进程以及自定义,安全模式则包括SAML,Kerberos,X509,用户/密码,自定义等多种标准与模式。

下面仅介绍托管应用程序宿主(自宿主)的方式:(例子是一个实现用户登录注册服务)
建立三个工程:
Client 使用WCF服务
Common 定义合同规范
Server 实现WCF服务
Common:
在WCF中,所有的服务都暴露合同。合同是一种描述服务所实现功能的平台中立的标准的方式。WCF定义了四种类型的合同:
  · 服务合同描述你可以在服务上执行哪些操作。
  · 数据合同定义哪些数据类型被传入和传出服务。WCF为内置类型定义隐式合同,例如int和string,但是你可以容易地为定制类型定义显式的选入式数据合同。
  · 错误合同定义哪些错误将被该服务所激发,以及该服务怎样处理错误信息和把如何把它们传播到客户端。
  · 消息合同允许服务直接与消息进行交互。消息合同可以被类型化或非类型化,并且有点类似于CLR中的迟绑定调用。不过,消息合同很少为SOA开发者所用。
1.服务合同:

using System.Collections.Generic;
using System.Text;
/**
 * ServiceContract,OperationContract 在System.ServiceModel.dll中
 * 
*/

using System.ServiceModel;
using Common.Entity;

namespace Common.Interface
{
    [ServiceContract]
    
public interface IUserService
    
{
        [OperationContract]
        OperationResult Login(
string userName, string password);

        [OperationContract]
        OperationResult Register(
string userName, string password);

        [OperationContract]
        OperationResult ModifyPassword(
string userName, string newPassword);
    }

}


2.数据合同:

using System;
using
 System.Collections.Generic;
using
 System.Text;
using
 System.Runtime.Serialization;

namespace
 Common.Entity
{
    [DataContract]
    
public class
 OperationResult
    
{
        
private
 ResultChoice result;
        
private string
 failReason;

        [DataMember]
        
public
 ResultChoice Result
        
{
            
get

            
{
                
return
 result;
            }

            
set
            
{
                result 
=
 value;
            }

        }


        [DataMember]
        
public string FailReason
        
{
            
get

            
{
                
return
 failReason;
            }

            
set
            
{
                failReason 
=
 value;
            }

        }

    }


    
public enum ResultChoice
    
{
        Succeed
=1
,
        Failed
=0

    }

}


3.错误合同:
//略

Server实现:

using System;
using
 System.Collections.Generic;
using
 System.Text;
using
 Common.Interface;
using
 Common.Entity;

namespace
 Server.DAL
{
    
public class
 UserService:IUserService
    
{
        
IUserService Members

    }

}

运行服务:

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using Server.DAL;

namespace Server
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            ServiceHost host 
= new ServiceHost(typeof(UserService));
            host.Open();
            Console.WriteLine(
"The service is ready.");
            Console.ReadLine();
            host.Close();
        }

    }

}


服务端App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<services>
      
<service name="Server.DAL.UserService">
        
<endpoint address="net.tcp://localhost:8001/UserService"
                   binding
="netTcpBinding"
           contract
="Common.Interface.IUserService" >
          
<!--指定服务的契约接口-->
        
</endpoint>
      
</service>
    
</services>
  
</system.serviceModel>
</configuration>


Client客户端:
客户端App.Config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
<system.serviceModel>
    
<client>
      
<endpoint name="UserServiceClient" address="net.tcp://localhost:8001/UserService"
                binding
="netTcpBinding"
                contract
="Common.Interface.IUserService">
      
</endpoint>
    
</client>
  
</system.serviceModel>
</configuration>


使用服务:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Client.Util;
using Common.Entity;
using Common.Interface;
using System.ServiceModel;

namespace Client
{
    
static class Program
    
{

        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main()
        
{
            OperationResult result 
= null;
            
using (ChannelFactory<IUserService> factory = new ChannelFactory<IUserService>("UserServiceClient"))
            
{
                IUserService service 
= factory.CreateChannel();
                result 
= service.Login("A""B");
                Console.WriteLine(result.FailReason);
            }

        }

    }

}

 

运行结果为:
The password of A is not B!!!

 

posted @ 2007-02-09 23:20  Stanley.Luo  阅读(2783)  评论(6编辑  收藏  举报