The basic process for creating a simple WCF Service in vs2010 is as follows:
I. Create a WCF ApplicationFirst, create a new WCF Service Application (Framework 4.0 is used as an example here), as shown in. After the application is created, vs automatically generates the simplest WCF project. In this application, contains the most basic
Contract )"And
"Service implementation )".The project is as follows:
Directly compile and generate a wcfservice1.dll file without editing any files.
Ii. Contract in WCF applications)In the generated WCF project, iservice1.cs is
Contract )"(In this example, the contract and service are placed in the same project. In fact, they can be divided into two projects.) The Code is as follows:
[ServiceContract]public interface IService1{ [OperationContract] string GetData(int value); [Operation Contract] CompositeType GetDataUsingDataContract(CompositeType composite);// TODO: Add your service operations here} // Use a data contract as illustrated in the sample below to add composite types to service operations.[DataContract]public class CompositeType{ bool boolValue = true; string stringValue = "Hello "; [DataMember] public bool BoolValue { get {return boolValue; } set {boolValue = value; } } [DataMember] public string StringValue { get {return stringValue; } set {string Value = value; } }}As you can see, the service contract iservice1 is declared as an interface. Two operation contracts, getdata and getdatausingdatacontract, are also included. The data contract compositetype is also declared as a class, which contains two data members: boolvalue and stringvalue.
3. servicebehavior)In the generated WCF application, service1.svc. CS is
"Servicebehavior )"(In this example, the contract and service are placed in the same project. In fact, they can be divided into two projects.) The Code is as follows (service1.svc has other functions, which will be discussed later ):
public class Service1 : IService1{ public string GetData(int value) { return string.Format("You entered: {0}",value); } public CompositeType GetDataUsingDataContract(CompositeType composite) { if (composite== null) { throw new ArgumentNullException("composite"); } if (composite.BoolValue) { composite.StringValue+= "Suffix"; } return composite; }}As you can see, this service1 class implements the iservice1 interface (Service Contract) declared in the contract, and also uses the compositetype class (Data contract ); the functions of getdata and getdatausingdatacontract are implemented. These functions are the functions that the WCF Service allows external programs to call.
4. Host-Based WCF ServiceThere are two common boarding methods: 1) one is to create a hosted application for a group of WCF services and host the service by manually starting the program, all managed applications can be used as the host of the WCF Service, such as the console application, Windows Forms Application, and ASP.. NET applications. We call this service hosting method self hosting ). 2) Another approach is to raise the host for the WCF Service through the existing process activation mode of the operating system, in Windows, process activation methods include IIS, Windows service, or was (Windows Process activation Service). No matter which boarding method is used, in the process of creating servicehost for a service, the WCF framework performs a series of operations. The most important step is to create a service description for the service. In this example, the first type is used as an example to create a winform application as a hosting program, in vs2010, create a common winform Program (taking frameword4.0 as an example), for example:
In the project, add. add a reference to the generated wcfservice1.dll, and then add a button and a label control on the form, such: add a description of the WCF Service to be managed in the winform project, which can be implemented through code or tools. This example is generated in vs2010 using the tools-WCF Service configeration Editor tool. Click Open, create a description file through file-New config in the menu, and then click Create a new service on the service page. The following page is displayed: Click Browse... select the generated wcfservice1.dll, select the service1 service, and click Next. The following page is displayed: select the contract to be used in this interface. By default, click Next, the following page is displayed: select the network communication protocol to be used, select the most common HTTP protocol, and select basic WebService interoperablitity on the next page ), in the following interface, enter the access address. Other programs can access this WCF Service through this address. After completing this step, I still have some settings. I am not clear about the purpose. I will skip this step and click File-Save to generate an app. config file. Paste the file here:
<?xml version="1.0" encoding="utf-8"?><configuration> <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name="NewBehavior0"> <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8585/wcf1/metadata" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="NewBehavior0" name="WcfService1.Service1"> <endpoint address="" binding="basicHttpBinding" bindingConfiguration="" name="ep1" contract="WcfService1.IService1" /> Put the app. config file in the root directory of the winform project (the same directory as form1.cs), and add the file to the project in. Add the processing function of the button control:
private void button1_Click(objectsender, EventArgs e){ System.ServiceModel.ServiceHosthost =new System.ServiceModel.ServiceHost(typeof(WcfService1.Service1)); host.Open(); this.label1.Text= "opened";}
Compile and run the program. click the button control on the page to host the WCF Service to the frm. (close the form and the WCF Service ends ).Note: If this error is reported during running (HTTP cocould not register URL http: // +: 8585/wcf1 /. your process does not have access rights to this namespace), which may be caused by insufficient permissions and must be run as administrator. 5. Access and call the WCF Service on the clientCreate a winfrm Project (do not close the boarding page), and click Project-add service reference in the vs menu .. enter the address defined in the boarding interface to add reference to the WCF Service. For example, you can call the getdata method in the WCF Service by using the following code in the program: servicereference1.service1clientaa = newservicereference1.service1client (); MessageBox. show (AA. getdata (2 ));