4 WCF中的RPC和OneWay


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);
    }
}
IHomeService
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);
        }
    }
}
HomeService

 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"/>,當然這個可以設置,正式由於請求響應的這個頭不同,服務才知道哪是請求、哪是響應


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM