我知道這已被打死,但我無法像它應該那樣工作。 我有幾個合同的WCF服務。 當他們直接打電話時,他們都可以正常工作。 http://merlin.com/CompanyServices/CompanyWcfService.svc/Get_Document_Dates_Received/223278 我已經在InfoPath Forms和Nintex Workflows上成功使用了此WCF服務。 現在我創建一個簡單的ASP.Net應用程序,例如在http://www.webcodeexpert.com/2013/04/how-to-create-and-consume-wcf-services.html中完成。 我能夠添加文章中所述的服務參考。 我添加了一個按鈕的形式,並在Button1_Click
事件添加以下代碼:無法找到引用合同的默認端點元素
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.CompanyWcfServiceClient x = new ServiceReference1.CompanyWcfServiceClient();
var result = x.Get_Document_Dates_Received("223278");
}
當我按一下按鈕我得到的錯誤:
"Could not find default endpoint element that references contract 'ServiceReference1.ICompanyWcfService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."
所以我嘗試添加以下內容在web.config:(直接從CompanyWcfService的web.config文件複製
<system.serviceModel>
<services>
<service name="CompanyWcfServices.CompanyWcfService" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="CompanyWcfServices.ICompanyWcfService" behaviorConfiguration="webHttpEndpointBehavior" >
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
</service>
</services>
<bindings>
<webHttpBinding>
<binding>
<security mode="None">
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name ="webHttpEndpointBehavior">
<webHttp helpEnabled ="true" faultExceptionEnabled="true" automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
我得到同樣的確切的錯誤,必須有別的東西布萊恩克上。
我終於放棄了,並呼籲像這樣的服務:
HttpWebRequest request = WebRequest.Create(@"http://merlin/Companyservices/CompanyWcfService.svc/Get_Document_Dates_Received/223278") as HttpWebRequest;
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = null;
var result = "";
try
{
response = request.GetResponse() as HttpWebResponse;
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream stream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
result = reader.ReadToEnd();
}
}
}
catch (Exception ex)
{
result = "";
}
我花了幾個小時閱讀職位,其中大部分建議的配置信息複製到web.config文件。這對我來說似乎有問題(除了它似乎沒有工作的事實)。如果我需要使用第三方WCF服務怎麼辦?我需要從第三方請求配置信息嗎?而Visa Versa,如果我創建了一個旨在供第三方使用的WCF服務,我是否也需要爲它們提供配置文件?
另外。在應用程序web.config中的system.ServiceModel節中,您不應該需要services部分。我會刪除它。您只需要在客戶端部分中引用定義的項目。與自定義的行爲配置或綁定設置類似。 –
馬特,謝謝你的迴應。對不起,但我仍然得到同樣的錯誤。 – user1337493
然後這個問題似乎是應用程序中的服務和web.config之間的不匹配。該錯誤表示無法找到具有合同「ServiceReference1.ICompanyWcfService」的服務。請注意,在web應用程序的web.config中,合同是「CompanyWcfServices.ICompanyWcfService」。你有沒有偶然創建一個服務,在你的web應用中添加一個引用,然後更改服務名稱空間? –