.NET 下动态设置 WebService 引用
来源:互联网 发布:lte ca矩阵 编辑:程序博客网 时间:2024/06/05 11:43
做需要部署 WebService 的项目时,要根据部署环境环境设置 WebService 引用。VS(2008)下可以引用 WebService 并且引用地址也设置在app.config(winform)中,但引用后生成了一堆配置文件,并且配置文件中含有引用地址,给人硬编码的错觉。如果手动引用,稍微配置可达到同样的效果。即使用 WSDL.exe 工具生成WebService的代理,本地调用即可。
有如下 WebService :
然后把构造方法中的 this.Url = "http://localhost/WebServiceHTTP/Service.asmx"; 在app.config中配置即可。
this.Url = ConfigurationManager.AppSettings.Get("service1");
有如下 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
生成的代理文件内容为:
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]));
}
//还有很多...
// <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
<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
- .NET 下动态设置 WebService 引用
- asp.net动态设置WebService引用
- asp.net动态设置WebService引用
- asp.net动态引用WebService
- asp.net动态引用WebService
- asp.net如何调用webservice,如何动态设置webservice引用地址
- 动态修改 webservice 引用
- 可动态引用webservice
- WebService引用动态配置
- SQLSERVER环境下调用包含webservice引用的.net类库
- 动态引用WebService,建立WebService虚拟机
- 动态引用WebService,建立WebService虚拟机
- .net 动态调用webService
- .Net 动态调用WebService
- .Net动态调用WebService
- .Net 动态调用WebService
- .Net下采用GET/POST/SOAP方式动态调用WebService
- asp.net动态调用webservice
- 利用 Signal Adapter 为你的现有软件增加 VoIP 通信界面
- 神经网络开发包joone的功能演示
- sqlServer分页公式
- JAVA上传文件 DiskFileUpload组件
- 《大话设计模式》学习总结,初步~
- .NET 下动态设置 WebService 引用
- CSS的十八般技巧
- 软件开发流程(转载)
- S60: cleanup stack
- [连载 5/15] Android 从入门到精通Example 之 SQLite Database Example
- Hibernate总结
- 感谢
- C/C++读写文件(fwrite,fread)
- Win Mobile利用mapirule接收短信