wcf小实例(2)

//用app.config实现wcf调用
////定义contrat interface


using System;
using System.ServiceModel;
using System.Text;

namespace DataSericesContract
{
    [ServiceContract(Name = "DataServicesContract", Namespace = "www.msdn.com")]
    public interface IDataObj
    {
        [OperationContract(Name = "MethodA", Action = "www.msdn.com/DataServicesContract/NameMethod", ReplyAction = "www.msdn.com")]
        string NameMethod();
        [OperationContract(Name = "MethodB", Action = "www.msdn.com/DataServicesContract/AdminMethod", ReplyAction = "www.msdn.com")]
        string AdminMethod();
    }
}
///////////实现该接口类

using System;
using System.ServiceModel;
using System.Text;
using DataSericesContract;

namespace DataSerices
{
   public class DataObj : IDataObj
    {

        #region IDataObj 成员

        public string NameMethod()
        {
            return "IDataOjb.NameMethod invoked";
        }

        public string AdminMethod()
        {
            return "IDataOjb.AdminMethod invoked";
        }

        #endregion
    }
}


///////////////////////service host

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ServiceModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataSerices;
using DataSericesContract;

namespace ServiceHostWCF
{
    public partial class HostWCF : Form
    {
        public HostWCF()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ServiceHost host = null;
            try
            {
                using (host = new ServiceHost(typeof(DataObj)))
                {
                    host.Open();
                    this.button1.Enabled = false;
                    this.toolstart.Text = "services start aucceed !";
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                this.button1.Enabled = true;
                this.toolstart.Text = "Services start fail";
            }
            finally
            {
                if (host != null)
                    host.Close();
            }
        }
    }
}


///////////////////app.config//////////////////////
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="DataSerices.DataObj" behaviorConfiguration="serviceBehavior">
        <endpoint address="DataObj" contract="DataSericesContract.IDataObj" binding="basicHttpBinding" />
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8900"/>
          </baseAddresses>
        </host>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="serviceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>
</configuration>

posted on 2011-06-23 15:53  SonyXbox  阅读(209)  评论(0编辑  收藏  举报

导航