WCF service -- 发布到IIS 上 + 客户端请求

 服务端行为

1. 创建wcf service

2. 项目中添加方法Connect() --> 便于客户端访问时判断连接是否成功

3. 在web.config中添加配置(客户端访问如果有文件, 最好设置最大访问量)

  在system.serviceModel 节点下添加配置

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpBinding>
      <basicHttpsBinding>
        <binding maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
        </binding>
      </basicHttpsBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpBinding" scheme="http" />
      <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

4. 发布到IIS 上(支持http和https)

 

客户端行为 (http与https请求)

1 add service reference

2. 客户端访问代码(客户端访问如果有文件, 最好设置最大访问量)

try
            {
                var url = "http://localhost:9991/Service1.svc";
                IService1 _service;
                if (url.StartsWith("https://"))
                {
                    ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
                    var basicHttpsBinding = new BasicHttpsBinding()
                    {
                        OpenTimeout = new TimeSpan(0, 5, 0),
                        CloseTimeout = new TimeSpan(0, 5, 0),
                        SendTimeout = new TimeSpan(0, 5, 0),
                        ReceiveTimeout = new TimeSpan(0, 5, 0),
                        MaxReceivedMessageSize = 2147483647,
                        MaxBufferSize = 2147483647,
                        MaxBufferPoolSize = 2147483647
                    };
                    _service = new Service1Client(basicHttpsBinding, new EndpointAddress(url));
                }
                else
                {
                    var basicHttpBinding = new BasicHttpBinding()
                    {
                        OpenTimeout = new TimeSpan(0, 5, 0),
                        CloseTimeout = new TimeSpan(0, 5, 0),
                        SendTimeout = new TimeSpan(0, 5, 0),
                        ReceiveTimeout = new TimeSpan(0, 5, 0),
                        MaxReceivedMessageSize = 2147483647,
                        MaxBufferSize = 2147483647,
                        MaxBufferPoolSize = 2147483647
                    };
                    _service = new Service1Client(basicHttpBinding, new EndpointAddress(url));
                }
                var connect = _service.Connect();
                Console.WriteLine(connect);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();

 

转载于:https://www.cnblogs.com/zxhome/p/10929585.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值