淘小店

WCF入门实例

WCF服务不能孤立的存在,如何运行呢?WCF 服务宿主程序可以分为两种:1、寄宿在自身进程中(控制台程序)2、寄宿在IIS进程中。

首先来看第一种:

创建一个控制台程序, 然后添加一个wcf服务,项目中会自动添加一个IXXService.cs、XXService.cs和App.config 三个文件,IXXService.cs就是要提供给外界的方法的借口,XXService.cs继承IXXService.cs实现IXXService.cs中定义的方法。

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace Wcf服务端
{
    
// 注意: 如果更改此处的接口名称“IService1”,也必须更新 App.config 中对“IService1”的引用。
    [ServiceContract]
    
public interface IMyWCFService
    {
        [OperationContract]
        
string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        
// 任务: 在此处添加服务操作
    }

    
// 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [DataContract]
    
public class CompositeType
    {
        
bool boolValue = true;
        
string stringValue = "Hello ";

        [DataMember]
        
public bool BoolValue
        {
            
get { return boolValue; }
            
set { boolValue = value; }
        }

        [DataMember]
        
public string StringValue
        {
            
get { return stringValue; }
            
set { stringValue = value; }
        }
    }
}

 

 代码

 

 app.config

代码

写好借口和实现之后就需要控制台程序把这个wcf运行起来

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace WCF服务端程序
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
using (ServiceHost host = new ServiceHost(typeof(MyWCFService)))
            {
                host.AddServiceEndpoint(
typeof(IMyWCFService), new WSHttpBinding(), "http://127.0.0.1:9999/mywcf");
                
if (host.Description.Behaviors.Find<ServiceMetadataBehavior>() == null)
                {
                    ServiceMetadataBehavior behavior 
= new ServiceMetadataBehavior();
                    behavior.HttpGetEnabled 
= true;
                    behavior.HttpGetUrl 
= new Uri("http://127.0.0.1:9999/mywcf/metadata");
                    host.Description.Behaviors.Add(behavior);
                }
                host.Opened 
+= delegate
                {
                    Console.WriteLine(
"mywcf已经启动,按任意键终止服务!");
                };

                host.Open();
                Console.Read();
            }
        }
    }
}

 运行之后访问http://127.0.0.1:9999/mywcf/metadata就会看到

第二种寄宿在IIS进程上

首先创建一个web项目,然后在项目中添加wcf服务,此时会生成 IXXService.cs、XXService.svc两个文件,并在web.config中添加

<system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="WCF绑在IIS.MyWCFonIISBehavior">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="false" />
                </behavior>
            </serviceBehaviors>
        </behaviors>
        <services>
            <service behaviorConfiguration="WCF绑在IIS.MyWCFonIISBehavior"
                name="WCF绑在IIS.MyWCFonIIS">
                <endpoint address="" binding="wsHttpBinding" contract="WCF绑在IIS.IMyWCFonIIS" name="wcfoniis">
                    <identity>
                        <dns value="localhost" />
                    </identity>
                </endpoint>
                <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel> 

 相应的在IXXService.cs和XXService.svc定义接口和实现,完成之后,在IIS中新建站点,指向程序所在目录。就可以访问了。

创建完成WCF服务之后,客户端怎样调用?在需要调用的项目中添加服务引用, 之后就可以调用了,以下介绍两种种调用的方式:

1、直接new一个wcf的实例  

using (var client = new MyWCFService.MyWCFServiceClient())

{
Console.WriteLine(client.GetData(1));Console.Read();}

2、通过服务代理的方式访问

using (ChannelFactory<IMyWCFService> channelFactory = new ChannelFactory<IMyWCFService>(new WSHttpBinding(), "http://127.0.0.1:9999/mywcf"))
{
IMyWCFService proxy = channelFactory.CreateChannel();
using (proxy as IDisposable)
{
Console.WriteLine(proxy.GetData(1));
Console.WriteLine(proxy.GetDataUsingDataContract(new MyWCF客户端程序.MyWCFService.CompositeType()));
Console.Read();
}

 wcf入门实例先简单介绍到这里。

 

 

 

posted on 2011-02-23 16:57  adu123  阅读(835)  评论(0编辑  收藏  举报

导航

淘小店