WCF instance (with steps)
Copy codeThe Code is as follows: <xmlnamespace prefix = "o" ns = "urn: schemas-microsoft-com: office"/>
This article is transferred from Baidu document and I have tried it myself. It can be used.
Using ticket booking as an Example
Create a wcf Service Application
Define a service contract in IService1.csCopy codeThe Code is as follows: namespace WcfDemo
{
// Note: If you change the Interface Name "IService1", you must also update the reference to "IService1" in Web. config.
[ServiceContract] // the service contract is the interface or class that provides the service
Public interface IService1
{
[OperationContract]
/* How to add a ticket */
Void AddTicket (int count );
[OperationContract]
/* How to buy a ticket */
Int BuyTickets (int Num );
[OperationContract] // the service contract is the implementation method for providing services.
/* Method for querying tickets */
Int GetRemainingNum ();
// Task: add service operations here
}
// Use the data conventions described in the following example to add a composite type to a service operation.
[DataContract] // data contract
Public class Ticket
{
Bool boolCount = true; // determines whether there are tickets.
Int howmany = 10; // how many tickets are there?
[DataMember]
/* Determine whether there are tickets */
Public bool BoolCalue
{
Get {return boolCount ;}
Set {
If (howtasks> 0)
{
BoolCount = false;
}
Else
{
BoolCount = true;
}
}
}
[DataMember]
/* Number of returned votes */
Public int HowMany
{
Get {return howmany ;}
Set {howtasks = value ;}
}
}
}
Implement Contract Service in Service1.svcCopy codeThe Code is as follows: namespace WcfDemo
{
// Note: If you change the class name "Service1", you must also update the reference to "Service1" in the Web. config file and the associated. svc file.
Public class Service1: IService1
{
Ticket T = new Ticket ();
/* Method for adding votes */
Public void AddTicket (int count)
{
T. HowMany = T. HowMany + count;
}
/* Method for returning the number of votes */
Public int GetRemainingNum ()
{
Return T. HowMany;
}
/* How to purchase a ticket */
Public int BuyTickets (int Num)
{
If (T. BoolCalue)
{
T. HowMany = T. HowMany-Num;
Return 1;
}
Else
{
Return 0;
}
}
}
}
Add Host Program for Monitoring Service
Add a WinForm project to the solution
The interface is shown in the following figure:
Two buttons on the interface:
Start Service button: used to start the wcf Service
Stop Service button: used to stop the wcf Service
Label: used to display service-related information
The background code is:
Application namespace using System. ServiceModel;
Add a dll file that references the wcf ServiceCopy codeThe Code is as follows: public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
ServiceHost host = null; // defines ServiceHost
Private void button#click (object sender, EventArgs e)
{
Host = new ServiceHost (typeof (WcfDemo. Service1); // WcfDemo. Service1 is the service in the referenced dll
Host. Open (); // start the service
This. label1.Text = "service started ";
}
Private void button2_Click (object sender, EventArgs e)
{
If (host. State! = CommunicationState. Closed) // determines whether the service is disabled.
{
Host. Close (); // Close the service
}
This. label1.Text = "the service is closed ";
}
}
Next, configure app. config.Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<System. serviceModel>
<Services> <! -- Add a service -->
<Service name = "WcfDemo. Service1" behaviorConfiguration = "CalculatorServiceBehavior">
<! -- Name must be the same as the host instance initialization service in the code.
BehaviorConfiguration behavior configuration -->
<Host>
<BaseAddresses>
<! -- Add the call service address -->
<Add baseAddress = "http: // localhost: 8000/"/>
</BaseAddresses>
</Host>
<! -- Add the contract interface contract = "WcfDemo. IService1" WcfDemo. IService1 as the contract interface binding = "wsHttpBinding" wsHttpBinding is called through Http -->
<Endpoint address = "" binding = "wsHttpBinding" contract = "WcfDemo. IService1"> </endpoint>
</Service>
</Services>
<! -- Defines the behavior of CalculatorServiceBehavior -->
<Behaviors>
<ServiceBehaviors>
<Behavior name = "CalculatorServiceBehavior">
<ServiceMetadata httpGetEnabled = "true"/>
<ServiceDebug includeExceptionDetailInFaults = "false"/>
</Behavior>
</ServiceBehaviors>
</Behaviors>
</System. serviceModel>
</Configuration>
Program running result:
After the service is started, you can view the Wcf Service through the baseAddress in the baseAddress node of appConfig.
Now that the service and service host have been created, create a test client!
Create a WinForm program as our test Client
One label for two buttons on the Interface
Ticket purchase: Call the BuyTickets () method of the wcf Service
Querying tickets: Call the GetRemainingNum () method of the wcf Service
Label is used to display running information
Enter the baseAddress address in appconfig of the service host for the project and click "go" (when adding a service reference, it is in the Service Startup State)
The background code is:Copy codeThe Code is as follows: public partial class Form2: Form
{
Public Form2 ()
{
InitializeComponent ();
}
ServiceReference1.Service1Client TClient = new WinFormsClient. ServiceReference1.Service1Client ();
// Declare the client call
Private void button#click (object sender, EventArgs e)
{
Int I = TClient. BuyTickets (2); // call the method in WCF
If (I = 1)
{
This. label1.Text = "purchased successfully ";
}
This. label1.Text + = "remaining tickets" + TClient. GetRemainingNum (). ToString ();
}
Private void button2_Click (object sender, EventArgs e)
{
This. label1.Text = "";
This. label1.Text = TClient. GetRemainingNum (). ToString (); // call the method in WCF
}
}
When purchasing a ticket, call the BuyTicket () method of wcf and return the information of the remaining ticket.
Call GetRemainingNum () of wcf to view the ticket information.
The running result is as follows:
Click to purchase a ticket:
When you click to query the number of votes: