Net Framework 3.0 WCF 自宿主例子

None.gif .Net Framework  3.0  WCF 自宿主例子 
None.gif.Net Framework 
3 .0是基于.Net  2.0 ,加上WPF,WCF,WWF封装而成的。
None.gif其中WCF是Windows Communication Foundation。
None.gif其实WCF做的贡献就是,把现有的所有分布式技术(如Web Services(ASMX), .Net Remoting, Enterprise Services, WS
-* , MSMQ消息队列)统一起来,让开发分布式应用更一致,更简单。
None.gif
None.gif以通信(Communiation)范围而论,它可以跨进程、跨机器、跨子网、企业网乃至于Internet;以宿主程序而论,可以以ASP.NET,EXE,WPF,Windows Forms,NT Service,COM
+ 作为宿主(Host)。WCF可以支持的协议包括TCP,HTTP,跨进程以及自定义,安全模式则包括SAML,Kerberos,X509,用户 / 密码,自定义等多种标准与模式。
None.gif
None.gif下面仅介绍托管应用程序宿主(自宿主)的方式:(例子是一个实现用户登录注册服务)
None.gif建立三个工程:
None.gifClient 使用WCF服务
None.gifCommon 定义合同规范
None.gifServer 实现WCF服务
None.gifCommon:
None.gif在WCF中,所有的服务都暴露合同。合同是一种描述服务所实现功能的平台中立的标准的方式。WCF定义了四种类型的合同:
None.gif  · 服务合同描述你可以在服务上执行哪些操作。
None.gif  · 数据合同定义哪些数据类型被传入和传出服务。WCF为内置类型定义隐式合同,例如int和string,但是你可以容易地为定制类型定义显式的选入式数据合同。
None.gif  · 错误合同定义哪些错误将被该服务所激发,以及该服务怎样处理错误信息和把如何把它们传播到客户端。
None.gif  · 消息合同允许服务直接与消息进行交互。消息合同可以被类型化或非类型化,并且有点类似于CLR中的迟绑定调用。不过,消息合同很少为SOA开发者所用。
None.gif
1 .服务合同:
None.gif
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
ExpandedBlockStart.gifContractedBlock.gif
/**/ /**/ /**/ /**
InBlock.gif * ServiceContract,OperationContract 在System.ServiceModel.dll中
ExpandedBlockEnd.gif * 
*/

None.gif
using  System.ServiceModel;
None.gif
using  Common.Entity;
None.gif
None.gif
namespace  Common.Interface
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [ServiceContract]
InBlock.gif    
public interface IUserService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        [OperationContract]
InBlock.gif        OperationResult Login(
string userName, string password);
InBlock.gif
InBlock.gif        [OperationContract]
InBlock.gif        OperationResult Register(
string userName, string password);
InBlock.gif
InBlock.gif        [OperationContract]
InBlock.gif        OperationResult ModifyPassword(
string userName, string newPassword);
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
2 .数据合同:
None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Runtime.Serialization;
None.gif
None.gif
namespace  Common.Entity
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [DataContract]
InBlock.gif    
public class OperationResult
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private ResultChoice result;
InBlock.gif        
private string failReason;
InBlock.gif
InBlock.gif        [DataMember]
InBlock.gif        
public ResultChoice Result
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return result;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [DataMember]
InBlock.gif        
public string FailReason
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return failReason;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                failReason 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
public enum ResultChoice
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        Succeed
=1,
InBlock.gif        Failed
=0
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
None.gif
3 .错误合同:
None.gif
//
None.gif

None.gifServer实现:
None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  Common.Interface;
None.gif
using  Common.Entity;
None.gif
None.gif
namespace  Server.DAL
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class UserService:IUserService
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ContractedSubBlock.gifExpandedSubBlockStart.gif        IUserService Members
IUserService Members#region IUserService Members
InBlock.gif
InBlock.gif        
public OperationResult Login(string userName, string password)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            OperationResult result 
= new OperationResult();
InBlock.gif            
if (userName.Equals("A"&& password.Equals("B"))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                result.Result 
= ResultChoice.Failed;
InBlock.gif                result.FailReason 
= "The password of A is not B!!!";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return result;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public OperationResult Register(string userName, string password)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public OperationResult ModifyPassword(string userName, string newPassword)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
throw new Exception("The method or operation is not implemented.");
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif        
#endregion

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif运行服务:
None.gif
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.ServiceModel;
None.gif
using  Server.DAL;
None.gif
None.gif
namespace  Server
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ServiceHost host 
= new ServiceHost(typeof(UserService));
InBlock.gif            host.Open();
InBlock.gif            Console.WriteLine(
"The service is ready.");
InBlock.gif            Console.ReadLine();
InBlock.gif            host.Close();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif服务端App.Config:
None.gif
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
None.gif
< configuration >
None.gif  
< system.serviceModel >
None.gif    
< services >
None.gif      
< service name = " Server.DAL.UserService " >
None.gif        
< endpoint address = " net.tcp://localhost:8001/UserService "
None.gif                   binding
= " netTcpBinding "
None.gif           contract
= " Common.Interface.IUserService "   >
None.gif          
<!-- 指定服务的契约接口 -->
None.gif        
</ endpoint >
None.gif      
</ service >
None.gif    
</ services >
None.gif  
</ system.serviceModel >
None.gif
</ configuration >
None.gifClient客户端:
None.gif客户端App.Config:
None.gif
<? xml version = " 1.0 "  encoding = " utf-8 "   ?>
None.gif
< configuration >
None.gif  
< system.serviceModel >
None.gif    
< client >
None.gif      
< endpoint name = " UserServiceClient "  address = " net.tcp://localhost:8001/UserService "
None.gif                binding
= " netTcpBinding "
None.gif                contract
= " Common.Interface.IUserService " >
None.gif      
</ endpoint >
None.gif    
</ client >
None.gif  
</ system.serviceModel >
None.gif
</ configuration >
None.gif使用服务:
None.gif
using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Windows.Forms;
None.gif
using  Client.Util;
None.gif
using  Common.Entity;
None.gif
using  Common.Interface;
None.gif
using  System.ServiceModel;
None.gif
None.gif
namespace  Client
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
static class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//**//**//// <summary>
InBlock.gif        
/// The main entry point for the application.
ExpandedSubBlockEnd.gif        
/// </summary>

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

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif 
None.gif
None.gif运行结果为:
None.gifThe password of A 
is  not B !!!
None.gif
None.gif
None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值