ÔõÑù´´½¨.NET Web Service
January 9th, 2009 by Õ¾³¤
¡¡¡¡ÎÒ½«ÓÃc#´´½¨Ò»¸öWeb Service ½ÐSecurityWebService¡£Ò»¸öWeb ServiceÎļþµÄÀ©Õ¹ÃûÊÇ£º.asmx(¾ÍÏóasp.netµÄÎļþÀ©Õ¹Ãû.aspxÄÇÑù£©£¬ÎļþµÄµÚÒ»ÐÐÊÇ£º
¡¡¡¡£¼%@ WebService Language="C#" class="SecurityWebService" %£¾
¡¡¡¡Õâ¸öÓï¾äµÄº¬ÒåÊÇ£º¸æËß±àÒëÆ÷ÔËÐÐWeb Serviceģʽ,»¹ÓÐc#ÀàÃû¡£ÎÒÃÇ»¹ÐèÒª·ÃÎÊWeb ServiceÃû×ֿռ䣬ÕâÒ²ÊÇÒýÓÃϵͳÃû×Ö¿Õ¼äµÄÒ»´ÎºÃʵ¼ù¡£
¡¡¡¡using System;
¡¡¡¡using System.Web.Services;
¡¡¡¡
¡¡¡¡SecurityWebService Ó¦¸Ã¼Ì³ÐÁËWeb ServiceÀàµÄ¹¦ÄÜ£¬Òò´ËÎÒÃÇÓбØÒª¼ÓÈëÏÂÃæÕâÐдúÂë
¡¡¡¡
¡¡¡¡public class SecurityWebService : WebService
¡¡¡¡
¡¡¡¡ÏÖÔÚÎÒÃÇʹÓÃÃæÏò¶ÔÏóµÄ±à³Ì¼¼ÇÉ´´½¨Ò»¸öÀ࣬c#µÄÀàÓëc++ºÍjava·Ç³£ÏàËÆ£¬ÓÃC#½¨Ò»¸öÀà¼þÏóÈ¥¹«Ô°É¢²½ÄÇÑù¼òµ¥£¬¶øÇÒ²»ÐèÒªÈκμ¼ÇÉ¡£
¡¡¡¡C#µÄ»ù±¾Êý¾ÝÀàÐÍÉè¼ÆµÄ·Ç³£´ÏÃ÷£¬Òò´Ë£¬Èç¹ûÎÒÃÇ·µ»Ø"int," "float," »òÕß "string" £¬ÄÇô½«×Ô¶¯½«ËûÃÇת±ä³É±ê×¼XmlÊä³ö¡£²»ÐÒµÄÊÇ£¬ÔÚ´ó¶àÊýÀý×ÓÖÐÎÒÃÇÐèÒª½«»ñµÃµÄÊý¾Ý¼¯ºÏ¿´³ÉÒ»¸öµ¥Ò»µÄʵÌ壨single entity£©¡£ÏÖÔÚÎÒÃǾÙÒ»¸öÀý×Ó¡£
¡¡¡¡ÎÒÃÇµÄ SecurityWebService ¹ÉƱ±¨¼ÛϵͳÐèÒªÓû§ÊäÈë¹ÉƱ´úÂ룬²¢ÇÒ»¹½«·µ»ØÍêÕûµÄ¹«Ë¾ÃûºÍÏÖÐÐ¹ÉÆ±¼Û¸ñ£¬ËùÒÔ¶ÔÒ»Ö»¹ÉƱ¶øÑÔÎÒÃÇÓÐÈý¸öÐÅÏ¢¿é¡£
¡¡¡¡1¡¢¹«Ë¾´úÂ루string£©
¡¡¡¡2¡¢¹«Ë¾Ãû£¨string£©
¡¡¡¡3¡¢¼Û¸ñ£¨double£©
¡¡¡¡µ±ÎÒÃÇÌá½»¹ÉƱʱ£¬ÎÒÃÇÐèÒªÌáÈ¡ËùÓÐÈýÖÖÊý¾Ý£¬Óм¸ÖÖ·½·¨À´Íê³ÉÕâÏ×÷£¬×îºÃµÄ·½·¨Êǽ«ËûÃǰ󶨵½Ò»Öֿɱ»Ã¶¾ÙµÄÊý¾ÝÀàÐÍÄÚ£¬ÎÒÃÇÔÚc#ÖпÉÓÃ"struct"À´Íê³É£¬c#ÖеÄ"struct"ºÍc++ÖеĽṹºÜÏàËÆ¡£
¡¡¡¡
¡¡¡¡public struct SecurityInfo
¡¡¡¡{
¡¡¡¡public string Code;
¡¡¡¡public string CompanyName;
¡¡¡¡public double Price;
¡¡¡¡}
¡¡¡¡
¡¡¡¡ÎÒÃÇ¿ÉÒÔͨ¹ýÄ£¿é´´½¨Web Service,´úÂëÈçÏ£º
¡¡¡¡
¡¡¡¡£¼%@ WebService Language="C#" class="SecurityWebService" %£¾
¡¡¡¡using System;
¡¡¡¡using System.Web.Services;
¡¡¡¡
¡¡¡¡public struct SecurityInfo
¡¡¡¡{
¡¡¡¡public string Code;
¡¡¡¡public string CompanyName;
¡¡¡¡public double Price;
¡¡¡¡}
¡¡¡¡
¡¡¡¡public class SecurityWebService : WebService
¡¡¡¡{
¡¡¡¡private SecurityInfo Security;
¡¡¡¡
¡¡¡¡public SecurityWebService()
¡¡¡¡{
¡¡¡¡Security.Code = "";
¡¡¡¡Security.CompanyName = "";
¡¡¡¡Security.Price = 0;
¡¡¡¡}
¡¡¡¡
¡¡¡¡private void AssignValues(string Code)
¡¡¡¡{
¡¡¡¡// This is where you use your business components.
¡¡¡¡// Method calls on Business components are used to populate the data.
¡¡¡¡// For demonstration purposes, I will add a string to the Code and
¡¡¡¡// use a random number generator to create the price feed.
¡¡¡¡
¡¡¡¡Security.Code = Code;
¡¡¡¡Security.CompanyName = Code + " Pty Ltd";
¡¡¡¡Random RandomNumber = new System.Random();
¡¡¡¡Security.Price = double.Parse(new System.Random(RandomNumber.Next(1,10)).NextDouble().Format("##.##",null));
¡¡¡¡}
¡¡¡¡
¡¡¡¡[WebMethod(Description="This method call will get the company name and the price for a given security code.",EnableSession=false)]
¡¡¡¡public SecurityInfo GetSecurityInfo(string Code)
¡¡¡¡{
¡¡¡¡AssignValues(Code);
¡¡¡¡SecurityInfo SecurityDetails = new SecurityInfo();
¡¡¡¡SecurityDetails.Code = Security.Code;
¡¡¡¡SecurityDetails.CompanyName = Security.CompanyName;
¡¡¡¡SecurityDetails.Price = Security.Price;
¡¡¡¡return SecurityDetails;
¡¡¡¡}
¡¡¡¡
¡¡¡¡}
¡¡¡¡¼ÇסËùÓÐÓû§¶¼ÄÜͨ¹ýhttp·ÃÎÊWeb Service£¬Ò²ÐíÄã»á̸µ½´úÂëÖеĻúÃÜÉÌÒµÊý¾ÝºÍ²»Ï£ÍûÆäËûÈËÖªµÀµÄÊý¾Ý£¬ÔõÑù±£ÊØÊý¾Ý»úÃÜ¡£½â¾ö·½·¨ÊDZ£»¤ÉÌÒµÂß¼¹¦Äܿ飬ֻÔÊÐí·ÃÎʱíʾ²ã£¬ÔÚc#ÖпÉÒÔͨ¹ýʹÓùؼü×Ö"[Web Method]"À´´ïµ½Õâ¸öÄ¿µÄ£¬ÎÒÃÇ¿´¿´ÏÂÃæµÄ´úÂ룺
¡¡¡¡[WebMethod(Description="This......",EnableSession=false)]
¡¡¡¡public SecurityInfo GetSecurityInfo(string Code)
¡¡¡¡Õâ¸öº¯ÊýÏÔʾ¸ø¹«ÖÚ£¬description±ê¼ÇÓÃÓÚÃèÊöWeb ServiceµÄ¹¦ÄÜ£¬ÓÉÓÚÎÒÃDz»ÄÜ´æ´¢ÈκλỰÊý¾Ý£¬ÎÒÃǾͽ«Ïû³ý»á»°×´Ì¬¡£
¡¡¡¡private void AssignValues(string Code)
¡¡¡¡Õâ¸öÉÌÒµÂß¼º¯Êý²»±»¹«ÖÚËùÖª£¬ÎÒÃDz»Ï£ÍûÃô¸ÐµÄÉÌÒµÐÅÏ¢±»¹«²¼ÔÚwebÉÏ£¨×¢Ò⣺ÉõÖÁ½«private¸ÄΪpublic£¬¹«ÖÚÈÔÈ»¿´²»¼û£¬ÎªÊ²Ã´ÄØ£¿£¬ÕâÊÇÓÉÓÚûÓÐʹÓÃ[Web Method]¹Ø¼ü×Ö¡££©
¡¡¡¡ÎÒÃÇ¿ÉÒÔÔÚÕâ¸öº¯ÊýÖÐÀûÓÃÉÌÒµÂß¼»ñµÃ×îÐÂµÄ¹ÉÆ±±¨¼Û£¬ÎªÁËÕâ¸öÄ¿µÄ£¬ÎÒÔÚ´úÂëÖÐÌí¼ÓÁËÎı¾¿òÒÔ±ãÊäÈ빫˾Ãû³Æ£¬¼Û¸ñÓÉÒ»¸öËæ»úº¯Êý²úÉú¡£
¡¡¡¡ÎÒÃǰÑÕâ¸öÎļþÒÔSampleService.asmx±£´æÔÚIISĿ¼Ï¡£ÎÒ½«Ëû±£´æÔÚÐéÄâĿ¼"/work/aspx"Ï¡£
¡¡¡¡Õâ¸öWEBÒ³ÊÇÓÉ.NET frameworkÉú³ÉµÄ£¬ÎÒÃÇûÓд´½¨Õâ¸öÒ³£¨ÕâÊÇÓÉϵͳ×Ô¶¯Éú³ÉµÄ£¬ÎÒÃÇûÓÐΪËûдÈκÎÒ»ÐдúÂ룬Õ⸽ͼÊÇÏÈǰ´úÂëµÄ¸±²úÆ·£©£¬×¼±¸Ê¹ÓõŦÄܶԵ¥Ò»µÄWeb ServiceÊÇÏ൱ºÏÊʵġ£
¡¡¡¡Ê¹ÓÃasp.netºÍconfig.webÎļþ¿ÉÒÔºÜÇáËɵĸıä¸ÃÒ³¡£²»¹ýҪעÒâÄǸöSDL¹æ·¶µÄÁ´½Ó£¨¼´Ê¹ÎÒÃÇÎÒÃÇʹÓÃWSDL,.NET °æÈÔÈ»ÒýÓÃÁËSDL£¬Õâ¸öÎÊÌâÔÚÏÂÒ»¸ö°æ±¾ÖÐÓÐÏ£Íû½ÃÕý£©£¬ÕâÊÇWeb ServiceµÄÒ»¸öÃèÊöÎļþÄ¿µÄÊÇ´´½¨Ò»¸ö´úÀí¶ÔÏó£¬Õâ»ù±¾Éϸø³öWeb ServiceµÄÒ»¸ö´óÖ½éÉÜ£¬Èç¹ûÄã¶ÔÕâЩ¶¼±È½ÏÊìϤ£¬Äã¿ÉÒÔÖ»¿´"Web-only"·½·¨£¬SDL¹æ·¶¶ÔËùÓÐ˽Óк¯ÊýºÍÊôÐÔ¶¼Î´ÃèÊö£¬SecurityWebService ÀàµÄSDL¹æ·¶ÔÚÀý³ÌAÖп´µ½¡£
#If you have any other info about this subject , Please add it free.# |
- Posted in jsgp.com edit