WCF instance-Windows Service Host development, debugging, and installation package Creation

Source: Internet
Author: User

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

  1. Public class Service1: IService1
  2. {
  3. Public string GetData (int value)
  4. {
  5. Return string. Format ("You entered: {0}", value );
  6. }
  7. }


2. Add a Windows Service project to implement Service1

[Html]View plaincopy

  1. Public partial class Service1: ServiceBase
  2. {
  3. Public Service1 ()
  4. {
  5. InitializeComponent ();
  6. }
  7. Private ServiceHost _ host;
  8. Protected override void OnStart (string [] args)
  9. {
  10. _ Host = new ServiceHost (typeof (WinSvcHostWcfLib. Service1 ));
  11. _ Host. Open ();
  12. }
  13. Protected override void OnStop ()
  14. {
  15. If (_ host! = Null)
  16. {
  17. _ Host. Close ();
  18. (_ Host as IDisposable). Dispose ();
  19. }
  20. }
  21. }

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

  1. Static void Main (string [] args)
  2. {
  3. Var channel = new ChannelFactory <WinSvcHostWcfLib. IService1> (new WSHttpBinding (),
  4. "Http: // localhost: 8732/WinSvcHost/Service1 /");
  5. Var svc = channel. CreateChannel ();
  6. Var result = svc. GetData (300 );
  7. Console. WriteLine (result );
  8. Console. Read ();
  9. }

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.