• Hosting the WCF service


    1. Hosting the service in a managed application

    We can create a .NET managed application and host a WCF service inside the
    application. The hosting application can be a command-line application, a Windows
    Forms application, or a web application. This hosting method gives you full control
    over the lifetime of the WCF service. It is very easy to debug and deploy, and
    supports all bindings and transports. The drawback of this hosting method is that
    you have to start the hosting application manually and it has only limited support
    for high availability, easy manageability, robustness, recoverability, versioning, and
    deployment scenarios.

     2. Using Console as the Host App

    the code for app.config:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <wsHttpBinding>
                    <binding name="WSHttpBinding_IHelloWorldService" closeTimeout="00:01:00"
                        openTimeout
    ="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        bypassProxyOnLocal
    ="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferPoolSize
    ="524288" maxReceivedMessageSize="65536"
                        messageEncoding
    ="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                        allowCookies
    ="false">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead
    ="4096" maxNameTableCharCount="16384" />
                        <reliableSession ordered="true" inactivityTimeout="00:10:00"
                            enabled
    ="false" />
                        <security mode="Message">
                            <transport clientCredentialType="Windows" proxyCredentialType="None"
                                realm
    ="" />
                            <message clientCredentialType="Windows" negotiateServiceCredential="true"
                                algorithmSuite
    ="Default" establishSecurityContext="true" />
                        </security>
                    </binding>
                </wsHttpBinding>
            </bindings>
            <client>
                <!--<endpoint address="http://localhost:8080/HostDevServer/HelloWorldService.svc"-->
                <endpoint address="http://localhost:8080/HostCmdLineApp/HelloWorldService/" 
                    binding
    ="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloWorldService"
                    contract
    ="IHelloWorldService" name="WSHttpBinding_IHelloWorldService">
                    <identity>
                        <userPrincipalName value="IT14\Administrator" />
                    </identity>
                </endpoint>
            </client>
        </system.serviceModel>
    </configuration>

    Your console service host code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.Configuration;

    namespace HostCmdLineApp
    {
        class Program
        {
            static void Main(string[] args)
            {
                Type serviceType = typeof(HelloWCF.Service.HelloWorldService);

                string httpBaseAddress = ConfigurationManager.AppSettings["HTTPBaseAddress"];
                Uri[] baseAddress = new Uri[] { new Uri(httpBaseAddress) };

                ServiceHost host = new ServiceHost(serviceType, baseAddress);
                host.Open();
                Console.WriteLine("HelloWorldService is now running. ");
                Console.WriteLine("Press any key to stop it ...");
                Console.ReadKey();
                host.Close();
            }
        }
    }

    Need to modify the app.config in your client app:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <system.serviceModel>
            <bindings>
                <wsHttpBinding>
                    <binding name="WSHttpBinding_IHelloWorldService" closeTimeout="00:01:00"
                        openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                        bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
                        maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                        messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
                        allowCookies="false">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                        <reliableSession ordered="true" inactivityTimeout="00:10:00"
                            enabled="false" />
                        <security mode="Message">
                            <transport clientCredentialType="Windows" proxyCredentialType="None"
                                realm="" />
                            <message clientCredentialType="Windows" negotiateServiceCredential="true"
                                algorithmSuite="Default" establishSecurityContext="true" />
                        </security>
                    </binding>
                </wsHttpBinding>
            </bindings>
            <client>
                <!--<endpoint address="http://localhost:8080/HostDevServer/HelloWorldService.svc"-->
                <endpoint address="http://localhost:8080/HostCmdLineApp/HelloWorldService/" 
                    binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloWorldService"
                    contract="IHelloWorldService" name="WSHttpBinding_IHelloWorldService">
                    <identity>
                        <userPrincipalName value="IT14\Administrator" />
                    </identity>
                </endpoint>
            </client>
        </system.serviceModel>
    </configuration>

    As you can see, I modified the address of the endpoint to the new address we have just declared in the app.config of the host

     http://localhost:8080/HostCmdLineApp/HelloWorldService/

    Now run the host(Ctrl + F5), and then run the client(Ctrl + F5)

    Result:

    How are you doing David Gu!

    BTW, you can also host the service in a windows service

    The steps to create such a hosting application are very similar to what we did to
    host a WCF service in a command-line application, except that you have to create an
    installer to install the Windows service in the Service Control Manager (or you can
    use the .NET Framework Installutil.exe utility)

    技术改变世界
  • 相关阅读:
    Selenium 验证
    Flask 拓展(flask-admin)
    读取 xlsx中数据
    OSS 上传内容
    Tornado 端口绑定方式
    Tornado 基础
    Flask 懒人版分页(未完善)
    kafka事务原理、事务API和使用场景(转)
    jvm几种垃圾回收机制小结
    理解Semaphore及其用法详解(转)  -实现一个文件允许的并发访问数
  • 原文地址:https://www.cnblogs.com/davidgu/p/2405614.html
Copyright © 2020-2023  润新知