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 WindowsForms application, or a web application. This hosting method gives you full controlover the lifetime of the WCF service. It is very easy to debug and deploy, andsupports all bindings and transports. The drawback of this hosting method is thatyou have to start the hosting application manually and it has only limited supportfor high availability, easy manageability, robustness, recoverability, versioning, anddeployment 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 aninstaller to install the Windows service in the Service Control Manager (or you canuse the .NET Framework Installutil.exe utility)