Create and invoke a WCF full instance using C # (Windows Service Host)

Source: Internet
Author: User

About the concept of WCF, principles, advantages and disadvantages, and so on, there is not much to say, online a lot, you can search by themselves, than I explain the more professional.

This is where the use of Windows Services (Windows Service) is implemented as a host, and other ways are not included in this tutorial.

At the end of this article there is a download connection for this example, including source code, instruction tutorial, post-compilation file

Instance environment: Windows Server R2 + VS (C #)

Open VS 2010, select New Project, select WCF | WCF Service Library, note that this is the WCF Service library, not the WCF service application

When you are sure, the default file "Service1.cs" and "IService1.cs" are created automatically, which is the example that the system gives you.

Open "IService1.cs", you can see the system to your sample code, the cursor to the "IService1", press F2 Jian, you can reset the interface to the name you want, renamed the solution Manager file also changed. I changed to "Imyfirstservice"; open "Service1.cs" and modify it in the same way

Back to Myfirstservice, where I defined two functions "Base64encrypt" and "Base64decrypt", please note the format, [OperationContract] must have, otherwise external unreachable

Switch to "Myfirstservice", interface implementation, write specific code.

OK, the Code section of the WCF Service Library is finished, and the following is the beginning of the configuration, open the App. Config file under the project

Here, we mainly look at the System.ServiceModel node, there are 2 of the child node "services" and "behaviors", where "behaviors" node is mainly the security and debugging settings, we do not discuss here, mainly about the 1th sub-node " Services, let's take a look at the Services node content

It is estimated that most people are like me, these are what mess things, head big. Since the beginning said, this article only say how to achieve, so do not say its why, certainly, such a configuration can not be successfully invoked in the subsequent host, for what, because the lack of the binding configuration, and the default does not generate configuration node, then we have to add, pay attention to the identity of the place

Configure the project properties, my build directory in F:\WCFTEST, build the project

OK, the WCF Service Library has finished working.

Start Host Program

Right-click the solution, add | new project "Windows" | Windows Services ", the project name is the default, no change

To add a reference to a WCF service library in a Windows service project

Open the "WindowsService1" project's app. config and find nothing

Copy the contents of the App. Config file in the "Mytestwcfservicelibrary" project (that is, the content of the app. Config file for 2 items is exactly the same).

To start writing the code for the Windows service, first add the System.ServiceModel reference and the Using

Add Service Installer

Configuring the Setup program

OK, the WCF host program is also over, genetic project

Start writing client Calls

Right-click Solution | add | New Project | Windows|windows form application, the project name is the default (WindowsFormsApplication1), add 2 label,2 textbox,3 a button

Right-click reference | Add Service reference, service address in the Windows Service project in the App. Config file, expand to locate the System.servicemodel|services|service|host|baseaddresses|add node , the string in double quotes after the baseaddress equals sign is the service address, and note that if it is a production environment, the port number used by your service is turned on. This example address is "http://localhost:8732/Design_Time_Addresses/MyTestWcfServiceLibrary/Service1/"

(in the tutorial in download connection this figure uses the error, should be such diagram, the tutorial is used in WindowsService1 in the Service reference diagram)

At this point, the service address is localhost, so you can directly reference the success, if the service address is a network address, you need to start the service in the host server before you can reference the service, otherwise you will not be prompted to find the service. In other words, if your service address is "http://192.168.1.1:8732/Design_Time_Addresses/MyTestWcfServiceLibrary/Service1/", Then you need to install and start the service before you can add a service reference

Client code

Set as Startup Project, run

Coding

Decoding

All the text is written in this download (written so detailed, if not done, then spend some money it ^_^)

http://download.csdn.net/detail/meerio/9215083

Create and invoke a WCF full instance using C # (Windows Service Host)

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.