WCF basicHttpBinding 实例

来源:互联网 发布:软件对比分析 编辑:程序博客网 时间:2024/06/16 04:26

一、定义契约

using System.Collections.Generic;using System.Linq;using System.ServiceModel;using System.Text;using System.Threading.Tasks;namespace Service.Interface{    [ServiceContract]    public interface IFileUpLoad    {        [OperationContract]        string FileUpLoad();    }}

二、实现契约

using Service.Dal;using Service.Interface;using System;using System.Collections.Generic;using System.Linq;using System.ServiceModel;using System.Text;using System.Threading.Tasks;namespace Service.Implement{    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]    public class FileUpLoadService : IFileUpLoad    {        public string FileUpLoad()        {            return FileUpLoadAccessor.FileUpLoad();        }    }}


会话,实例与并发

InstanceContextMode = InstanceContextMode.PerCall

在这种实例化模式下,客户端对服务方法的每一次调用,服务端都会new一个实例,方法调用结束即销毁,这种模式可能要频繁的花费创建对象和销毁对象的花销,因此性能比较差,但是每次调用后服务对象被销毁,对象所把持的资源也就被释放(如文件啦、数据库连接啦),因此伸缩性是最好的。

InstanceContext = PerCall & ConcurrencyMode = Multiple

InstanceContext = PerCall 为每个顾客要的每个需求都提供一个师傅专门服务(顾客提出要求时现招,师傅做完后就被解雇)

ConcurrencyMode = Multiple 所有的师傅同时工作,每个师傅只用做一件事儿。

这在现实生活中也是不可能存在的。这时客户的满意度理论上是最高的,顾客的每个要求都有师傅在做,而且所有的师傅在同时做事儿。当然这对早点铺的老板要付出的代价是最大的。对师傅能力要求不高,能做好一件事儿就好。



三、数据访问层

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Service.Dal{    public class FileUpLoadAccessor    {        public static string FileUpLoad()        {            return "测试";        }    }}

四、创建WCF


创建 FileUpLoadService.svc 文件,代码如下:

<%@ ServiceHost Language="C#" Debug="true" Service="Service.Implement.FileUpLoadService" %>

Web.config配置文件

<?xml version="1.0" encoding="utf-8"?><configuration>  <appSettings>    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />  </appSettings>  <!--<connectionStrings>    <add name="DB" connectionString="server=.;database=数据库名;Integrated Security=false;UID=sa;PWD=111111;enlist=false;Max Pool Size=1024;" providerName="System.Data.SqlClient" />  </connectionStrings>-->  <system.web>    <compilation debug="true" targetFramework="4.5" />    <httpRuntime targetFramework="4.5"/>  </system.web>  <system.serviceModel>    <behaviors>      <serviceBehaviors>        <behavior>          <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false -->          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>          <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 -->          <serviceDebug includeExceptionDetailInFaults="false"/>        </behavior>      </serviceBehaviors>    </behaviors>        <!--绑定配置-->    <bindings>      <basicHttpBinding>        <binding name="CommonBinding" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed" useDefaultWebProxy="true">          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />          <security mode="None">            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />            <message clientCredentialType="UserName" algorithmSuite="Default" />          </security>        </binding>      </basicHttpBinding>    </bindings>        <!--大量数据导入时,取消注释-->    <!--<services>      <service name="Guoany.Service.Implement.FileUpLoadService">        <endpoint address=""                  binding="basicHttpBinding"                  bindingConfiguration="CommonBinding"                  contract="Guoany.Service.Interface.IFileUpLoad" />        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>      </service>    </services>-->        <protocolMapping>        <add binding="basicHttpsBinding" scheme="https" />    </protocolMapping>        <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  </system.serviceModel>  <system.webServer>    <modules runAllManagedModulesForAllRequests="true"/>    <!--        若要在调试过程中浏览 Web 应用程序根目录,请将下面的值设置为 True。        在部署之前将该值设置为 False 可避免泄露 Web 应用程序文件夹信息。      -->    <directoryBrowse enabled="true"/>  </system.webServer></configuration>

五、测试


六、创建WEB网站,添加客户端节点

<client>  <endpoint address="http://localhost:15929/FileUpLoadService.svc" binding="basicHttpBinding"       bindingConfiguration="CommonBinding" contract="Service.Interface.IFileUpLoad"       name="BasicHttpBinding_IFileUpLoad" /></client>

创建通道工厂

        public ActionResult Index()        {            using (var factory = new ChannelFactory<IFileUpLoad>("*"))            {                var client = factory.CreateChannel();                ViewBag.cs= client.FileUpLoad();            }            return View();        }


0 0