WCF Quick Start -- fully-automated compilation of WCF Quick StartProgram
I think there are three methods to compile a WCF program:
1. Fully Automated writing
2. semi-automated writing
3. Manual writing
Automatic ModeWriteWCF Program
Why is it called automatic writing mode? It is mainly because the service is created using a system template, the tool is used to configure the service, and IIS is used as the host. We only need to write a little bitCodeYou can.
The function of the example is to obtain the string"Hello, the WCF automatic service call is successful!".
Procedure
Refer to the "basic architecture of the WCF program" figure in "WCF Quick Start-WCF Introduction". We can see that we want to create three projects:
1) Service
2) the platform that runs the service. Because this example uses IIS as the host, this project does not need to be created.
3) Client
Note: The following programs are written in vs2010.
I. BuildingCubeCase
1. Create an empty solution wcfauto.
2. Create two projects in the empty solution wcfauto, as shown in the following table.
Project name |
Type |
Purpose |
Wcfautoservice |
WCF Service Application |
Used to createWCFService |
Userclient |
Console Application |
Client. Used for callingWCF |
Modify some names in the project. The final solution is as follows.
2. Compile the service, that is, compile the wcfautoservice Project
Based on the basic architecture diagram of the WCF program, we can see that the Service consists of three parts: Address, contract, and binding. When creating the WCF Service Application project, the system has helped us build the contract and service type.
2.1 prepare a contract
The contract is actually an interface modification. ModifyIautoserviceTo the following code.
Note: Contract useServicecontractIdentifier for operationOperationcontractId.
Namespace Wcfautoservice
{
/*
Contract
*/
[Servicecontract]
Public Interface Iautoservice
{
[Operationcontract]
String Getdata ();
}
}
2.2 write service types
The service type is contract implementation. ModifyAutoservice,The code below.
Namespace Wcfautoservice
{
/*
Service Type
*/
Public Class Autoservice: iautoservice
{
Public String Getdata ()
{
Return " Hello, the WCF automatic service call is successful! " ;
}
}
}
2.3 modify web. config
Set"Wcfautoservice"At the bottom of Web. config in the projectSystem. servicemodelPartially turn into the following code. For the service configuration method, see "simple configuration instructions for the WCF host configuration file app. config ".
< System. servicemodel >
< Services >
< Service Behaviorconfiguration = "Wcfautoservice. service1behavior"
Name = "Wcfautoservice. autoservice" >
< Endpoint Address = "" Binding = "Wshttpbinding" Contract = "Wcfautoservice. iautoservice" >
< Identity >
< DNS Value = "Localhost" />
</ Identity >
</ Endpoint >
< Endpoint Address = "Mex" Binding = "Mexhttpbinding" Contract = "Imetadataexchange" />
</ Service >
</ Services >
< Behaviors >
< Servicebehaviors >
< Behavior Name = "Wcfautoservice. service1behavior" >
< Servicemetadata Httpgetenabled = "True" />
< Servicedebug Includeexceptiondetailinfaults = "False" />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
</ System. servicemodel >
In this way, the basic service has been created.
3.Compile the running service, that is, compile the host
After the service is compiled, let it run. There are two ways to run the "WCF Service Application" project: one is to run the service through IIS, and the other is to create a program to run the service by yourself. Because the fully-automated method is used to compile the WCF Entry Program, IIS is used to start WCF, that is, IIS is used as the WCF host.
How to run services using IIS:
1) Find the root directory of the wcfautoservice project and set this directory to a virtual directory.
Modify the properties of the virtual directory: Allow Anonymous Access; set the default startup document to "autoservice. SVC ". For example.
2) Test Service
Enter "http: // localhost/wcfautoservice/autoservice. SVC" in the browser address ". The following page is displayed. Indicates that the WCF Service is ready to run.
4. Compile the client
4.1 Add service reference
Right-click the "userclient" project and choose "add service reference )". For example.
Enter the SVC path in the virtual directory configured in IIS, "http: // localhost/wcfautoservice/autoservice. SVC ". Click Go. In namespace, enter the namespace of the service on the client.
4.2 compile client code
InProgram. CS to call the service, the Code is as follows.
Namespace Userclient
{
Class Program
{
Static Void Main ( String [] ARGs)
{
Using (Wcfautoservice. autoserviceclient Client = New Wcfautoservice. autoserviceclient ())
{
String Result = Client. getdata ();
Console. writeline ( " The WCF call result is: {0} " , Result );
Console. readkey ();
}
}
}
}
4.3 run the program
Set the "userclient" project to start the project and run it. The result is shown in.
In this way, the "fully automated way to write the WCF program" is complete. Key words such as "host", "contract", and "endpoint" are not described in detail, however, it will be easier to understand after three entry-level programs are compiled.
Download source code: wcfauto.rar