2014-10-31 123 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