加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

.NET 下动态设置 WebService 引用

发布时间:2020-12-17 02:09:52 所属栏目:安全 来源:网络整理
导读:??? 做需要部署 WebService 的项目时,要根据部署环境环境设置 WebService 引用。VS(2008)下可以引用 WebService 并且引用地址也设置在app.config(winform)中,但引用后生成了一堆配置文件,并且配置文件中含有引用地址,给人硬编码的错觉。如果手动引用,稍
??? 做需要部署 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读