博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Hosting the WCF service
阅读量:6281 次
发布时间:2019-06-22

本文共 5117 字,大约阅读时间需要 17 分钟。

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

 

 

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)

 

转载地址:http://nenva.baihongyu.com/

你可能感兴趣的文章
《UNIX网络编程 卷1:套接字联网API(第3版)》——8.6 UDP回射客户程序:dg_cli函数...
查看>>
不要将时间浪费到编写完美代码上
查看>>
《第一桶金怎么赚——淘宝开店创业致富一册通》一一第1章 创业梦想,怎样起步...
查看>>
基于容器服务的持续集成与云端交付(三)- 从零搭建持续交付系统
查看>>
《算法基础:打开算法之门》一3.4 归并排序
查看>>
高德开放平台开放源代码 鼓励开发者创新
查看>>
《高并发Oracle数据库系统的架构与设计》一2.5 索引维护
查看>>
《Exchange Server 2010 SP1/SP2管理实践》——2.4 部署外部网络环境
查看>>
Firefox 是 Pwn2own 2014 上攻陷次数最多的浏览器
查看>>
阿里感悟(十八)- 应届生Review
查看>>
《计算广告:互联网商业变现的市场与技术》一第一部分 在线广告市场与背景...
查看>>
话说模式匹配(5) for表达式中的模式匹配
查看>>
《锋利的SQL(第2版)》——1.7 常用函数
查看>>
《Arduino家居安全系统构建实战》——1.5 介绍用于机器学习的F
查看>>
jquery中hover()的用法。简单粗暴
查看>>
线程管理(六)等待线程的终结
查看>>
spring boot集成mongodb最简单版
查看>>
DELL EqualLogic PS存储数据恢复全过程整理
查看>>
《Node.js入门经典》一2.3 安装模块
查看>>
《Java 开发从入门到精通》—— 2.5 技术解惑
查看>>