WCF入門教程(二) IIS宿主Web服務運用程序

上一篇我們寫了WCF控制檯寄宿運用程序,其實不管宿主是Winform/WPF或者Windows服務,都是通過Button或者服務起停變更ServiceHost的開閉函數,雖換了框架,但是內容還是控制檯程序裏面的程序。

WCF也是一款Web應用發佈,註定它能進行IIS發佈,那麼不禁聯想到Web服務。我們知道,每一個ASP.NET Web服務都具有一個.asmx文本文件,客戶端通過訪問.asmx文件實現對相應web服務的調用。與之類似,每個WCF服務也具有一個對應的文本文件,其文件擴展名爲.svc。基於IIS的服務寄宿要求相應的WCF服務具有相應的.svc文件,.svc文件部署於IIS站點中,對WCF服務的調用體現在對.svc文件的訪問上。

新建一個WCF服務應用程序,自動生成IService1.cs、Service1.svc、Service1.svc.cs。IService1.cs是添加接口方法的地方,把函數寫在IService1接口下。Service1.svc.cs是實現接口類,把實現方法寫在class  Service1中。至於Service1.svc,svc文件相當於asmx文件。

//IService1.cs文件 接口添加函數
[ServiceContract]
public interface IService1
{

    [OperationContract]
    string GetData(int value);

    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);   
    // TODO: 在此添加您的服務操作
    [OperationContract]
    string Run(int distance);
}

//Service1.svc.cs文件函數實現
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;
    }
    public string Run(int distance)
    {
        return "行駛了" + distance.ToString();
    }
}

服務創建到寄宿過程,可以用代碼實現,也可以在*.config文件中配置完成。採用這種文件配置方式,要完成三個部分,即Behavior、Service和Binding,可以看如下代碼:

<system.serviceModel>   
  <services>
    <service name="wcf_IIS.Service1" behaviorConfiguration="">
      <endpoint address="" binding="wsHttpBinding"  bindingConfiguration="NewBindingCnfg" 
        contract="wcf_IIS.IService1"></endpoint>
      <endpoint contract="wcf_IIS.IService1" binding="mexHttpBinding" address="mex" />
    </service>
  </services>
  <!-- 配置服務端可接收最大數據容量  -->
  <bindings>
    <wsHttpBinding>
      <binding name="NewBindingCnfg" maxReceivedMessageSize="655360000" />
    </wsHttpBinding>
  </bindings>
  <behaviors>
    <serviceBehaviors>
      <!-- name務必爲空,否則發佈不了  -->
      <behavior name="">
      <!-- 要接收故障異常詳細信息以進行調試,請將以下值設置爲 true。在部署前設置爲 false 以避免泄漏異常信息 -->
      <serviceDebug includeExceptionDetailInFaults="true"/>
      <!-- 爲避免泄漏元數據信息,請在部署前將以下值設置爲 false -->
      <serviceMetadata httpGetEnabled="true" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>    
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>

address這裏爲空,真實地址以IIS的url。編譯調試,服務相當於掛在vs專供測試人員調試輕量級IIS Express中,在工程屬性——Web中可以看到,這個url就是服務地址,下來我們可以用客戶端訪問。

現在情況我已經掛在本地的IIS中,進行客戶端調用,建一個控制檯客戶端。先討論添加服務引用方式,添加完成,app.config會自動生成xml配置,見下代碼。生成後纔可以去配置一些額外的屬性。

<system.serviceModel>
  <client>
    <endpoint address="http://localhost:8086/Service1.svc" binding="wsHttpBinding"
      bindingConfiguration="WSHttpBinding_IService1" contract="ServiceReference1.IService1"
      name="WSHttpBinding_IService1">
      <identity>
        <servicePrincipalName value="host/Pc-Yang" />
      </identity>
    </endpoint>
    <endpoint address="http://localhost:8086/Service1.svc/mex" binding="wsHttpBinding"
      bindingConfiguration="MetadataExchangeHttpBinding_IService1"
      contract="ServiceReference1.IService1" name="MetadataExchangeHttpBinding_IService1"/>
  </client>
  <bindings>
    <wsHttpBinding>
      <!--<生成後可以自己配置發送端可最大接受數據容量屬性 maxReceivedMessageSize>-->
      <binding name="WSHttpBinding_IService1" maxReceivedMessageSize="100000" />
      <binding name="MetadataExchangeHttpBinding_IService1">
        <security mode="None" />
      </binding>
    </wsHttpBinding>
  </bindings>  
</system.serviceModel>

再看與之匹配的後臺調用函數代碼,注意這裏是兩個終結點,要明確指明用哪個。

class Program
{
    static void Main(string[] args)
    {
        //new時候構造填終結點名稱
        wcf_client.ServiceReference1.Service1Client wcfclient = new wcf_client.ServiceReference1.Service1Client("WSHttpBinding_IService1");

        Console.WriteLine(wcfclient.Run(3));
    }
}

我們再來討論另一種自己代碼創建的訪問代理對象,會報錯" 遠程服務器返回了意外響應: (405) Method Not Allowed",至於原因,可能跟IIS有關,也有可能別的,有興趣的可以再去研究下。

class Program
{
    static void Main(string[] args)
    {
        using (var cf = new ChannelFactory<wcf_IIS.IService1>(new WSHttpBinding(), "http://localhost:8086/"))
        {

            Console.WriteLine(cf.CreateChannel().Run(3));
            Console.Read();
        }
    }
}

最後,說下如何放到IIS上,跟web服務發佈差不多。新建一個網站,地址是本機或服務器,端口自己確定,不衝突就行。物理路徑是指向發佈包,必須確保有這幾個文件存在。

以上就是WCF IIS發佈全過程,寫的有點倉促,比較忙這階段。

總結

IIS發佈,新建服務應用程序,服務端只可通過配置文件來實現服務的創建、寄宿和暴露元數據不能通過代碼;客戶端可以採用引用服務方式來生成代理對象至於代碼寫代理對象,實驗沒成功

WCF控制檯自我寄宿,服務端只可通過配置文件來實現服務的創建、寄宿和暴露元數據也可通過代碼客戶端可以採用引用服務方式來生成代理對象也可代碼寫代理對象

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章