zoukankan      html  css  js  c++  java
  • [转]手把手教你学Web Service

    手把手教你学Web Service
    2001年9月12日

    作者:nfs (小肥家族·极品小肥车)

    发信站: 华南网木棉站

      Well,各位观众,现在开始我的手把手交Visual Studio.net的第一课―-手把手教你学Web Service.有没有下一课还得看诸位看官的反应了!我们知道,在MS新一代战略.net中,Web Service占了一个相当次大的份量,为什么这样说那?那是因为Web Service 是未来编程的新思路,他将编程由本机扩大到了Internet上,他通过一个proxy.dll就可以访问在Internet上提供的Service,并且就像在本机上操作一样方便,其实,Web Service也可以看成是Web上的Class,这样,大家code时候就不用局限于本机,本局域网…的限制了。

      既然Web Service这么好,怎样写阿?在下就将在Visual Studio.net 中究竟如何编写Web Service的全过程一一奉上。

      所有的编程教程不免都已Hello World作为开始,我也难免如此。

    //Web Service建立的方法不用说了吧?
    //project->new->asp.net web service->输入名称…..

      1.Hello World~~

    [WebMethod]
    public string HelloWorld()
    {
      return "Hello World"
    }

      其实,这个函数在一生成Web Service的时候已经生成,只不过注释起来了。先不要执行这个Web Service,先研究一下,这个WS没有输入参数,输出参数string类型,那么执行期间会有什么情况了?按!执行,弹处一个IE,上面有一打废话(对于初学者,呵呵)。最有用的是开头两句:

    The following operations are supported. For a formal definition, please review the Service Description.
    Hello World

      好了,我们的方法出现在上面了。点击HelloWorld链接,就进入到下一个页面,有一个invoke按钮,还有这样的字:

    Test
    To test, click the ’Invoke’ button.

      按了之后,就出现:

    <?xml version="1.0" encoding="utf-8" ?>
      <string xmlns="http://tempuri.org/">Hello World</string>

      OK!我们的第一个Web Service 成功了!

      2.让我们试一下改改一些东西,输入参数好像太简单了,加点东西吧:

      public string HelloWorld(string strInput)

      返回值变为return str+"Hello World"

      其余不变,有什么事情发生那?执行之,第一个界面不变,但是有invoke按钮的那个页面就不同了,多了一个表格,parameter那一列多了个strInput:,value那列多了一个text box。喔,原来是在text box里面输入strInput的值。随便打什么,比方说"Nfs is the best",按invoke按钮,返回如下信息:

    <?xml version="1.0" encoding="utf-8" ?>
      <string xmlns="http://tempuri.org/">Nfs is the best Hello World</string>

      会用了吗?

      3.在变化一些,输入参数变为数组

    public string HelloWorld(string[] str)
    {
      return str[0]+"Hello World";
    }

      执行之,第一个页面好像没什么问题,但是一点hello world这个链接,就好像出错了!

    Test Test forms are only available for methods with non-array primitive types as parameters.
    SOAP
    The following is a sample SOAP request and response. The placeholders shown need to be replaced with actual values.
    .............

      怎么回事?Web Service不能使用数组,只能是简单的变量?也太差劲了吧?非也!这只是说明该Web Service输入参数的数目不能在ie中定下来,当然无法显示了,现在,新建一个Application,例如说是Windows Application, 然后再form1上加一个button,双击,出现事件:

    private void button1_Click(object se nder, System.EventArgs e)

      在Solution Explore中添加Web Reference, IP为localhost。UDDI(MS的讨厌的命名,呵呵)会帮我们找到刚才我们写的Web Service。在button 1_Click中,添加如下语句:

    localhost1.Service1 ls=new localhost1.Service1();
    string[] strIn=new string [2];
    strIn[0]="Nfs is the best ";
    string strRet= ls.HelloWorld(strIn);

      单步跟踪,你会发现strRet="Nfs is the best Hello World"

      成功!

      4.好了,简单变量的输入输出都试完了,(注意,没有试输出为数组的情形,为什么?诸位看官自有公论,C#能返回指针么?呵呵,在输入中采用out 或者ref 方式坑可能会好些把?),还有什么没有试?自定义对象!试试看?

    public class Foo
    {
      public Foo(int x)
      {
        str=new string[x];
      }
      public string[] str;
    }
    public string HelloWorld(Foo FooTest)
    {
      for(int i=0;i<FooTest.str.Length;i++)
        FooTest.str[i]+=" Hello World"+i.ToString();
        //do something
      return "Hello World";
    }

      编译,完了,第一个页面就说:

    Server Error in ’/WebService4’ Application.
    WebService4.Service1+Foo cannot be serialized because it does not have a default public constructor.
    Description: An unhandled exception occurred during the execution of the current web request.Please review the stack trace for more information about the error and where it originated in the code.
    Exception Details:System.Exception: WebService4.Service1+a cannot be serialized because it does not have a default public constructor.
    Source Error:一大堆Error

      怎么回事!看看error吧!WebService4.Service1+Foo出问题了,什么问题?

    it does not have a default public constructor.

      Faint,是都有default的吗?没有,系统会生成一个的波!不管了,加上一个。

    public Foo()
    {
      //do nothing
    }

      再编译,成功了!至少Hello World的第一个页面出来了点击链接,不错,如你所想,还是出现上述输入数组的情形,道理都明白了吗?

    Test
    No test form is available as this service or method does not support the HTTP GET protocol.
    SOAP
    The following is a sample SOAP request and response.
    The placeholders shown need to be replaced with actual values.

      没事的,象3中情况一样,调用是没有问题的,放心!

      5.好像都将完了吧?没有!诸位有没有考虑到在人家的程序中调用我的时候,怎样定的输入参数的类型?该类型使我自己定义的啊!如上变量Foo。好,我们到刚才生成的Windows Application里面看看那。在Solution Explore中的localhost,右键选择Update Web Referece。(我们改过,他可不知道,呵呵)把原来的语句改成:

    localhost.Service1 ls=new localhost.Service1();
    localhost.Foo FooTest=new localhost.Foo();

      按点的时候,自动Foo会弹出来了,一切正常,没什么特别啊?好,我们回去改改Web Service。将class Foo改为:

    public class Foo
    {
      public Foo ()
      {

      }
      public Foo (int x)
      {
        str=new string[x];
      }
      public class HiddenOrNot
      {
        public HiddenOrNot()
        {
          //do nothing
        }
      }
      public string[] str;
    }

      Web Method不变。回去Windows Application里面,再update一次,试试输入ls.,看看弹出来什么?没有我们新加上去的HiddenOrNot类吧?奇怪,我明明声明他是public的波!怎么不行?我们再回去Web Service,将Web Method改为:

    public string HelloWorld(Foo FooTest, Foo.HiddenOrNot fhTest)
    {
      //do something
    }

      回去Windows Application 里面,再update一次,试试输入ls.,再看看弹出来什么? 有了!有ls.Foo 和ls.HiddenOrNot了。看来,Web Reference中,你的Web Service对象是否可见,就是根据输入参数确定的,他才不管你是public那(对了,记得constractor要为public波,还要有default constrator)。

      6.好了,剩余的问题,就是上面几种情况的综合了,根据排列组合定理...嗯,不说了。各位看官自行实践ba!

      Thx all. -- 肥车有主,谢绝上客.

  • 相关阅读:
    nj07---npm
    nj06---包
    nj05---模块
    nj04---事件回调函数
    nj03---阻塞和线程
    nodejs02---demo
    nodejs简介
    【转贴】内存系列一:快速读懂内存条标签
    【转贴】4个你未必知道的内存小知识
    Linux上面mount 域控的目录 超时 然后提示 error的解决办法
  • 原文地址:https://www.cnblogs.com/volnet/p/615878.html
Copyright © 2011-2022 走看看