Wcf 基础编程

1.设计服务协定 比较好的方法是接口来定义服务的协定

 

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

namespace WcfService2
{
    [ServiceContract]   //服务协定定义

    public interface IInterface1
    {
        [OperationContract]  //要公开的服务方法
        string Function1(int value);

    }
    [ServiceContract]  //定义的第二个服务协定
    public interface IService1
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    [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; }
        }
    }
}

2.实现服务协定  也就是用类实现已定义好的接口 

 

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

namespace WcfService2
{
    public class Service1 : IService1,IInterface1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

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

        public string Function1(int a)
        {
            return "hello world!";
        }
    }
}

3.承载服务的实现 实现服务的宿主

 

a.用windowform 实现的宿主

 

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 WcfService2;
using System.ServiceModel;
using System.ServiceModel.Description;

namespace Host001
{
    public partial class Form1 : Form
    {
        ServiceHost sHost = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Uri uri = new Uri("http://localhost:8000");   //服务的地址

            sHost = new ServiceHost(typeof(Service1), uri);   //创建服务宿主

            sHost.AddServiceEndpoint(typeof(IService1), new WSHttpBinding(), "Service1");  //添加第一个终结点
            sHost.AddServiceEndpoint(typeof(IInterface1), new WSHttpBinding(), "Interface1");//第二个终结点

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();  //定义服务传输层
            smb.HttpGetEnabled = true;   //定义传输的属性
            sHost.Description.Behaviors.Add(smb);

            
            sHost.Open();   //打开服务
        }
    }
}

b.用web服务器做为wcf 的宿主

 

实现wcf 服务发布 .svc文件

<%@ServiceHost language="c#" Debug="true" Service="WcfService2.Service1"%>

 

在web.config 中配置wcf 服务

 

    <system.serviceModel>
    <services>
      <service name="WcfService2.Service1" behaviorConfiguration="MyServiceTypeBehaviors">

        <!-- This endpoint is exposed at the base address provided by host:                                        http://localhost/servicemodelsamples/service.svc  -->
        <endpoint address="Service1"
                  binding="wsHttpBinding"
                  contract="WcfService2.IService1" />
        <endpoint address="Interface1"
                  binding="wsHttpBinding"
                  contract="WcfService2.IInterface1" />

        <!-- The mex endpoint is explosed at http://localhost/servicemodelsamples/service.svc/mex -->
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />

      </service>
    </services>
      <behaviors>
        <serviceBehaviors>
          <behavior name="MyServiceTypeBehaviors" >
            <serviceMetadata httpGetEnabled="true" />
          </behavior>
        </serviceBehaviors>
      </behaviors>

    </system.serviceModel>

 

4.添加wcf 服务引用

 用.net IDE 或 用 svcutil.exe 工具 生成 wcf 服务的 stub 文件

 

5. 创建wcf 服务 的客户端

 

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 Client1.ServiceReference2;

namespace Client1
{
    public partial class Form1 : Form
    {
        Service1Client service1 = new Service1Client();  //实例化终结点1
        Interface1Client interface1 = new Interface1Client();  //实例化第二个终结点

        public Form1()
        {
            InitializeComponent();
            service1.Open();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = service1.GetData(21);   //调用wcf的方法
            CompositeType v = new CompositeType() ;   //生成传入数据协定的数据实例
            v.BoolValue = true;
            v.StringValue = "hello";
            CompositeType type = service1.GetDataUsingDataContract(v);   //调用wcf的方法
            string s = interface1.Function1(1);   //调用wcf的方法
        }
    }
}

后记:WCF就是这么简单,它把通信分成几个独立层,第层都可以用不同的技术来实现,看来是一个比较有新意的技术,呵呵,祝大家编程顺利。

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值