zoukankan      html  css  js  c++  java
  • 控制台中寄宿WCF服务

    一、首先创建一个类库,用来定义WCF服务

    修改服务代码定义,具体代码如下

    	 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IHelloService”。
        [ServiceContract]
        public interface IHelloService
        {
            [OperationContract]
            string GetMessage(string message);
        }
    

    	 // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“HelloService”。
        public class HelloService : IHelloService
        {
            public string GetMessage(string message)
            {
                return message + "@" + DateTime.Now;
            }
        }
    

    二、创建一个控制台项目,用来承载WCF服务

    1、首先添加对服务类库的引用,并添加引用System.Service.Model

    2、修改配置文件,在 <system.serviceModel> </system.serviceModel>节点中设置服务相关节点信息、绑定信息以及基地址,具体代码如下

    	  <system.serviceModel>
        <services>
          <service name="SimpleService.HelloService">
            <!--设置服务节点,服务的地址直接采用基地址,使用basicHttpBinding-->
            <endpoint address="" binding="basicHttpBinding" contract="SimpleService.IHelloService">
            </endpoint>
            <!--设置元数据交换节点-->
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
            </endpoint>
            <host>
              <baseAddresses>
                <!--服务的基地址用来访问获取元数据-->
                <add baseAddress="http://localhost:8057/HelloService"/>
              </baseAddresses>
            </host>
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior name="HttpGetEnable">
              <!--公开元数据,正是部署时候应该去掉防止元数据泄露-->
              <serviceMetadata httpGetEnabled="true"/>
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <bindings>
          <basicHttpBinding>
            <binding name="NoneSecurity">
              <!--取消安全验证-->
              <security mode="None">
              </security>
            </binding>
          </basicHttpBinding>
        </bindings>
      </system.serviceModel>
    

    3、启动服务

    在main方法中加入如下代码启动服务

     using (ServiceHost host = new ServiceHost(typeof(HelloService)))
     {
             host.Open();
    
            Console.WriteLine("WCF 已经启动@" + DateTime.Now);
    
            Console.ReadKey();
    }
    

    服务启动后效果如下图

    如果出现如下异常信息,只要以管理员身份重新启动VS并启动服务即可

  • 相关阅读:
    C# 使用消息队列,包括远程访问
    Python3中urllib使用与源代码
    多年前写的DataTable与实体类的转换
    DataTable添加列和行的三种方法
    DevExpress 常用命令包括导出-打印-打印预览等
    c#开发_Dev的关于XtraGrid的使用(GridControl小结)
    正则表达式精华(包涵常用经典方法)
    数据库 插入时 碰到NULL报错判断的一种方法(技巧)
    MDI窗体简单方法(调用,闪屏)
    GridControl GridView 修改表格中的标题居中
  • 原文地址:https://www.cnblogs.com/enternal/p/4784726.html
Copyright © 2011-2022 走看看