在C#中访问Web服务的标准

时间:2012-06-05 15:11:26

标签: c# wcf web-services

我想知道在C#中访问Web服务的标准方法,其中可以通过编程方式定义Web服务引用。

我有以下情况:

  1. 可以通过编程方式设置多个Web服务。 因此,我无法使用Visual Studio提供的“添加Web服务参考” (或者如果我错了,我认为纠正我。)
  2. 添加的webservices具有相同的结构/操作/操作/请求/响应,但可能属于提供不同数据的不同域。
  3. 例如:

      webservice 01 :  http://abc.example.com/getData
      webservice 02 :  http://xyz.example.net/getData
    
    1. 我是否仍然可以使用从一个服务生成的代理并将其用于另一个服务,还是我必须处理原始XML响应?
    2. 编辑01: 我想知道访问web服务的以下片段是否可以推广用于所有web服务

              var binding = new BasicHttpBinding();
              var address = new EndpointAddress("http://www.abc.com/service.asmx");
              var factory = new ChannelFactory<IGeneralProxy>(binding, address);
      
              var obj = factory.CreateChannel();
              var responseString = obj.GetData("UserName", "Password");
              Assert.IsNotNull(responseString);
      

      其中IGeneralProxy是客户端的接口

      如果上述任何一点不清楚,请告诉我。

3 个答案:

答案 0 :(得分:1)

是的,只要服务相同,您就可以为两种服务使用相同的生成代理。我一直这样做。

以下是我的代码片段。我使用WSE 3.0作为我正在研究的项目是.net 2.0。

ServiceWse serivce = new ServiceWse();
CookieContainer cookieContainer = new CookieContainer();
serivce.Timeout = 1000 * 60 * CommonFunctions.GetConfigValue<int>(Consts.Common.WebServiceTimeout, 20);
serivce.Url = CommonFunctions.GetConfigValue(Consts.Urls.MyServiceSecuredURL, string.Empty);
serivce.CookieContainer = cookieContainer;
if (CommonFunctions.GetConfigValue(Consts.Security.UseSecuredServices, false))
    CommonFunctions.SetWSSecurity(_service.RequestSoapContext);

答案 1 :(得分:0)

查看以前对类似问题的回答:

How to programmatically connect a client to a WCF service?

您可以为每个Web服务执行此操作。

答案 2 :(得分:0)

我们做类似的事情。 Web服务引用是针对我们服务的Dev版本创建的。我们还在web.config中为每个Web服务URI设置了一个设置,我们在实例化服务时使用它;这样,当我们部署到生产环境时,我们所要做的就是更改web.config中的URI,而不是重建项目。

var myService= new myService(){Uri = [service uri from web.config]};
相关问题