Windows Service Demo for the WCF host and Windows Demo for the wcf host

Source: Internet
Author: User

Windows Service Demo for the WCF host and Windows Demo for the wcf host

Try to host the service on the window service. The procedure is as follows:

Entire Solution

1. Create a window service

Wcf. WinService

Namespace Wcf. winService {public partial class CalService: ServiceBase {public ServiceHost serviceHost = null; // service host public CalService () {InitializeComponent (); base. serviceName = "WCF. serviceHostByWindowService ";} protected override void OnStart (string [] args) {if (serviceHost! = Null) {serviceHost. close ();} serviceHost = new ServiceHost (typeof (Cal); serviceHost. open (); base. onStart (args);} protected override void OnStop () {if (serviceHost! = Null) {serviceHost. Close (); serviceHost = null;} base. OnStop ();}}}Window Service Code

 

 Ii. Create a data contract and the wcf Service Class

Namespace Wcf. WinService {[ServiceContract] public interface ICal {[OperationContract] int Add (int a, int B); [OperationContract] string GetValue (string value );}}Service Contract namespace Wcf. winService {public class Cal: ICal {public int Add (int a, int B) {return a + B;} public string GetValue (string value) {return "hello" + value ;}}}Cal Service

 

<? Xml version = "1.0" encoding = "UTF-8"?> <Configuration> <system. serviceModel> <services> <service name = "Wcf. winService. cal "behaviorConfiguration =" basicBehavior ">  

3. Install the window service

1. Double-click CalService and right-click Add Installer. A ProjectInstaller file is automatically added to the project, which contains serviceProcessInstaller1 and serviceInstaller1 components.

2. Set the attribute Account of serviceProcessInstaller1 to LocalSystem, and set ServiceName of serviceInstaller1: WCF. CalService (the name of the final service, which can be set by yourself)

3. Generate a solution. Copy Wcf.WinService.exe and Wcf.WinService.exe. config to disk D under debug.

4. Run the vs2012 developer command as an administrator to install the installUtil D: \ Wcf.WinService.exe service. The service is successfully installed. Go to service management to enable the Service.

 

4. Client segment call

The client references the service address http: // localhost: 8999/CalService. After the service address is referenced, the client configuration file is automatically generated.

Verify on the client.

Download the source code of the entire solution


How to use windows service or winform to host a WCF Service

What are the endpoints required for the Internet Intranet? wenjiebaby says that for a single endpoint, You need to configure the server Nic, that is, resolve the dual Nic. If wenjiebaby doesn't understand it, don't say it!
The endpoint server is published as 127.0.0.1/service.
Then the dual Nic performs Resolution, and the Intranet accesses the server, for example, 192.168.0.252.
Access through the internet: 254.23.23.23

Because I have worked on the wcf product server for two years, this is certainly no problem.

Why is the wcf Service managed? What is the role of the host environment?

The answer from the Microsoft forum is as follows:

1. WebService: strictly speaking, it is an industry standard, not a technology. XML extension markup language is used to represent data (this is the key to boast of languages and platforms ). Microsoft's Web Service implementation is called ASP. NET Web Service. It uses the Simple Object Access Protocol Soap to implement data interaction between applications in a distributed environment. Description of the service interface. In addition, Web services can be registered to the UDDI Center for customers to find and use.
Later, Microsoft implemented ASP. NET Web Service Security, performance, data encryption, decryption, hosting host and other aspects of the extension, called the WSE series, this is an over-product, up to WSE3.0. later is the WCF era.

2. WCF: to some extent, it is ASP. NET Web Service, because it supports the industry standard and Core Protocol of Web Service, ASP. NET Web Service and WSE can do almost all the things they can do. cross-platform and language are not a problem (data also supports XML formatting and provides their own formatter ).
However, as a communication component or platform promoted by Microsoft, WCF aims not only to support and integrate Web Services, because it is also compatible with and has many features of Microsoft's early technology.
According to Microsoft's official explanation, WCF (formerly called "Indigo") is a unified framework for building and running Service-Oriented applications using managed code. It enables developers to build a secure, trusted, and transactional solution across platforms, and to be compatible with existing systems. WCF is a set of developers for Microsoft Distributed Application Development. It integrates. net platform, such as Enterprise Sevices (COM + ). net Remoting, Web Service (ASMX), WSE3.0, and MSMQ message queues. In terms of the scope of communication (Communiation), it can be cross-process, cross-machine, cross-subnet, enterprise network, or Internet; in terms of host programs, it can be ASP. NET, EXE, WPF, Windows Forms, NT Service, COM + as the Host (Host ). The protocols supported by WCF include TCP, HTTP, cross-process, and custom. Security modes include SAML, Kerberos, X509, user/password, and custom. That is to say, it is easy to develop a SOA-based Distributed System under the WCF framework. Microsoft includes all the technical elements related to this, and has mastered the concept, it is equivalent to mastering the key to open the SOA door.


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.