4 WCF中的RPC和OneWay
阿新 • • 發佈:2018-12-23
1 建立兩個控制檯專案
WcfService和WcfClient
在wcfService專案中新建一個wcf服務的檔案項(HomeService)會自動附帶生成一個IHomeService.cs的檔案
using System.ServiceModel; namespace WcfService { // 注意: 使用“重構”選單上的“重新命名”命令,可以同時更改程式碼和配置檔案中的介面名“IHomeService”。 [ServiceContract] public interface IHomeService {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; namespaceHomeServiceWcfService { // 注意: 使用“重構”選單上的“重新命名”命令,可以同時更改程式碼和配置檔案中的類名“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>wsdl
我們看到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"/>,當然這個可以設定,正式由於請求響應的這個頭不同,服務才知道哪是請求、哪是響應