How to host WCF services WCF boarding is a very flexible operation that can be hosted in a variety of processes, with the common boarding of IIS services, Windows Services, WinForm programs, and console programs to enable the operation of the WCF service, for the convenience of the caller, Efficiently deliver service calls.
The signature described earlier that WCF is a common way to host IIS services. This kind of boarding is the most convenient way, and it is widely used because the service only needs to be run automatically by IIS. To create this way of IIS boarding, simply add the WCF service application inside the solution, and you can generate this kind of service module.
Host WCF services into other processes: (such as console applications) 1. New Solution: Add WCF Service Library project.
Modify the configuration items inside the production:
<?xml version="1.0"encoding="Utf-8"?><configuration> <system.web> <compilation debug="true"/> </system.web> <!--When you deploy a service library project, you must add the contents of the configuration file to the Host App. Config file. System.Configuration does not support the library's configuration file. -<system.serviceModel> <services> <service name="WcfService.Library1.Service1"> "http://localhost:20002/Design_Time_Addresses/WcfService.Library1/Service1/"/> </baseAddresses> "WcfService.Library1.Service1"binding="BasicHttpBinding"contract="WcfService.Library1.IService1"> <!--at deployment time, the following identity elements should be removed or replaced to reflect the identity used to run the deployed service. After deletion, WCF automatically infers the identity. -<identity> <dns value="localhost"/> </identity> </endpoint> <!--Metadata Endpoints--<!--metadata Exchange endpoint For the appropriate service to introduce itself to the client. -<!--This endpoint does not use a secure binding, you should ensure it is secure or removed before deployment-<endpoint address="Mex"binding="mexhttpbinding"contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior> ; <!--to avoid leaking metadata information, set the following values before deployment tofalse-<servicemetadata httpgetenabled="True"/> <!--to receive fault exception details for debugging, set the following values totrue。 Before deployment, set tofalseto avoid leaking exception information-<servicedebug includeexceptiondetailinfaults="False"/> </behavior> </serviceBehaviors> </behaviors> </system.servicemodel></conf Iguration>
View Code
Add a console application, introduce a reference to Wcfservice.library1, add a using System.ServiceModel, and a library file reference. Copy the App. Config from Wcfservice.library1 to the console app below. Modify the property to always copy.
1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.ServiceModel;5 usingSystem.ServiceModel.Channels;6 usingSystem.ServiceModel.Description;7 usingSystem.Text;8 usingWcfservice.library1;9 Ten namespaceWcftest.demo2 One { A class Program - { - Static voidMain (string[] args) the { - Coreconfigopen (); - } - + /// <summary> - ///Configure App. config to turn on WCF services by adding + /// </summary> A Static voidCoreconfigopen () at { - Try - { -ServiceHost Selfhost =NewServiceHost (typeof(WcfService.Library1.Service1)); - Selfhost.open (); -Console.WriteLine ("host to the console application and open the WCF service by loading app. config"); in console.readline (); - } to Catch(Exception ex) + { -Console.WriteLine ("corecodeopen Error:"+Ex. Message); the } * } $ }Panax Notoginseng}
View Code
Run the project directly as an administrator after the build.
In the browser address bar, enter the URL of the baseaddress in the configuration file.
You can see that the service is hosted successfully and can be referenced and developed based on the links above. This allows the console application to be hosted simply. The same is true for Windows services and other process boarders. The above is hosted according to the configuration items in the configuration file, and can be implemented in a purely code-based manner in other processes.
Create a WCF service self-hosted