C# 建立一個WCF服務

作代碼統計,方便之後使用:web

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服務會生成基本內容,服務分紅兩個,一個去作接口,一個去實現接口:app

接口類:spa

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類:code

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服務orm

相關文章
相關標籤/搜索