服务端行为
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();