2014-10-31 122 views
0

我正在使用.NET Web引用来使用使用基本身份验证的Web服务。在过去,我可以通过访问相同名称的属性来设置证书。例如:在Visual Studio中使用Web引用功能时缺少属性

MyWebReference.Service1 client = new MyWebReference.Service1(); 
client.Credentials = new System.Net.NetworkCredential("username", "password"); 
client.MyOperation(); 

这在旧项目中工作得很好。现在我正试图在一个新的VS项目中设置Credentials块,我找不到该属性了。我检查了我的老项目,并有像AllowAutoRedirectClientCertificatesConnectionGroupNameContainerCookieContainer ...

在我的新的VS项目我刚刚UrlUseDefaultCredentials几个属性。我还将旧的(工作)VS项目中的Web服务包含到我的新项目中,但也没有任何属性。所以对VS项目来说这肯定是奇怪的事情,或者缺少引用。

我检查了引用,重新创建了多次Web引用,但没有得到工作。

我正在使用Visual Studio 2010 Professional。

有什么想法?

感谢您的帮助

回答

0

I resolved the problem by myself. Unfortunately I had added a WCF Service named System.svc到项目地图,导致System命名空间和命名System

阶级之间的冲突,我改名为System.svc类并且凭证属性可以访问。

0

确保您的服务类继承:System.Net.HttpWebRequest This has properties that you are looking for.

using System; 
using System.Net; 
using System.Security.Cryptography.X509Certificates; 
using System.Web.Services; 
using System.Web.UI; 

namespace WebApplication4 
{ 
    public partial class WebForm1 : Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      MyService math = new MyService() 
      { 
       Credentials = new NetworkCredential("Joe", "mydomain", "password"), 
       AllowAutoRedirect = false, 
       ClientCertificates = new X509CertificateCollection(), 
       ConnectionGroupName = "" 
      }; 
     } 
    } 

    [WebService(Namespace = "http://tempuri.org/")] 
    public class MyService : HttpWebRequest 
    { 
     [WebMethod] 
     public int SumOfNums(int First, int Second) 
     { 
      return First + Second; 
     } 
    } 
} 

PLEASE MARK THIS AS ANSWER IF THIS SOLVES YOUR PROBLEM.

Reference: MSDN

+0

我试图消费的服务不是从我开发的。但它必须是客户端的东西,因为相同的服务在另一个项目中具有'Credentials'属性。 – user3231903 2014-11-03 08:04:22

+0

[MSDN](http://msdn.microsoft.com/de-de/library/system.web.services.protocols.soaphttpclientprotocol(v = vs.110).aspx)我的客户端继承自'SoapHttpClientProtocol',它继承'WebClientProtocol'的'Credentials'属性。所以我不明白为什么这个属性不可访问。 – user3231903 2014-11-03 08:18:00