• Hosting WCF Service


    There are some ways to hosting WCF service as below:

    1. IIS; 2. Console App; 3. Window Service

    Using Console Application to host WCF service:

    Step 1:

     

    Step 2: Add a Class Library project : 'Contract'

    Step 3: Add a Interface : 'IHelloWorld'

    using System.ServiceModel;
    
    namespace Contract
    {
        [ServiceContract(Name = "HelloWorldService")]
        public interface IHelloWorld
        {
            [OperationContract]
            string SayHello();
        }
    }

    Step 4: Add a Class : 'HelloWorld'

    namespace Contract
    {
        public class HelloWorld : IHelloWorld
        {
            public string SayHello()
            {
                return "Hello World. I'm WCF Service.";
            }
        }
    }

    Step 5: Add a Console Application : 'Host'

    Step 6:
    Use WCF Service Configuration Editor to generate the server config.

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.serviceModel>
            <behaviors>
                <serviceBehaviors>
                    <behavior name="HelloWorldBehavior">
                        <serviceMetadata httpGetEnabled="true" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <services>
                <service behaviorConfiguration="HelloWorldBehavior" name="Contract.HelloWorld">
                    <endpoint address="http://localhost:9999/HelloWorldService" binding="basicHttpBinding"
                        bindingConfiguration="" contract="Contract.IHelloWorld" />
                    <host>
                        <baseAddresses>
                            <add baseAddress="http://localhost:9999/HelloWorldService" />
                        </baseAddresses>
                    </host>
                </service>
            </services>
        </system.serviceModel>
    </configuration>

    Step 7:
    Set up server host

    using System;
    using System.ServiceModel;
    using Contract;
    
    namespace Host
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (var host = new ServiceHost(typeof(HelloWorld)))
                {
                    host.Opened += delegate
                                       {
                                           Console.WriteLine("CalculaorService已经启动,按任意键终止服务!");
                                       };
                    host.Open();  
                    Console.Read();
                }
            }
        }
    }
  • 相关阅读:
    EF Core使用笔记(基于MySql数据库)
    开发环境---->服务器(数据库迁移Migration)
    正向代理和反向代理
    Linux基础命令
    Git + Docker + Jenkins自动化部署web到Linux(Centos)
    poj3320(尺取法)
    poj3061(尺取法)
    51nod 1092(lcs)回文字符串
    51nod1268(基础dfs)
    51nod-1459-迷宫游戏
  • 原文地址:https://www.cnblogs.com/vincentDr/p/2913645.html
Copyright © 2020-2023  润新知