v. Console Application Host
(1) Create a new console output project consolehosting under solution. Such as.
(2) Add a reference to the System.ServiceModel.dll.
(3) Add a project reference to the WCF service class library (Wcfservicelib).
(4) Create the host program with the following code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.ServiceModel;usingSystem.ServiceModel.Description;usingSystem.Text;usingSystem.Threading.Tasks;usingWcfservicelib;namespaceconsolehosting{classProgram {Static voidMain (string[] args) { //Create a host's base addressUri baseaddress =NewUri ("Http://localhost:8080/BookService"); //Create a host using(ServiceHost host =NewServiceHost (typeof(Bookservice), baseaddress)) { //to add an endpoint to the hostHost. AddServiceEndpoint (typeof(Ibookservice),Newwshttpbinding (), baseaddress); if(host. description.behaviors.find<servicemetadatabehavior> () = =NULL) { //set the Httpgetenabled property to TrueServiceMetadataBehavior behavior =NewServiceMetadataBehavior (); Behavior. Httpgetenabled=true; Behavior. Httpgeturl=baseaddress; //add behavior to behaviorshost. DESCRIPTION.BEHAVIORS.ADD (behavior); //Open HostHost. Opened + =Delegate{Console.WriteLine ("Bookservice Console Program boarding has started, HTTP listener has started ...., press any key to terminate the service! "); }; Host. Open (); Console.read (); } } } }}
(5) Run the host program, before running the client to make the call, you need to run the host program first. As shown, the host was established successfully.
Establish client
(1) Re-establish the solution-->windows form Application project. such as.
(2) Add a reference to the service (right-click on the reference and enter the base address of our defined service host (here: Http://localhost:8080/BookService)----go to----See the first section. such as 1, Figure 2.
Figure 1
Figure 2
(2) Add a text box and a button to the From1. Such as.
(3) write the following code in From1.cs.
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacewinclient{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidBtngetbook_click (Objectsender, EventArgs e) {bookservicereference.bookserviceclient Client=Newbookservicereference.bookserviceclient (); stringBook = client. GetBook ("5"); TextBox1.Text=Book ; } }}
(4) test procedures as shown in the success (note: Be sure to run our host program before the line, if the host does not open the error will be: because the target computer actively refused to connect. ). such as.
In this example we put the endpoint in the ABC, the base address, behaviors, etc. are written directly in the code, but the actual application process is to rely on the configuration file, in order to compare the following example will use the configuration file.
vi. Windows application hosting
(1) Create a new Windows Forms Application project winhosting under the solution. such as.
(2) Add a reference to the System.ServiceModel.dll.
(3) Add a project reference to the WCF service class library (Wcfservicelib).
(4) Add application configuration file app. config, this time we use the configuration to advertise the WCF service, and the WCF Service Configuration code is as follows.
<?XML version= "1.0" encoding= "Utf-8"?><Configuration> <Startup> <supportedruntimeversion= "v4.0"SKU=". netframework,version=v4.5.2 " /> </Startup> <System.ServiceModel> <Behaviors> <servicebehaviors> <Behaviorname= "Metadatabehavior"> <Servicemetadatahttpgetenabled= "true"Httpgeturl= "Http://127.0.0.1:8080/BookService/metadata" /> <ServicedebugIncludeexceptiondetailinfaults= "True" /> </Behavior> </servicebehaviors> </Behaviors> <Services> <Servicebehaviorconfiguration= "Metadatabehavior"name= "Wcfservicelib.bookservice"> <EndpointAddress= "Http://127.0.0.1:8080/BookService"binding= "Wshttpbinding"Contract= "Wcfservicelib.ibookservice" /> </Service> </Services> </System.ServiceModel></Configuration>
(5) Create the Host program Form1 form and modify App. config with the following code:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.ServiceModel;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingWcfservicelib;namespacewinhosting{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } ServiceHost Host=NULL; Private voidForm1_Load (Objectsender, EventArgs e) { Try{host=NewServiceHost (typeof(Bookservice)); Host. Opened+=Delegate{Label1. Text="Form Host program, Bookservice, use config file! "; }; Host. Open (); } Catch(Exception ex) {Label1. Text=Ex. Message; } } Private voidForm1_formclosed (Objectsender, Formclosedeventargs e) {host.} Close (); } }}
(6) Run the program as shown .
Establish client
Use the client that we wrote in the console host program to access the WCF service for the Windows Forms host program.
WCF Learning Journey-WCF Service deployment to applications (10)