Here is a self-implemented WCF example. For beginners like me, you can download the example. The demo has a download link at the end of this article. The following describes several projects in the solution:
1,
Public The wcfcontract class library defines the WCF Service Contract, which is usually an Interface
2,
WCF Server
A. The mywcflib class library must reference system. servicemodel and wcfcontract, which implement all the methods of interfaces in wcfcontract.
B. The mywcfwebhost web project must reference the web host of system. servicemodel, wcfcontract, and mywcflib WCF for external calls.
C. The mywcfwinhost winform project must reference the winform host of system. servicemodel, wcfcontract, and mywcflib WCF to start the WCF Service, and then the open service can be called externally.
Note: In actual projects, You may select either B or C. All examples are provided here for learning and reference.
3,
Client
Myclient console application Program System. servicemodel and two service references.
Note: The "client" here refers to the Web Forms project and win forms project that call the WCF Service. The example is a simple console application.
Next, let's look at two configuration files:
A. Web. config in the getdata folder of the mywcfwebhost project: Code
<? XML version = "1.0" ?>
< Configuration >
< System. servicemodel >
< Behaviors >
< Servicebehaviors >
< Behavior Name = "Getdataservicebehavior" >
<! -- Httpgetenabled-Indicates whether to publish service metadata for retrieval using HTTP/GET requests. If the WSDL is published, the value is true. Otherwise, the value is false. The default value is false. -->
< Servicemetadata Httpgetenabled = "True" />
< Servicedebug Includeexceptiondetailinfaults = "True" />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
< Services >
<! -- Name-name of the class that provides the service -->
<! -- Behaviorconfiguration-specify the relevant behavior Configuration -->
< Service Name = "Mywcflib. getdataservice" Behaviorconfiguration = "Getdataservicebehavior" >
<! -- Address-service address -->
<! -- Binding-Communication Mode -->
<! -- Contract-service contract -->
< Endpoint Binding = "Basichttpbinding" Contract = "Wcfcontract. igetdataservice" />
</ Service >
</ Services >
</ System. servicemodel >
</ Configuration >
Note the class library name and contract. = "Wcfcontract. igetdataservice ", It's not that difficult. You just need to know the meaning of the configuration.
B. App. config IN THE mywcfwinhost project: Code
<? XML version = "1.0" encoding = "UTF-8" ?>
< Configuration >
< System. Web >
< Compilation Debug = "True" />
</ System. Web >
<! -- When deploying a service library project, you must add the configuration file content
In the app. config file of the host. System. configuration does not support database configuration files. -->
< System. servicemodel >
< Services >
< Service Behaviorconfiguration = "Mywcflib. getdataservicebehavior"
Name = "Mywcflib. getdataservice" >
< Endpoint Address = "" Binding = "Wshttpbinding" Contract = "Wcfcontract. igetdataservice" >
</ Endpoint >
< Endpoint Address = "Mex" Binding = "Mexhttpbinding" Contract = "Imetadataexchange" />
< Host >
< Baseaddresses >
< Add Baseaddress = "Http: // localhost: 1234/mygetdataservice/getdataservice" />
</ Baseaddresses >
</ Host >
</ Service >
</ Services >
< Behaviors >
< Servicebehaviors >
< Behavior Name = "Mywcflib. getdataservicebehavior" >
<! -- To avoid metadata leakage,
Set the following value to false before deployment and delete the above metadata endpoint. -->
< Servicemetadata Httpgetenabled = "True" />
<! -- To receive fault exception details for debugging,
Set the value to true. Before deployment
Set to false to avoid leakage of exception information. -->
< Servicedebug Includeexceptiondetailinfaults = "True" />
</ Behavior >
</ Servicebehaviors >
</ Behaviors >
</ System. servicemodel >
</ Configuration >
note: " servicedebug includeexceptiondetailinfaults =" true " />" This line is a configuration for a WCF Service exception. The default value is "false ", this parameter is set to true for debugging exceptions. during deployment, it must be set to "false ".
demo download: wcfstudydemo