Tôi biết điều này đã bị đánh đến chết, nhưng tôi không thể làm điều này để làm việc như nó cần. Tôi có một dịch vụ WCF với nhiều hợp đồng. Tất cả đều hoạt động tốt khi gọi điện trực tiếp, ví dụ: http://merlin.com/CompanyServices/CompanyWcfService.svc/Get_Document_Dates_Received/223278 Tôi đã sử dụng dịch vụ WCF này thành công trên Biểu mẫu InfoPath và Quy trình làm việc của Nintex. Bây giờ tôi tạo một ứng dụng ASP.Net đơn giản, chẳng hạn như được thực hiện trong http://www.webcodeexpert.com/2013/04/how-to-create-and-consume-wcf-services.html. Tôi có thể thêm tham chiếu dịch vụ như được mô tả trong bài viết. Tôi đã thêm một nút hình thức, và thêm đoạn mã sau trong Button1_Click
sự kiện:Không thể tìm thấy phần tử điểm cuối mặc định tham chiếu đến hợp đồng
protected void Button1_Click(object sender, EventArgs e)
{
ServiceReference1.CompanyWcfServiceClient x = new ServiceReference1.CompanyWcfServiceClient();
var result = x.Get_Document_Dates_Received("223278");
}
khi tôi nhấp vào nút Tôi nhận được lỗi:
"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."
Vì vậy, tôi cố gắng thêm dòng sau vào web.config: (được sao chép trực tiếp từ tệp web.config của CompanyWcfService.
<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>
Tôi gặp lỗi chính xác tương tự, phải có điều gì đó khác goin g trên.
Cuối cùng tôi đã từ bỏ và được gọi là dịch vụ như thế này:
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 = "";
}
Tôi đã dành giờ đọc bài viết và hầu hết trong số họ đề nghị để sao chép các thông tin cấu hình vào file web.config. Điều này dường như có vấn đề với tôi (bên cạnh thực tế là nó dường như không hoạt động). Nếu tôi cần sử dụng dịch vụ WCF của bên thứ ba thì sao? Tôi có phải yêu cầu thông tin cấu hình từ bên thứ ba không? Và Visa Versa, nếu tôi tạo ra một dịch vụ WCF được thiết kế để được tiêu thụ bởi các bên thứ ba, tôi có cần cung cấp cho họ tệp cấu hình không?
Ngoài ra. Trong phần system.ServiceModel trong web.config ứng dụng của bạn, bạn không cần phần dịch vụ. Tôi sẽ xóa nó. Bạn chỉ cần xác định các mục được tham chiếu trong phần máy khách. Giống như cài đặt behaviorConfiguration hoặc binding nếu bạn đã tùy chỉnh. –
Matt, cảm ơn phản hồi. Rất tiếc, tôi vẫn gặp lỗi tương tự. – user1337493
Vấn đề này sau đó có vẻ là sự không khớp giữa dịch vụ và web.config trong ứng dụng. Lỗi này chỉ ra rằng không thể tìm thấy dịch vụ có hợp đồng 'ServiceReference1.ICompanyWcfService'. Thông báo trong web.config của bạn trong ứng dụng web, hợp đồng là "CompanyWcfServices.ICompanyWcfService". Bạn có vô tình tạo một dịch vụ, thêm một tham chiếu trong ứng dụng web của bạn vào nó và sau đó thay đổi không gian tên dịch vụ? –