WebService

作者:互联网   出处:控件中国网   2014-11-05 19:21:29   阅读:1

WebService

1、.NET Framework4并没有直接提供ASP.NET Web Service Application模版。
这说明了webService已经找到了更好的替代品。
2、实例中的Service1继承System.Web.Services.WebService,说明了WebService所用的方法来自这个类。
3、HellowWorld,只是服务器上的一个方法,这个方法有个标签[WebMethod]。返回了一个字符串。
  这个方法是为了说明客户端能够调用到该服务器上的方法。
4、运行后能够看到一个页面,这个页面做了一个测试,用来测试Helloworld是否被调用了。
点击调用,可以看到页面是打印有"Hello World",说明该方法的确被调用了。
5、观察浏览器的地址栏,上面的地址是
http://localhost:2645/Service1.asmx/HelloWorld
说明webService的请求地址是http://localhost:2645/Service1.asmx/HelloWorld
但是直接请求不能够正常运行,还得提交参数。
6、可以通过post的方式提交数据给上面的地址。可以新建页面,也可以用WebClient
然后post数据给上面的地址。postData数据为byte[],不允许为空。
7、关于可以使用的协议
在<system.web>配置
    <webServices>
        <protocols>
          <add name="HttpSoap"/>
          <add name="HttpPost"/>
          <add name="HttpGet"/>
          <add name="Documentation"/>
        </protocols>
      </webServices>
8.通过框架来调用该webServcie

可以在项目中直接对webService的引用。

然后调用

ServiceReference1.Service1SoapClient soapClient = new ServiceReference1.Service1SoapClient();
        string s =soapClient.HelloWorld();
生成的文件都保存在

Service References\ServiceReference1文件中。

这是文件的Reference.cs的最后代码

code//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.1
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Runtime.Serialization;
using System.ServiceModel;


namespace ConsoleApplication1.ServiceReference1
{

    [ServiceContractAttribute(ConfigurationName = "ServiceReference1.Service1Soap")]
    public interface Service1Soap
    {
        // CODEGEN: Generating message contract since element name HelloWorldResult from namespace http://tempuri.org/ is not marked nillable
        [OperationContractAttribute(Action = "http://tempuri.org/HelloWorld1", ReplyAction = "*")]
        HelloWorldResponse HelloWorld1();
    }
    [MessageContractAttribute(IsWrapped = false)]
    public  class HelloWorldResponse
    {
        [MessageBodyMemberAttribute(Name = "HelloWorldResponse", Namespace = "http://tempuri.org/", Order = 0)]
        public HelloWorldResponseBody Body;
    }


    [DataContractAttribute(Namespace = "http://tempuri.org/")]
    public  class HelloWorldResponseBody
    {
        [DataMemberAttribute(EmitDefaultValue = false, Order = 0)]
        public string HelloWorldResult;
    }


    public  class Service1SoapClient : ClientBase<Service1Soap>
    {
        public string HelloWorld1()
        {
            HelloWorldResponse retVal = base.Channel.HelloWorld1();
            string sRet = retVal.Body.HelloWorldResult;
            return sRet;
        }
    }
}


 

Copyright© 2006-2015 ComponentCN.com all rights reserved.重庆磐岩科技有限公司(控件中国网) 版权所有 渝ICP备12000264号 法律顾问:元炳律师事务所
客服软件
live chat