WCF基本应用

  今天是十一假期,在家呆着随便写一篇简单的。想了想就写wcf 吧。wcf是一项很好的技术,去年之前公司用的都是webserves,今年来到这个公司后用的都是wcf,于是便开始学习wcf,发现基本用法不是很难,

  语法就是c#语法, 就是配置的东西多了一些,不经常写的话就容易忘。

  WCF中最主要三个概念就是ABC(A代表Address-where(对象在哪里)B代表Binding-how(通过什么协议取得对象)C代表Contact(契约))

  当然这三个概念细说的话又有好多好多比如说C:通信双方的沟通方式,由合约(Contract)来订定。通信双方所遵循的通信方法,由协议绑定(Binding)来订定。通信期间的安全性,由双方约定的安全性层次来订定

总之,举个例子演示一遍流程就都明白了

如图从新建项目开始到项目源码到结束:

调试方法:

1.使用客户端测试。在客户端项目中添加服务器连接,然后调用接口内的方法即可。

2.使用WCF自带的测试客户端来测试。开打VS自带的命令行工具输入wcftestclient,弹出测试客户端。添加服务连接就可以了。

原文出处:

上代码:如下

原文地址:http://www.cnblogs.com/sixiangqimeng/p/3344473.html

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>

      <services>
          <service behaviorConfiguration="WcfServicetext.IService1Behavior" name="WcfServicetext.Service1">
              <endpoint address="" binding="basicHttpBinding" bindingConfiguration="WcfServicetext.basicHttpBinding" contract="WcfServicetext.IService1" />
          </service>
      </services>

      <behaviors>
          <serviceBehaviors>
              <behavior name="WcfServicetext.IService1Behavior">
                  <serviceMetadata httpGetEnabled="True"/>
                  <serviceDebug includeExceptionDetailInFaults="True" />
                  <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                  <serviceThrottling maxConcurrentCalls="1000" maxConcurrentInstances="1000" maxConcurrentSessions="1000"/>
              </behavior>
          </serviceBehaviors>
      </behaviors>

      <bindings>
          <basicHttpBinding>
              <binding name="WcfServicetext.basicHttpBinding" closeTimeout="00:03:00"
                openTimeout="00:03:00" sendTimeout="00:10:00" maxReceivedMessageSize="20971520"
                messageEncoding="Text" transferMode="Streamed">
                  <readerQuotas maxDepth="3145727" maxStringContentLength="20971520"
                    maxArrayLength="20971520" maxBytesPerRead="20971520" maxNameTableCharCount="3145727" />
                  <security mode="None" />
              </binding>
          </basicHttpBinding>
      </bindings>
      

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServicetext
{
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfServicetext
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);
    }

}
View Code
原文地址:https://www.cnblogs.com/sixiangqimeng/p/3344473.html