微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

.NET 下动态设置 WebService 引用

    做需要部署 WebService 的项目时,要根据部署环境环境设置 WebService 引用。VS(2008)下可以引用 WebService 并且引用地址也设置在app.config(winform)中,但引用后生成了一堆配置文件,并且配置文件中含有引用地址,给人硬编码的错觉。如果手动引用,稍微配置可达到同样的效果。即使用 WSDL.exe 工具生成WebService的代理,本地调用即可。
有如下 WebService :
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

[WebService(Namespace
= " http://tempuri.org/ " )]
[WebServiceBinding(ConformsTo
= WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    
public Service () {

        
// Uncomment the following line if using designed components
        
// InitializeComponent();
    }

    [WebMethod]
    
public int Sum( int a, int b)
    {
        
int c = a + b;

        
return c;
    }
    
}

 WSDL.exe 命令行为:wsdl /n:namespace1 /out:c:/service1.cs http://localhost/WebServiceHTTP/Service.asmx?WSDL
生成的代理文件内容为:
// ------------------------------------------------------------------------------
// <auto-generated>
//      This code was generated by a tool.
//      Runtime Version:2.0.50727.1433
//
//      Changes to this file may cause incorrect behavior and will be lost if
//      the code is regenerated.
// </auto-generated>
// ------------------------------------------------------------------------------

//
// This source code was auto-generated by wsdl,Version=2.0.50727.1432.
//
namespace namespace1 {
    
using System.Diagnostics;
    
using System.Web.Services;
    
using System.ComponentModel;
    
using System.Web.Services.Protocols;
    
using System;
    
using System.Xml.Serialization;
    
    
    
/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute( " wsdl " , " 2.0.50727.1432 " )]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute(
" code " )]
    [System.Web.Services.WebServiceBindingAttribute(Name
= " ServiceSoap " ,Namespace = " http://tempuri.org/ " )]
    
public partial class Service : System.Web.Services.Protocols.soapHttpClientProtocol {
        
        
private System.Threading.SendOrPostCallback SumOperationCompleted;
        
        
/// <remarks/>
         public Service() {
            
this .Url = " http://localhost/WebServiceHTTP/Service.asmx " ;
        }
        
        
/// <remarks/>
         public event SumCompletedEventHandler SumCompleted;
        
        
/// <remarks/>
        [System.Web.Services.Protocols.soapDocumentMethodAttribute( " http://tempuri.org/Sum " ,RequestNamespace = " http://tempuri.org/ " ,ResponseNamespace = " http://tempuri.org/ " ,Use = System.Web.Services.Description.soapBindingUse.Literal,ParameterStyle = System.Web.Services.Protocols.soapParameterStyle.Wrapped)]
        
public int Sum( int a, int b) {
            
object [] results = this .Invoke( " Sum " , new object [] {
                        a,
                        b});
            
return (( int )(results[ 0 ]));
        } 
         //还有很多...

然后把构造方法中的 this .Url = " http://localhost/WebServiceHTTP/Service.asmx " ; 在app.config中配置即可。

this.Url = ConfigurationManager.AppSettings.Get("service1"); 

app.config 中的内容为:

<? xml version="1.0" encoding="utf-8" ?>
< configuration >
  
< appSettings >
    
< add key ="service1" value ="http://localhost/WebServiceHTTP/Service.asmx" />    </ appSettings >
</ configuration >

这样做有个缺点,就是每次修改 WebService后都要重新生成一遍,设置URL。可以调试的时候直接用VS引用,完成后手工引用即可。也可写一个批处理生成代理。也可以再写一个代理,派生自WSDL.exe生成的代理,把URL以参数的形式传到WSDL.exe生成的代理。
WSDL.exe的使用方法可参考:http://msdn2.microsoft.com/en-us/library/7h3ystb6(VS.80).aspx

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐