Allen Wang

专注web开发
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

开始了解WCF

Posted on 2007-10-11 14:53  AllenWang.NET  阅读(274)  评论(0)    收藏  举报
通过在VS 2005中用Website建立WCF Service工程示例了解WCF Service的创建
1)建立一个Solution, 在这个Solution下添加一个用来建立WCF Service的Website以及测试这个WCF Service的测试Console Project。
2)在Website下建立WCF Service
     2.1)建立Service Contract,[ServiceContract()] [OperationContract()]
[ServiceContract()]
public interface IHelloService
{
    [OperationContract]
    
string SayHello(string name);
}
     2.2) 根据Service Contract,建立Service Implemention 
     2.3) 配置Service 
  <system.serviceModel>
    
<services>
      
<service name="HelloService" behaviorConfiguration="Default">
        
<endpoint contract="IHelloService" binding="wsHttpBinding"/>
        
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      
</service>
    
</services>
    
<behaviors>
      
<serviceBehaviors>
        
<behavior name="Default">
          
<serviceMetadata httpGetEnabled="true"/>
        
</behavior>
      
</serviceBehaviors>
    
</behaviors>
  
</system.serviceModel>
       注意点:对一个Service而言一定要一个对应的behavior, 且在声明endpoint时一定要有IMetadataExchange contract.
     2.4) 建立WCF Service Host
            在Website中添加一个扩展名为svc的文件,其中代码内容如下:       
<% @ServiceHost Language=C# Debug="true" Service="HelloService" CodeBehind="~/App_Code/Service.cs" %>

3)建立Service 测试客户端
    3.1)生成Service 代理文件
           SvcUtil.exe http://localhost:5000/WebSite/Service.svc
           用上面的命令可生成一个Service代理文件和一个Service客户端配置文件

    3.2)编写调用Service的代码

WCF 开发工具
C:\Program Files\Microsoft SDKs\Windows\v6.0\Bin

http://msdn2.microsoft.com/zh-cn/library/ms732015.aspx
1) SvcConfigEditor.exe
2) SvcTraceViewer.exe