WebService之间相互通信的问题

最近对已完结项目进行部署,遇到了一些以前没有想到过的问题,主要有两个,一个是WebService服务间相互通信的问题,另一个是WebService中global.asax的一些知识。下面进行总结:
首先是一个WebService如何调用另外一个WebService,在VS2015中,只要在项目上右键,在菜单列表中添加服务引用就好。url写调试的时候地址栏中的地址就可以。但在部署时需要动态的改变地址怎么办呢?这就需要能从配置文件中读入发布好的地址了。
使用Webservice的第一步应该是创建对象吧 是不是这样,看下面代码:

public DB_Service.DBSoapClient sw = new DB_Service.DBSoapClient();
如果你细心一点的话,右建这个构造方法查看一下转到定义试试。代码如下:
public DBSoapClient() {}
public DBSoapClient(string endpointConfigurationName): 
        base(endpointConfigurationName) {}
public DBSoapClient(string endpointConfigurationName,   string remoteAddress) : 
        base(endpointConfigurationName, remoteAddress) {
        }
public DBSoapClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : 
        base(endpointConfigurationName, remoteAddress) {
        }
public DBSoapClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : 
                base(binding, remoteAddress) {}

可以看到,C#已经写好了动态调用的方法,原来我们提前设好的端口在Web.Config中已经写好:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="SCYZT_DBSoap" closeTimeout="00:30:00" openTimeout="00:30:00" receiveTimeout="00:30:00" sendTimeout="00:30:00" maxBufferSize="2147483647"
                    maxReceivedMessageSize="2147483647"/>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:62119/SCYZT.DB.asmx" binding="basicHttpBinding"
        bindingConfiguration="SCYZT_DBSoap" contract="DB_Service.SCYZT_DBSoap"
        name="SCYZT_DBSoap" />
    </client>
  </system.serviceModel>

我们在定义时用如下方法即可:

public DB_Service.DBSoapClient sw = new DB_Service.DBSoapClient("Service1Soap","你要修改的URL例如:http://www.cckan.net/webservices.asmx");

其中:”Service1Soap”就是Web.config中节点的Name属性值。

第二就是WebService中global.asax的一些知识,global.asax是一个文本文件,它提供全局可用代码。这些代码包括应用程序的事件处理程序以及会话事件、方法和静态变量。有时该文件也被称为应用程序文件。
global.asax 文件中的任何代码都是它所在的应用程序的一部分。每个应用程序在其根目录下只能有一个global.asax文件。然而,这个文件是可选的。如果没有global.asax文件,应用程序将对所有事件应用由 HttpApplication类提供的默认行为。
提示:经典ASP有一个与global.asax类似格式和构造的,名为global.asa的文件。实际上,如果将一个正在运行的global.asa文件代码复制到global.asax中,应用程序同样可以运行。
当应用程序运行的时候,global.asax的内容被编译到一个继承自HttpApplication类的类中。因此,HttpApplication类中所有的方法、类和对象对于应用程序都是可用的。
项目只运用了global.asax的Application_Start方法,即程序启动时调用的函数,Application_Start 方法 是在第一个用户访问时才被调用的,并且只会被调用一次。读取配置文件的方法可以写于此。
如果项目中没有global.asax文件,右键添加即可。

猜你喜欢

转载自blog.csdn.net/wangbingqian_110/article/details/53264437
今日推荐