1 创建两个控制台项目
WcfService和WcfClient
在wcfService项目中新建一个wcf服务的文件项(HomeService)会自动附带生成一个IHomeService.cs的文件
using System.ServiceModel; namespace WcfService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IHomeService”。 [ServiceContract] public interface IHomeService { //默认生成的 [OperationContract] void DoWork(string msg); //这个是我后来新加入的接口方法 [OperationContract(IsOneWay =true)] void DoWork_OneWay(string msg); } }
using System; using System.Linq; using System.Net; using System.Net.Sockets; namespace WcfService { // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的类名“HomeService”。 public class HomeService : IHomeService { public void DoWork_OneWay(string msg) { Console.WriteLine($"这是OneWay通讯,{msg}"); } void IHomeService.DoWork(string msg) { var ip = Dns.GetHostAddresses(Dns.GetHostName()). FirstOrDefault(i => i.AddressFamily == AddressFamily.InterNetwork).ToString(); var info = string.Format($"当前 request 由 server={ip}返回message={msg}"); Console.WriteLine(info); } } }
2 创建WcfService项目会默认在其配置文件中生成相应的wcf配置
我们请求endPoint中的address,在打开的页面中可以看到有关wsdl的链接
在1中,我们wcf接口定义了两个方法,这两个方法一个是rpc的 一个是oneway的。rpc发送信息有去有回,oneway只去不回(适合用来指示服务器写日志)
以下是wsdl代码:
<?xml version="1.0" encoding="ISO-8859-1"?> <wsdl:definitions xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:tns="http://tempuri.org/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://tempuri.org/" name="HomeService"> <wsdl:types> <xsd:schema targetNamespace="http://tempuri.org/Imports"> <xsd:import namespace="http://tempuri.org/" schemaLocation="http://localhost:8733/Design_Time_Addresses/WcfService/HomeService?xsd=xsd0"/> <xsd:import namespace="http://schemas.microsoft.com/2003/10/Serialization/" schemaLocation="http://localhost:8733/Design_Time_Addresses/WcfService/HomeService?xsd=xsd1"/> </xsd:schema> </wsdl:types> <wsdl:message name="IHomeService_DoWork_InputMessage"> <wsdl:part name="parameters" element="tns:DoWork"/> </wsdl:message> <wsdl:message name="IHomeService_DoWork_OutputMessage"> <wsdl:part name="parameters" element="tns:DoWorkResponse"/> </wsdl:message> <wsdl:message name="IHomeService_DoWork_OneWay_InputMessage"> <wsdl:part name="parameters" element="tns:DoWork_OneWay"/> </wsdl:message> <wsdl:portType name="IHomeService"> <wsdl:operation name="DoWork"> <wsdl:input message="tns:IHomeService_DoWork_InputMessage" wsaw:Action="http://tempuri.org/IHomeService/DoWork"/> <wsdl:output message="tns:IHomeService_DoWork_OutputMessage" wsaw:Action="http://tempuri.org/IHomeService/DoWorkResponse"/> </wsdl:operation> <wsdl:operation name="DoWork_OneWay"> <wsdl:input message="tns:IHomeService_DoWork_OneWay_InputMessage" wsaw:Action="http://tempuri.org/IHomeService/DoWork_OneWay"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="BasicHttpBinding_IHomeService" type="tns:IHomeService"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/> <wsdl:operation name="DoWork"> <soap:operation style="document" soapAction="http://tempuri.org/IHomeService/DoWork"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> <wsdl:operation name="DoWork_OneWay"> <soap:operation style="document" soapAction="http://tempuri.org/IHomeService/DoWork_OneWay"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> </wsdl:operation> </wsdl:binding> <wsdl:service name="HomeService"> <wsdl:port name="BasicHttpBinding_IHomeService" binding="tns:BasicHttpBinding_IHomeService"> <soap:address location="http://localhost:8733/Design_Time_Addresses/WcfService/HomeService"/> </wsdl:port> </wsdl:service> </wsdl:definitions>
我们看到dowork方法由于是rpc(默认rpc),其节点中有两个节点 一个input 一个output;dowork_OneWay方法是OneWay的(其特性构造函数中OneWay=true),其节点中只有一个input节点
3.有一点要注意的是
rpc的方法 请求的时候默认是 wsaw:Action="http://tempuri.org/IHomeService/DoWork"/> 响应是 wsaw:Action="http://tempuri.org/IHomeService/DoWorkResponse"/>,当然这个可以设置,正式由于请求响应的这个头不同,服务才知道哪是请求、哪是响应
原文:https://www.cnblogs.com/wholeworld/p/10165343.html