C# 建立一個WCF服務

來源:互聯網
上載者:User

標籤:

做代碼統計,方便以後使用:

app.config設定檔設定:

<configuration>  <system.serviceModel>    <bindings>      <webHttpBinding>        <binding name="webBinding" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647">          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>        </binding>      </webHttpBinding>    </bindings>    <behaviors>      <serviceBehaviors>        <behavior name="mySerBeh">          <serviceMetadata httpGetEnabled="true"/>          <!--httpGetUrl="mex"-->          <!-- 要接收故障異常詳細資料以進行調試,請將以下值設定為 true。在部署前設定為 false 以避免泄漏異常資訊-->          <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="false"/>        </behavior>      </serviceBehaviors>      <endpointBehaviors>        <behavior name="webHttpendBehavior">          <webHttp></webHttp>        </behavior>      </endpointBehaviors>    </behaviors>    <!-- 看到services節,就表明這是在定義服務相關的內容 -->    <services>      <!-- 定義一個服務,name是契約實作類別的全名 -->      <service behaviorConfiguration="mySerBeh" name="WCFExample.WCF.UserService">        <host>          <baseAddresses>            <add baseAddress="http://127.0.0.1:21467/"/>          </baseAddresses>        </host>        <!-- 定義一下終節點,address一般為空白,如果不為空白,最終服務地址就是在baseAddress的基礎上加上這個address,binding指定為basicHttpBinding,        這是最基礎的基於http的綁定方式,contract標明這是為哪個契約服務 -->        <endpoint address="wcfs" behaviorConfiguration="webHttpendBehavior"          binding="webHttpBinding" bindingConfiguration="webBinding" contract="WCFExample.WCF.IService"></endpoint>      </service>    </services>  </system.serviceModel></configuration>

基本內容可以直接建立一個wpf服務會產生基本內容,服務分成兩個,一個去做介面,一個去實現介面:

介面類:

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;using System.ServiceModel.Web;namespace WCFExample.WCF{    //需要引用類庫 System.ServiceModel 和 System.ServiceModel.Web    [ServiceContract]    public interface IService    {        [OperationContract]        [WebInvoke(Method = "GET", UriTemplate = "GetCon?op={name}", ResponseFormat = WebMessageFormat.Json)]        string GetCon(string name);    }}

實現方法類:

using System;using System.Collections.Generic;using System.Linq;using System.Runtime.Serialization;using System.ServiceModel;using System.Text;namespace WCFExample.WCF{    /// <summary>     /// 用ServiceBehavior為契約實作類別標定行為屬性,此處指定並行存取模型為ConcurrencyMode.Multiple,即並發訪問     /// </summary>     [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]    public class UserService : IService    {        public string GetCon(string name)        {           return name;        }    }}

啟動WCF類:

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.ServiceModel;using System.Threading;namespace WCFExample.WCF{    public class WCFService    {      public  static void Begion()        {            //無設定檔App.config的情況下手動綁定            //Uri HttpUri = new Uri("Http://localhost:21467/wcf");            //Type Servicetype = typeof(WCFExample.WCF.UserService);            //using (ServiceHost Chost=new ServiceHost (Servicetype,new Uri[]{HttpUri}))            //{            //    Binding basicHttpBinding = new BasicHttpBinding();            //    string address = "";            //Chost.AddServiceEndpoint(typeof(WCFExample.WCF.UserService), basicHttpBinding, address);            //    Chost.Open();            //    Console.WriteLine("Service Running...");            //    Console.ReadKey(true);            //    Chost.Close();            //}            //定義一個ServiceHost,注意參數中要使用契約實作類別而不是介面            ServiceHost host = new ServiceHost(typeof(WCFExample.WCF.UserService));            host.Open();            while (true)                Thread.Sleep(1000);        }    }}

服務啟動後,即可正常使用WCF服務

 

C# 建立一個WCF服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.