.NET 下动态设置 WebService 引用

本文介绍如何在VS中高效引用WebService,包括使用WSDL.exe工具生成代理的方法及URL配置,提供了一种灵活的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    做需要部署 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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值