zoukankan      html  css  js  c++  java
  • WCF中,通过C#代码或App.config配置文件创建ServiceHost类

    C#

    static void Main(string[] args)
    {
        //创建宿主的基地址
        Uri baseAddress = new Uri("http://localhost:8080/User");
    
        //创建宿主
        using (ServiceHost host = new ServiceHost(typeof(User), baseAddress))
        {
            host.AddServiceEndpoint(typeof(IUser), new WSHttpBinding(), "");
    
            //将HttpGetEnabled属性设置为true
            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
    
            //将行为添加到Behaviors中
            host.Description.Behaviors.Add(smb);
    
            //打开宿主
            host.Open();
            Console.WriteLine("WCF中的HTTP监听已启动....");
            Console.ReadLine();
            host.Close();
        }
    }

    App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
        <services>
          <service name="WCFLibrary.User">
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/User"/>
              </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="WCFLibrary.IUser"></endpoint>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <serviceMetadata httpGetEnabled="True"/>
              <serviceDebug includeExceptionDetailInFaults="False"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>
    </configuration>
    public partial class MainForm : Form
    {
        ServiceHost host;
    
        public MainForm()
        {
            InitializeComponent();
        }
    
        private void MainForm_Load(object sender, EventArgs e)
        {
            host = new ServiceHost(typeof(User));
            //打开宿主
            host.Open();
            this.label1.Text = "WCF中的HTTP监听已启动....";
        }
    
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            host.Close();
        }
    }
  • 相关阅读:
    1-直播转点播
    3-美团 HTTP 服务治理实践
    3-SSDB 高性能NoSQL数据库, 用于替代 Redis.
    配置kubectl在Mac(本地)远程连接Kubernetes集群
    4-rocketmq 发送时异常:system busy 和 broker busy 解决方案
    3-RocketMQ 简单梳理 及 集群部署笔记
    2-Rocketmq产品架构(参考阿里云)
    1-RocketMq 学习 中文文档(一)
    tar命令参数详解
    Ubuntu 安装 .bundle 文件
  • 原文地址:https://www.cnblogs.com/gilbert/p/5725053.html
Copyright © 2011-2022 走看看