zoukankan      html  css  js  c++  java
  • 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>
  • 相关阅读:
    Jupyter Notebook的使用
    作业
    第七周:Python
    第六周:统计学
    JS 怎么刷新当前页面
    PHP中获取当前页面的完整URL
    点击删除按钮后 弹出确认对话框弹窗特效
    php 判断时间是否超过
    php 区分中文,英文,中英混合
    微信view类型的菜单获取openid范例
  • 原文地址:https://www.cnblogs.com/server126/p/2088205.html
Copyright © 2011-2022 走看看