Knowledge Conclusion - PART VI: WCF

3 篇文章 0 订阅

/*

Author: Jiangong SUN

*/

Last update: 18/06/2013


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


5. WCF vs REST


WCF Service is Microsoft's implementation of SOAP. There are others implementations or you could roll your own (not recommended).

SOAP is a kind of stateful, session-based, message-based web service. It's good if your service is designed as a set of complex actions.

REST is a stateless, sessionless, resource-based web service. It's good if your service is designed to access data and perform simple CRUD operations on it. SOAP and REST are mutually exclusive. A service cannot be both. There are ways to manipulate vanilla WCF to make is RESTful but these techniques are becoming deprecated. If you want to implement a RESTful web service there are two main choices in the Microsoft world: WCF Data Services and ASP.NET Web API.


WCF binding: basicHttpBinding, wsHttpBinding

REST binding: webHttpBinding, mexHttpBinding

reference:

http://msdn.microsoft.com/en-us/magazine/dd315413.aspx#id0070023

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值