¿Æ¼¼ÐÐÕß

ÐÐÕßѧԺ תÐÍ˽¶­»á ¿Æ¼¼ÐÐÕßרÌⱨµÀ Íøºì´óÕ½¿Æ¼¼ÐÐÕß

֪ʶ¿â

֪ʶ¿â °²È«µ¼º½

ÖÁ¶¥ÍøÈí¼þƵµÀ»ù´¡Èí¼þASMX2.0¡¢WSE3.0ºÍWCFÎÊÌâ½â´ð(Ò»)

ASMX2.0¡¢WSE3.0ºÍWCFÎÊÌâ½â´ð(Ò»)

  • ɨһɨ
    ·ÖÏíÎÄÕµ½Î¢ÐÅ

  • ɨһɨ
    ¹Ø×¢¹Ù·½¹«ÖÚºÅ
    ÖÁ¶¥Í·Ìõ

¡¡Microsoft® .NET Framework 2.0 °æͨ¹ý¼¸¸öÓÐȤµ«¿ÉÄÜÁîÈËÀ§»óµÄ·½Ê½ÖØËÜÁË Web ·þÎñµÄÇ°¾°¡£

À´Ô´£ºITר¼ÒÍø 2008Äê6ÔÂ8ÈÕ

¹Ø¼ü×Ö£º WCF WSE ASM net Windows

  • ÆÀÂÛ
  • ·ÖÏí΢²©
  • ·ÖÏíÓʼþ
Microsoft® .NET Framework 2.0 °æͨ¹ý¼¸¸öÓÐȤµ«¿ÉÄÜÁîÈËÀ§»óµÄ·½Ê½ÖØËÜÁË Web ·þÎñµÄÇ°¾°¡£Òò´Ë£¬±¾ÔÂÎÒ´òËã½â´ðһЩÓë ASP.NET Web Services (ASMX) 2.0¡¢Web Services Enhancements (WSE) 3.0 ºÍ Windows® Communication Foundation (WCF)(WinFX® µÄÒ»²¿·Ö)ÓйصÄ×î³£¼ûÎÊÌâ¡££¨²é¿´µÚ¶þ²¿·Ö£©

¡¡¡¡ÎÊ:ÄܽâÊÍÒ»ÏÂÖ§³Ö½Ó¿ÚµÄРASMX 2.0 µÄ¹¤×÷·½Ê½Âð?ÄÄЩÊôÐÔÓÃÓÚ½Ó¿Ú£¬ÄÄЩÊôÐÔÓÃÓÚÀà?

¡¡¡¡´ð:.NET Framework 2.0 ÖÐµÄ ASMX ¿ò¼ÜÒýÈëÁËÒ»¸öÖØ´óµÄ±à³ÌÄ£Ð͸Ľø:Äܹ»ÔÚ .NET ½Ó¿Ú¶¨ÒåÖж¨Òå Web ·þÎñЭ¶¨¡£´Ë¹¦ÄÜÔÊÐíÄúʹÓó£¹æµÄ System.Web.Services ÊôÐÔ([WebServiceBinding]¡¢[WebMethod]¡¢[SOAPDocumentMethod] µÈ)À´Åú×¢ .NET ½Ó¿Ú¶¨Ò塣Ȼºó£¬Äú¾Í¿ÉÒÔÔÚ .NET ÀàÉÏʵÏÖ .NET ½Ó¿Ú£¬ÒÔÓÐЧµØʵÏÖ·þÎñЭ¶¨¡£

¡¡¡¡¸Ã·½·¨Óë WCF Öеķ½·¨(Ö÷ÒªÇø±ðÊÇÊôÐÔÃû²»Í¬)Ê®·ÖÀàËÆ¡£ËüÔÊÐíÄú´ÓʵÏÖ´úÂëÖÐÈ¥ñî·þÎñЭ¶¨£¬´Ó¶øʹ¹ÜÀíºÍÖØÓñäµÃ¸ü¼Ó·½±ã£¬Ò²Ê¹´úÂëµÄ¿É¶ÁÐÔ¸ü¸ß¡£ÈçÏÂÏÔʾ¸Ã·½·¨µÄÒ»¸öʾÀý¡£

using System.Web.Services;

[WebServiceBinding(
    Name = "QuoteService",
    Namespace = "http://example.org/stocks",
    ConformsTo = WsiProfiles.BasicProfile1_1,
    EmitConformanceClaims=true)]
public interface IQuoteService
{
    [WebMethod]
    StockQuote GetQuote(string symbol);
    [WebMethod]
    List<StockQuote> GetQuotes(List<string> symbols);
}

[WebService(Namespace="http://example.org/stocks")]
public class QuoteService : IQuoteService
{
    public StockQuote GetQuote(string symbol)
    {
        ... // retrieve and return new StockQuote
    }
    public List<StockQuote> GetQuotes(List<string> symbol)
    {
        ... // retrieve and return List<StockQuote>
    }
}

¡¡¡¡[WebServiceBinding] ºÍ [WebMethod] ÓÃÓÚ½Ó¿Ú£¬¶ø [WebService] ÓÃÓÚÀࡣʹÓôËÄ£ÐÍʱ£¬Ó°Ïì·þÎñЭ¶¨¶¨ÒåµÄËùÓÐÊôÐÔ¶¼±ØÐëÖÃÓÚ½Ó¿Ú¶¨Òå(¶ø²»ÊÇÀà)ÖС£Õâ°üÀ¨ [SOAPDocumentService]¡¢[SOAPDocumentMethod] ÒÔ¼°Äܹ»Ó¦ÓÃÓÚ·½·¨Ç©ÃûµÄ¸÷ÖÖ System.XML.Serialization ÊôÐÔ¡£Äú²»ÄÜ¿ç½Ó¿ÚºÍÀඨÒå»ìºÏºÍÆ¥ÅäÕâЩÊôÐÔ£¬ASMX ½«°ïÖúÄúÈ·±£ÕâÒ»µã¡£

ÀýÈ磬һµ©Äú´Ó½Ó¿ÚÅÉÉúÁËÒ»¸ö°üº¬ÕâЩ·þÎñЭ¶¨ÊôÐÔµÄÀàÖ®ºó£¬ASMX ½«ÔÚÔËÐÐʱ¼ì²é¸ÃÀàÒÔÈ·±£ÄúûÓÐʹÓÿÉÄÜ»áÔÚ½«À´¸ü¸Ä·þÎñЭ¶¨µÄÈκθ½¼ÓÊôÐÔ¡£Èç¹û ASMX ·¢ÏÖÁË´ËÀ฽¼ÓÊôÐÔ£¬Ëü½«ÏÔʾһ¸ö´íÎó£¬Ö¸³öÄúÎ¥·´ÁËÄ£ÐÍ¡£

¡¡¡¡ASMX ·þÎñÊôÐÔÓ÷¨´íÎó

¡¡¡¡ËäÈ»Õâ×îС»¯ÁËÐèÒªÔÚÅÉÉúÀàÉÏʹÓõÄÊôÐÔÊýÁ¿£¬µ«ÈÔÓÐһЩÐèÒªÓÃÓÚÅäÖ÷þÎñÖÕ½áµã¡£Äú½«ÔÚÀàÉÏʹÓà [WebService] À´Ö¸¶¨ÖÕ½áµãµÄ ÏêϸÐÅÏ¢¡£ÕâºÜÓÐÒâÒ壬ԭÒòÊÇÄú¿ÉÒÔÈöà¸ö·þÎñʵÏÖͬһ¸ö·þÎñЭ¶¨¡£Í¬Ñù£¬Èç¹ûÄúʹÓà WSE 3.0£¬ÔòÓ¦ÔÚÀàÉÏʹÓà [Policy] ½«°²È«²ßÂÔÅäÖÃÓ¦ÓÃÓÚÌض¨µÄÖÕ½áµã¡£

¡¡¡¡µ«ÔÚijЩÇé¿öÏ£¬Èç¹ûÐèҪͨ¹ý¸÷ÖÖ [WebMethod] ÊôÐÔ(BufferResponse¡¢CacheDuration¡¢EnableSession¡¢TransactionOption)À´ÅäÖñ¾µØ´¦ÀíÐÐΪ£¬Äú¿ÉÄÜ»¹ÐèÒªÔÚÅÉÉú·½·¨ÉÏʹÓà [WebMethod]¡£ÀýÈ磬ÈçÏÂËùʾµÄ´úÂëÖУ¬ÎÒʹÓà [WebMethod] À´ÅäÖÃÏìÓ¦»º´æ¡£

[WebService(Namespace="http://example.org/stocks")]
public class QuoteService : IQuoteService
{
    [WebMethod(CacheDuration=60)]
    public StockQuote GetQuote(string symbol)
    {
        ... // retrieve and return new StockQuote
    }
    [WebMethod(CacheDuration=60)]
    public List<StockQuote> GetQuotes(List<string> symbol)
    {
        ... // retrieve and return List<StockQuote>
    }
}

¡¡¡¡´Ë´¦Ê¹Óà [WebMethod] ²»»áÓ°Ïì·þÎñЭ¶¨¡£ËüÖ»ÅäÖñ¾µØ´¦ÀíÐÐΪ¡£ÔÚ¸ÃÉÏÏÂÎÄÖÐʹÓà [WebMethod] ʱ£¬ÄúÖ»ÄÜʹÓÃÕâЩÐÐΪÊôÐÔ£¬²¢ÇÒ±ØÐë±ÜÃâÄܹ»Ð޸ķþÎñЭ¶¨µÄÈκÎÊôÐÔ¡£

¡¡ÎÊ:ÈçºÎÔÚ×Ô¶¨ÒåÓ¦ÓóÌÐòÖгÐÔØ ASMX 2.0 Àà(²»Ê¹Óà IIS)?

¡¡¡¡´ð:ÔÚ MSDN®Magazine 2004 Äê 12 ÔµÄרÀ¸ (Service Station: Run ASMX Without IIS) ÖУ¬ÎÒÌÖÂÛÁËÈçºÎÀûÓà HTTP.sys ºÍ .NET Framework 2.0 ÖÐÐ嵀 HTTPListener ÀàÔÚ×Ô¼ºµÄÓ¦ÓóÌÐòÖÐËÞÖ÷ ASP.NET ¹ÜµÀ¡£ÕâÊÇÒ»¸öÖØÒªµÄÈÎÎñ£¬Ò²ÊÇÔÚ×Ô¼ºµÄÓ¦ÓóÌÐòÖÐÓÚ IIS ÍⲿÖØÓà ASMX ÀàµÄΨһ·½Ê½¡£µ« WSE 3.0 ÓëÖ®ÍêÈ«²»Í¬¡£

¡¡¡¡WSE 3.0 ͨ¹ýËüµÄÏûÏ¢´¦Àí²ãºÍËÞÖ÷Ä£Ðͺϲ¢ÁËеĺ͸ĽøºóµÄ ASMX 2.0 ±à³ÌÄ£ÐÍ¡£Õâ¿ÉÒÔͨ¹ýÈκÎÖ§³Ö WSE µÄ´«Êä(Èç TCP)ÔÚ×Ô¼ºµÄÓ¦ÓóÌÐòÖÐËÞÖ÷ ASMX ÖÕ½áµã¡£ÕâÒâζ×Å£¬ÏÖÔÚÎÒ¿ÉÒÔÔÚ Windows ·þÎñ¡¢Windows ´°ÌåÓ¦ÓóÌÐòÉõÖÁ¿ØÖÆ̨ӦÓóÌÐòÖгÐÔØÈçͼ 1ËùʾµÄ ASMX ´úÂë¡£´úÂëºÜ¼òµ¥ ¡ª Ö»Ðèµ÷Óà SOAPReceivers.Add ²¢Ìṩ ASMX À༴¿É£¬ÈçÏÂËùʾ:

class Program {
    static void Main(string[] args) {
        Uri uri = new Uri("SOAP.tcp://localhost:9393/quoteservice");
        SOAPReceivers.Add(uri, typeof(QuoteService));
        Console.WriteLine("Listening...");
        Console.ReadLine();
    }
}

¡¡¡¡ÕâÓë WCF Ä£Ðͷdz£ÀàËÆ£¬Äú¿ÉÒÔʹÓà ServiceHost ÔÚÈκÎËÞÖ÷»·¾³ÖгÐÔØ·þÎñÀàÐÍ¡£µ«ÄúÏȲ»Òª¼¤¶¯£¬ÎÒÓ¦¸ÃÖ¸³ö£¬Äú²»ÄÜÏñ¶Ô´ý WCF ÖÐµÄ ServiceHost ÄÇÑù½« HTTP µØÖ·Ìṩ¸ø SOAPReceivers.Add¡£SOAPReceivers ²»ÌṩÓë HTTP.sys µÄÖ±½Ó¼¯³É£¬¶øÕâÕýÊDZ¾ÀýËùÐèµÄ¡£Òò´Ë£¬ÒªÍ¨¹ý HTTP ³ÐÔØ ASMX ·þÎñ£¬Äú±ØÐëת»»Îª Ó³Éä»òÕß×Ô¼º±àд HTTP.sys ¼¯³É´úÂë¡£

¡¡ÎÊ:WCF Ϊ¿ª·¢ÈËÔ±ÌṩÁËÄÄЩ ASMX 2.0 ºÍ WSE 3.0 ËùûÓеŦÄÜ?

¡¡¡¡´ð:ASMX 2.0 ºÍ WSE 3.0 ÌṩÁËÐí¶àÖØÒª¹¦ÄÜ£¬µ«Ä³Ð©¿ª·¢ÈËÔ±´íÎóµØÈ