1. Host in IIS
1. Create a WCF project.
2. Publish the WCF project to IIS.
3. Create a client project and add a service reference.
2. Host in Application
1. Create a new library and compile the WCF Service.
2. Create a console application (or other applications) to host the compiled WCF Service.
The procedure is as follows:
A. Add a reference to the WCF class library project.
B. Compile the bearer function.
Example:
// Create a new servicehost instance to host the service
Using (servicehost host = new servicehost (typeof (helloservice. sayhelloservice )))
{
Host. open ();
Console. Write ("host has started ...");
Console. Readline ();
}
C. Configure app. config
Example:
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. servicemodel>
<Services>
<Service name = "helloservice. sayhelloservice" behaviorconfiguration = "servicebehavior">
<Endpoint binding = "basichttpbinding" Contract = "helloservice. isayhelloservice" address = "helloservice"/>
<Endpoint binding = "mexhttpbinding" Contract = "imetadataexchange" address = "mex"/>
<Host>
<Baseaddresses>
<Add baseaddress = "http: // localhost: 8000"/>
</Baseaddresses>
</Host>
</Service>
</Services>
<Behaviors>
<Servicebehaviors>
<Behavior name = "servicebehavior">
<Servicemetadata httpgetenabled = "true"/>
</Behavior>
</Servicebehaviors>
</Behaviors>
</System. servicemodel>
</Configuration>
3. Create a client application
A. Add the WCF reference in step 2nd, such as baseaddress = "http: // localhost: 8000"/, and configure app. config.
B. Compile the function call service.
Example:
Static void main (string [] ARGs)
{
Sayhelloservicereference. sayhelloserviceclient proxy = new helloclient. sayhelloservicereference. sayhelloserviceclient ();
String S = proxy. sayhello ("wangwei ");
Console. writeline (s );
Console. Readline ();
}
Note: Pay attention to the startup sequence and settings of the startup items in the solution.
Iii. Host in Windows Service
1. Create a new library and compile the WCF Service.
2. Create a new Windows service program to host and configure the compiled WCF Service.
Example:
/**/
/// <Summary>
/// Initialize a new instance of the system. configuration. Install. installer class.
/// </Summary>
[Runinstaller (true)]
Public class projectinstaller: Installer
{
Private serviceprocessinstaller process;
Private serviceinstaller service;
/**/
/// <Summary>
/// Constructor
/// </Summary>
Public projectinstaller ()
{
Process = new serviceprocessinstaller ();
Process. Account = serviceaccount. LocalSystem;
Service = new serviceinstaller ();
// Set the service name (the service name displayed in the control panel service list)
Service. servicename = "wcfbywindowsservice ";
// Set the service description (the service description displayed in the control panel service list)
Service. Description = "the service that hosts the WCF Service in windowsservice ";
Base. installers. Add (process );
Base. installers. Add (service );
}
}
Public partial class windowsservice: servicebase
{
Public servicehost = NULL;
/// <Summary>
/// Main Function
/// </Summary>
Public static void main ()
{
Servicebase. Run (New windowsservice ());
}
Public windowsservice ()
{
Initializecomponent ();
}
Protected override void onstart (string [] ARGs)
{
If (servicehost! = NULL)
{
Servicehost. Close ();
}
// Create a servicehost
Servicehost = new servicehost (typeof (helloservice. sayhelloservice ));
Servicehost. open ();
}
Protected override void onstop ()
{
If (servicehost! = NULL)
{
Servicehost. Close ();
Servicehost = NULL;
}
}
}
App. config
<? XML version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. servicemodel>
<Services>
<! -- Name-name of the class that provides the service -->
<! -- Behaviorconfiguration-specify the relevant behavior configuration -->
<Service name = "helloservice. sayhelloservice" behaviorconfiguration = "helloservicebehavior">
<! -- Address-service address -->
<! -- Binding-Communication Method -->
<! -- Contract-service contract -->
<Endpoint address = "" binding = "wshttpbinding" Contract = "helloservice. isayhelloservice"/>
<! -- Metadata exchange endpoint -->
<! -- Note: The address is MEX, Which is spliced with the baseaddress in the host/baseaddresses node. That is, the address that provides metadata exchange is http: // localhost: 12345/binding/MEX -->
<Endpoint binding = "mexhttpbinding" Contract = "imetadataexchange" address = "mex"/>
<Host>
<Baseaddresses>
<Add baseaddress = "http: // localhost: 11233/wcfbywindowsservicehost/"/>
</Baseaddresses>
</Host>
</Service>
</Services>
<Behaviors>
<Servicebehaviors>
<Behavior name = "helloservicebehavior">
<Servicemetadata httpgetenabled = "true"/>
<Servicedebug includeexceptiondetailinfaults = "false"/>
</Behavior>
</Servicebehaviors>
</Behaviors>
</System. servicemodel>
</Configuration>
3. Install the service
Path of this service: "E: \ vsprojects \ wcfstudyii \ wcfbywindowsservicehost \ host \ bin \ debug \ host.exe"
4. Create a client application, reference the service, and configure the application. For example:
Baseaddress = "http: // localhost: 11233/wcfbywindowsservicehost/"
5. Compile a client program to call the service.
4. Host in was (Windows Process activation Service)
...