程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> Knowledge Conclusion - PART VI: WCF

Knowledge Conclusion - PART VI: WCF

編輯:C#入門知識

/*by garcon1986*/

 

 

1. What is WCF and how it works?

Windows Communication Foundation is a framework for intercommunication between different applications regardless of their languages.

Example:


IHelloWorldService.cs:


using System.Runtime.Serialization; //used for DataContract.
using System.ServiceModel; //used for ServiceContract.
//it contains the types necessary to build Windows Communication Foundation (WCF) service and client applications.

namespace MyWCFServices
{
    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string GetMessage(string name);

        [OperationContract]
        string GetPersonCompleteInfo(Person person);
    }

    //DataContract is used to be transmitted between web service and client
    [DataContract]
    public class Person
    {
        [DataMember]
        public int Age { get; set; }

        [DataMember]
        public string Sex { get; set; }

        [DataMember]
        public string Name { get; set; }
    }
}
HelloWorldService.cs:

 

    /// <summary>
    /// Create a service class and implement service interface
    /// </summary>
    public class HelloWorldService : IHelloWorldService
    {
        public string GetMessage(string name)
        {
            return "Hello world from " + name + "!";
        }

        public string GetPersonCompleteInfo(Person compInfo)
        {
            return "Person complete info( age is:" + compInfo.Age + ";sex is:" + compInfo.Sex + ";name is:" + compInfo.Name + ")";
        }
    }
2. WCF vs ASMX web service:
AXMX web service is just add WebMethod attribute to methods.
Example:


[WebMethod]
public string MyMethod(string text)
{    
return "Hello" + text;
}
WCF use interface , the class implements interface which provide encapsulation and security.

 

 

3. WCF binding types:

 

BasicHttpBinding is designed to replace ASMX Web services. It supports both HTTP and Secure HTTP. As far as encoding is concerned, it provides support for Text as well as MTOM encoding methods. BasicHttpBinding doesn’t support WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging.

WsHttpBinding also supports interoperability. With this binding, the SOAP message is, by default, encrypted. It supports HTTP and HTTPS. In terms of encoding, it provides support for Text as well as MTOM encoding methods. It supports WS-* standards like WS-Addressing, WS-Security and WS-ReliableMessaging. By default, reliable sessions are disabled because it can cause a bit of performance overhead.

reference:
http://www.codeproject.com/Articles/431291/WCF-Services-Choosing-the-appropriate-WCF-binding

4. EndPoint                                                                           

 

<endpoint address="http://localhost:8080/HostDevServer/HelloWorldService.svc"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloWorldService"
                contract="HelloWorldService.IHelloWorldService" name="WSHttpBinding_IHelloWorldService">
                <identity>
                    <userPrincipalName value="username" />
                </identity>
            </endpoint>
ABC: Address, Binding, Contract

 

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved