zoukankan      html  css  js  c++  java
  • WCF(二) 使用配置文件实现WCF应用程序

    服务三要素ABC
    A:Address 在哪里 (包含传输方式的信息)
    B:Binding 怎么实现(与地址的传输方式要匹配)
    C:Contract做什么(服务契约)

    namespace WCFServiceDemo
    {
        [ServiceContract]
      public  interface IHelloService
        {
            [OperationContract]
            DateTime GetDateTime();
    
            [OperationContract]
            DataTable GetUserInfo();
        }
    }

    继承接口

     class HelloService:IHelloService
        {
           public DateTime GetDateTime()
           {
                return DateTime.Now;
           }
     
            public DataTable GetUserInfo()
            {
                string connstring = @"Data Source=WIN7U-20130122RSQLEXPRESS;Initial Catalog=TCEKT;Persist Security Info=True;User ID=sa";
                SqlConnection conn = new SqlConnection(connstring);
                SqlDataAdapter sda=new SqlDataAdapter("select * from User",conn);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                return dt;
            }
        }

    Winfom调用

        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 = "服务已启动!";
            }
        }

    配置文件App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
        <startup> 
            <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
        </startup>
        <!--WCF-->
        <system.serviceModel>
            <services>
                <service name="WinformHello.HelloService" behaviorConfiguration="testBehavior"><!--name:指实现服务契约的类-->
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:8001/Hello"/>
                        </baseAddresses>
                    </host>
                    <endpoint address="" binding="basicHttpBinding" contract="WCFServiceDemo.IHelloService">
                    </endpoint> 
                </service> 
            </services>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="testBehavior">
                        <serviceMetadata httpGetEnabled="true"/><!--允许访问wcf服务-->
                    </behavior>
                </serviceBehaviors>
            </behaviors>
        </system.serviceModel>
    </configuration>

    启动:

    因win7安全性问题,如果启动不了 vs2012 右键

    如果启动成功

    在app.config文件中找到

    <baseAddresses>
    <add baseAddress="http://localhost:8001/Hello"/>
    </baseAddresses>

    Ctrl键+左击单击

    OK  成功……

    --学云网Tiger老师的视频


    作者:PEPE
    出处:http://pepe.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

  • 相关阅读:
    linux下小知识点积累
    马斯洛需求层次理论
    tar命令的小经验
    shell 和c语言的区别
    使用vue实现的品牌列表简单小例子
    vue的基本代码以及常见指令
    MVC和MVVM
    CSS3幽灵
    Web版App,原生App,混合App的区别以及优缺点
    常见的sql操作
  • 原文地址:https://www.cnblogs.com/PEPE/p/3304508.html
Copyright © 2011-2022 走看看