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)

 

时间: 2024-10-12 11:19:57

Hosting the WCF service的相关文章

MVC应用程序使用Wcf Service

前一篇Insus.NET有演示过MVC应用程序使用Web Service, 此篇Insus.NET想继续演示Service,不过是WCF Service. 两者实施起来,多少有些不一样. 在Services目录下,创建一个Calculator.svc 服务.创建成功之后,它会生生成一个接口以及一个svc的文件: 在Calculator.svc中,它是实作上面的接口,而且均实现了四个方法: 更多精彩内容:http://www.bianceng.cnhttp://www.bianceng.cn/we

WCF后续之旅(3) WCF Service Mode Layer 的中枢—Dispatcher

在本系列的第一部分.第二部分中,我们对WCF的channel layer进行了深入的讨论.我们接下来继续讨论WCF的service mode layer.本篇文章着重介绍service 端的ServiceMode.写作此篇文章旨在达到以下两个目的: 希望读者对ServiceMode有一个大致的了解,结合前面介绍的channel layer的相关知识,帮助读者了解WCF的整个实现机制和执行的流程. 介绍ServiceMode涉及到的绝大部分extension point,让读者在具体的项目开发中能

如何使用WCF Service Web Role

这篇文章我们主要在local development fabric上创建一个WCF服务角色,并且在一个控制台应用程序中使用它. Windows Azure是一个基于Silverlight的软件,它的开发门户现在也已经焕然一新了.它的所有信息,所有的操作都可以在一个页面中完成.使用这个全新的门户,从配置guest操作系统到停止或重启一个服务,几乎在一个页面上,就可以把所有的事情完成.在本文中,我们将会创建一个WCF服务角色,把它托管在local development fabric上,然后在一个控

用JavaScript调用WCF Service

原文:用JavaScript调用WCF Service   原创地址:http://www.cnblogs.com/jfzhu/p/4039604.html 转载请注明出处   前面介绍过<Step by Step 创建一个WCF Service>和<使用WCF的Trace与Message Log功能>,本文介绍一下如何用JavaScript来调用WCF Service. WCF Service的代码如下: IHelloService.cs using System.Service

如何创建一个RESTful WCF Service

原创地址:http://www.cnblogs.com/jfzhu/p/4044813.html 转载请注明出处   (一)web.config文件 要创建REST WCF Service,endpoint binding需要用webHttpBinding,参见<webHttpBinding.basicHttpBinding和wsHttpBinding的区别>. web.config <?xml version="1.0"?> <configuration

Using WCF Service Library

1. Two Ways to create template WCF Service There are a few built-in WCF service templates within Visual Studio 2010; twoof them are Visual Studio WCF Service Library and Visual Studio WCF ServiceApplication.    

WCF Service的一些参考资源

Blog The .NET Endpoint http://blogs.msdn.com/b/endpoint/ 随着云计算的推进,现在是 http://blogs.msdn.com/b/appfabric/  The WCF services ecosystem http://blogs.msdn.com/b/endpoint/archive/2009/11/18/the-wcf-services-ecosystem.aspx · WCF Core Services – Allows full

如何在IIS7.0中Host引用了自定义的DataAccess库的WCF service

问题描述 请教:我以library形式定义了一个WCFService.该Service库,还引用了我的数据访问库,现在我想将该WCFServiceHost到IIS7中,看到网上的帖子,建一个App_Code,将自己的代码copy进去,再将config文件copy到和App_Code同级的目录.那如何确保引用的数据访问库可以访问呢?自己感觉下面两点是不对的:1.将数据访问库代码放到app_code目录2.将数据访问库dll放到app_code目录请大家赐教.... 解决方案 解决方案二:一般是连接

Azure开发者任务之八:使用WCF Service Web Role

Accenture的CIO Frank Modruson说,云让Accenture的IT变得更好,更快,更便宜了.在他正在准备进一步的IaaS(Infrastructure as a Service)迁移计划的过程中,他分享了自己关于云计算的5个经验. Frank Modruson有一样更好,更快,更便宜的东西.作为业务和IT服务提供商Accenture的CIO,这是他想要交付给他的内部客户的东西,反过来,这也是他们向客户承诺的东西.Modruson说:"我相信技术,也相信技术可以用多种方式促进