码迷,mamicode.com
首页 > 其他好文 > 详细

从配置文件实现WCF的调用

时间:2015-03-22 10:31:47      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:

   使用配置文件

1.创建一个类库

技术分享

添加”System.ServiceModel“引用

IHelloService:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.ServiceModel;
namespace wcfService
{
    [ServiceContract]
public     interface IHelloService
    {
        [OperationContract]
        DateTime GetDataTime();
          [OperationContract]
        DataTable GetUserInfo();
    }
}
View Code

2.创建一个windows窗体程序

技术分享

添加”System.ServiceModel“,还有项目wcfservice层的引用

HelloService:

技术分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using wcfService;
namespace WinformHello
{
public    class HelloService:IHelloService
    {

        public DateTime GetDataTime()
        {
            return DateTime.Now;
        }

        public System.Data.DataTable GetUserInfo()
        {
            string sql = @"Data Source=.;Initial Catalog=Students;User ID=sa;Password=abcd1234!";
            SqlConnection conn = new SqlConnection();
            SqlDataAdapter sda = new SqlDataAdapter("select  * from student",conn);
            //DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            sda.Fill(dt);
            return dt;
        }
    }
}
View Code

App.config:

技术分享
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <system.serviceModel>
    <services>
      <service name="WinformHello.HelloService" behaviorConfiguration="TestBehavior">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/Hello"/>
          </baseAddresses>
        </host>
        <endpoint address="" binding="basicHttpBinding" contract="wcfService.IHelloService">
          
        </endpoint>
      </service>
    </services>

    <behaviors>
      <serviceBehaviors>
        <behavior name="TestBehavior">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    
  </system.serviceModel>
</configuration>
View Code

Form1:

技术分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace WinformHello
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        ServiceHost host = null;

        private void button1_Click(object sender, EventArgs e)
        {
            host = new ServiceHost(typeof(WinformHello.HelloService));
            host.Open();
            label1.Text = "服务已启动……";
        }
    }
}

 
View Code

 

从配置文件实现WCF的调用

标签:

原文地址:http://www.cnblogs.com/zychengzhiit1/p/4356737.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!