开发者社区> master_haku> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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 theapplication.
+关注继续查看

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)

 

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
web service2
引用:http://baike.baidu.com/view/67105.htm 网络管理员行业对网络管理员的要求基本就是大而全,不需要精通,但什么都得懂一些。所以,总结下来,一个合格的网络管理员最好在网络操作系统、网络数据库、网络设备、网络管理、网络安全、应用开发等六个方面具备扎实的理论知识和应用技能,才能在工作中做到得心应手,游刃有余。
836 0
web service hacking
 http://www.soapui.org/SOAP-and-WSDL/web-service-hacking.
484 0
WebService又一个不爽的地方
昨天在做项目时,发现了WebService又一个不人性化的地方,记录于此,希望能帮到遇到类似问题的同学们。 很多大型b/s项目,通常会分成几层,为了重现问题,这里我简化为三层:(以下代码仅出于演示,也许并无太大的实际用途) 1、Model层 放置一些业务需要的实体类(通常这些类要求是可序列化的,以方便后面提到的“服务层"中能被序列化后传递),这里为了演示,弄了三个类: 1.
792 0
JS调用WebService示例
  要想实现JS调用WebService,可以按如下步骤实现(经过测试): 第一步:创建一个WebService    在此处我就创建了一个默认的web服务,并不做什么修改,把主要集力放在怎么实现JS调Web服务上面。
644 0
+关注
master_haku
Master HaKu
502
文章
0
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载