This example describes how to use Windows Service to host the WCF Service:
1. Add the WCF Service Library project. There is nothing to say about this step.
To perform a simple test, only the following service method is retained:
[Csharp]View plaincopy
- Public class Service1: IService1
- {
- Public string GetData (int value)
- {
- Return string. Format ("You entered: {0}", value );
- }
- }
2. Add a Windows Service project to implement Service1
[Html]View plaincopy
- Public partial class Service1: ServiceBase
- {
- Public Service1 ()
- {
- InitializeComponent ();
- }
- Private ServiceHost _ host;
- Protected override void OnStart (string [] args)
- {
- _ Host = new ServiceHost (typeof (WinSvcHostWcfLib. Service1 ));
- _ Host. Open ();
- }
- Protected override void OnStop ()
- {
- If (_ host! = Null)
- {
- _ Host. Close ();
- (_ Host as IDisposable). Dispose ();
- }
- }
- }
Note: The typeof in ServiceHost is the Wcf Service. Because it is called "Service1", be sure to use namespace to distinguish it.
Add reference to the WCF Service Library project and System. ServiceModel. dll
On the design form of Service1.cs (Windows Service class), right-click "Add Installer" to Add a project Installer class to the project.
(1) Modify ServiceName of serviceInstaller1 attribute in ProjectInstaller. cs designer to "WinSvcHostWcf"
(2) modify the Account of the serviceProcessInstaller1 attribute in the ProjectInstaller. cs designer to LocalSystem (default: User)
(3) to be able to Debug the Service, add the above script to the Project compilation attribute and install the Service with installutil.exe. $ (TargetPath) is the absolute path of the Project compilation macro to indicate the output.
Delete first:
% Systemroot % \ Microsoft. NET \ Framework \ v4.0.30319 \ installutil.exe/u $ (TargetPath)
Add more
% Systemroot % \ Microsoft. NET \ Framework \ v4.0.30319 \ installutil.exe $ (TargetPath)
(4) Compile the WinSvcHost project and you will see that the WinSvcHostWcf Service is in the Windows Service list. (Use the console command Services. msc to view details)
3. Debug the Windows Service and Debug the hosted Process by using Debug-> Attach to Process.
Of course, the code in OnStart cannot be debugged, because the hosted process can only be seen after normal start.
However, you can use a Thread to start the Service. Added Thread. Sleep () to delay calling of real code. The following code uses delegate. BeginInvoke to delay the call.
You can also use the OnStart input parameter args to control whether latency is required, making debugging more flexible.
4. Create a Console project to test the normal operation of the WCF Service:
[Csharp]View plaincopy
- Static void Main (string [] args)
- {
- Var channel = new ChannelFactory <WinSvcHostWcfLib. IService1> (new WSHttpBinding (),
- "Http: // localhost: 8732/WinSvcHost/Service1 /");
- Var svc = channel. CreateChannel ();
- Var result = svc. GetData (300 );
- Console. WriteLine (result );
- Console. Read ();
- }
Run the output: You entered: 300
5. Create and install the deployment Program
(1) Add primary output
(2) modify the custom Action. ServiceInstaller has been added to WinSvcHost. You can call the primary output in the following steps.
Note: Run installutil.exe/u to clear the WinSvcHostWcf service installed during debug, and then run the generated msi installer.