代码改变世界

WCF学习记录

2011-12-25 08:38  java线程例子  阅读(215)  评论(0编辑  收藏  举报

原来搞WCF RIA,限制多点,这次把WCF好好复习一下,准备做分布式调度用。

1、WCF服务中的合同,可施加于接口,而非类;
2、WCF服务中如果私有方法被施加了操作属性,则远程可以调用,这样做的好处可以让有些方法仅对远程调用有用,而本地调用无效;
3、接口和实体模型可单独放在一起,供客户端和服务端共用;
4、WCF支持多种协议,而且支持双工通信,这是其优势;

例子:

A)接口

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

namespace Interfaces
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService1”。
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: 在此添加您的服务操作
    }

    // 使用下面示例中说明的数据协定将复合类型添加到服务操作
    [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; }
        }
    }
}



B)服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Interfaces;
namespace WcfServiceLibrary1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“Service1”。
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}



C)宿主
宿主可以有4种方式,自主应用,Windows服务,IIS,WAS,下面是自主应用示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using Interfaces;
using WcfServiceLibrary1;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost host = new ServiceHost(typeof(Service1),
                new Uri("http://localhost:80/HelloService"));
            host.AddServiceEndpoint(typeof(IService1), new BasicHttpBinding(),"Svc");
            host.Open(); 
            Console.WriteLine("Start Your Service.");
            Console.ReadKey();
            host.Close();
        }
    }
}



D)客户端

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;
using Interfaces;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //静态调用方式,通过手工添加服务应用来进行,这种方式在性能上会比较好,但灵活性不足.
            //ServiceReference1.IService1 svr1 = new ServiceReference1.Service1Client();
            //string name = svr1.GetName();
            //MessageBox.Show(name);
            //动态调用方式,这个时候合同接口和实体类共用的优势就显示出来了,好处服务修改后,不用更新服务,但缺点是感觉比较慢。
            //因为每次都要装载WSDL信息。
            ServiceEndpoint httpEndpoint = new ServiceEndpoint(ContractDescription.GetContract(typeof(IService1)),
                new BasicHttpBinding(), 
                new EndpointAddress("http://localhost:80/HelloService/Svc"));
            using (ChannelFactory<IService1> factory = new ChannelFactory<IService1>(httpEndpoint))
            { 
                //创建IHello服务的代理对象; 
                IService1 service = factory.CreateChannel();
                string name = service.GetData(100);
                CompositeType theType = new CompositeType();
                theType.BoolValue = true;
                theType.StringValue = "dsafasdf";
                CompositeType theRet = service.GetDataUsingDataContract(theType);
                MessageBox.Show(theRet.StringValue);
            } 
        }
    }
}


2011-12-25 (自主Host方式中的关系)

1)一个ServiceHost只能寄主一个服务,虽然可以让服务类实现多个接口达到多个服务寄主一个Host的目的,但这个服务类将变得非常臃肿和杂乱;
2)微软的这个Host体系真的不见其水平,不知是出于什么用意搞成这样的,非常的不利于工程化作业,其实这种服务过程非常的简单,就是服务端接到服务请求时,根据接口契约去实例化一个实现了其接口的服务类来提供服务,完全可以采用服务节点为(服务接口,服务类,协议,地址)来实现。虽然有人也利用一些机制实现了一个ServiceHost寄主多个服务的方法(http://www.cnblogs.com/sweethome/archive/2010/11/16/one_servicehost_mutil_service.html),但我觉得这种方式非常的不纯粹。
3)不过如果采用IIS寄主方式和WAS方式在维护上就好很多,特别是WAS可以支持多种绑定协议(http://msdn.microsoft.com/zh-cn/library/ms731053.aspx),但微软不地道,全用命令绑定,为啥不整个图形配置界面多好(你也可以直接修改applicationHost.config 这个文件,比较暴力一点)。

 

PS:我发现微软做的很多东西,都不太适合规模化,工程化作业,比如AEF,WCF RIA等.