C# 尝试添加服务引用时出错

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10085048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 12:23:41  来源:igfitidea点击:

Error when trying to add a Service Reference

c#web-serviceswebclientservice-reference

提问by VilemRousi

I′m trying to create a client in C# to a web service which (I suppose) is written in Java. It′s my first time trying to write a client, so I′m following the instructions on MSDN, but I′m stuck on a problem with Add Reference. When I open the Add Service Referencedialog and add the URL, an error occurs:

我正在尝试用 C# 为 Web 服务创建一个客户端,该服务(我想)是用 Java 编写的。这是我第一次尝试编写客户端,所以我正在按照 MSDN 上的说明进行操作,但是我遇到了添加引用的问题。当我打开“添加服务引用”对话框并添加 URL 时,出现错误:

There was an error downloading 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
The request failed with HTTP status 404: Not Found.
Metadata contains a reference that cannot be resolved: 'http://geoportal.cuzk.cz/WCTService/WCTService.svc'.
There was no endpoint listening at http://geoportal.cuzk.cz/WCTService/WCTService.svc that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.

What should my next step be? I don′t know what I should do with this! (It is a coordinates-transformation service from the Czech Republic.)

我的下一步应该是什么?我不知道我该怎么办!(这是来自捷克共和国的坐标转换服务。)

For more information:
Property services (GetCapabilities) http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?

欲了解更多信息:
物业服务 (GetCapabilities) http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?

Localization services: http://geoportal.cuzk.cz/WCTService/WCTService.svc/get?request=GetCapabilities&service=WCTS

本地化服务:http: //geoportal.cuzk.cz/WCTService/WCTService.svc/get?request=GetCapabilities&service= WCTS

采纳答案by DotNetFreak

I was facing a similar situation in which I had created a WCF Service (Employee.svc) and later changed the named to EmployeeService.svc. WCF project compiled just fine but when I was trying to add service reference from by UI Client, I was getting following error:

我遇到了类似的情况,我创建了一个 WCF 服务 (Employee.svc),后来将名称更改为 EmployeeService.svc。WCF 项目编译得很好,但是当我尝试从 UI 客户端添加服务引用时,出现以下错误:

Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc?wsdl'.
The document format is not recognized (the content type is 'text/html; charset=UTF-8').
Metadata contains a reference that cannot be resolved: 'http://localhost:2278/EmployeeService.svc'.
There was no endpoint listening at 'http://localhost:2278/EmployeeService.svc' that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.

I resolved it by replacing the correct service class name everywhere. In my case, it should have been EmployeeService and NOT employee. The left out place was in the markup code of svc file:

我通过在任何地方替换正确的服务类名称来解决它。就我而言,它应该是 EmployeeService 而不是员工。遗漏的地方是在 svc 文件的标记代码中:

<%@ ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**Employee**" CodeBehind="EmployeeService.svc.cs" %>

Changed it to

改成

<%@ ServiceHost Language="C#" Debug="true" Service="WCFServiceHost.**EmployeeService**" CodeBehind="EmployeeService.svc.cs" %>

And it started working again!!! Dont forget to build your WCF project after changing the service name.

它又开始工作了!!!不要忘记在更改服务名称后构建您的 WCF 项目。

回答by ldgorman

Is the service definitely up and running before you try to add a service reference? If it exposes meta data, does it have a service behaviour or equivalent configured? Have you configured your firewall correctly?

在您尝试添加服务引用之前,该服务是否确实已启动并正在运行?如果它公开元数据,它是否具有服务行为或配置的等效项?您是否正确配置了防火墙?

回答by Fabske

The webservices doesn't run.

网络服务不运行。

If you don't have access to the server where this service run, you're blocked.

如果您无权访问运行此服务的服务器,则会被阻止。

Otherwise, you need to check if the server run, etc. As I don't know how the Java webservice is run, I can't help you further.

否则,您需要检查服务器是否运行等。由于我不知道Java webservice 是如何运行的,我无法进一步帮助您。

回答by Nick Ryan

I tried browsing to http://geoportal.cuzk.cz/WCTService/WCTService.svc?wsdl. It looks like this service is not exposing metadata.

我尝试浏览到http://geoportal.cuzk.cz/WCTService/WCTService.svc?wsdl. 看起来这个服务没有公开元数据。

I did a bit of googling on OpenGIS, and I think you need to have a look at this article:

我在 OpenGIS 上做了一些谷歌搜索,我认为你需要看看这篇文章:

OpenGIS with .NET

带有 .NET 的 OpenGIS

You won't be able to just add a service reference and go. It looks like you need to craft a concrete WSDL.

你不能只添加一个服务引用就可以了。看起来您需要制作一个具体的 WSDL。

There may be a client-side library you can use / customize to assist with integration. Have a look at Stack Overflow question Using MySQL GeoSpatial data types in .NET.

可能有一个客户端库,您可以使用/自定义来协助集成。看看 Stack Overflow 问题Using MySQL GeoSpatial data types in .NET

回答by Sheo Dayal Singh

While adding service reference to the client application, metadata is not accessible from service to client application. hence The remote server returned an error: (404) Not Found. Actually we can host the WCF service as follows:

在向客户端应用程序添加服务引用时,无法从服务到客户端应用程序访问元数据。因此远程服务器返回错误:(404) Not Found。实际上,我们可以按如下方式托管 WCF 服务:

  1. Self hosting(console application)
  2. IIS Hosting
  3. WAS Hosting
  4. window service hosting.
  1. 自托管(控制台应用程序)
  2. IIS 托管
  3. 托管
  4. 窗口服务托管。

if you are using self hosting then you need to host the service in console application and run the service(run the console application) and then add the service reference to the client application, then metadata would be exchange. If service is not running then while adding service reference to the client application then 404 not found error would be getting. Same process would follow for all the hosting type. first run the service then add service reference.

如果您使用自托管,那么您需要在控制台应用程序中托管服务并运行该服务(运行控制台应用程序),然后将服务引用添加到客户端应用程序,然后元数据将被交换。如果服务未运行,则在向客户端应用程序添加服务引用时,将出现 404 not found 错误。所有托管类型都将遵循相同的过程。首先运行服务,然后添加服务引用。

回答by Scope Creep

I know this is an old thread and has already been resolved, but I just finished troubleshooting this exact issue, and none of the resolutions presented here worked for me. Wanted to share my resolution in case anyone else runs into this thread with a similar issue.

我知道这是一个旧线程并且已经得到解决,但我刚刚完成了对这个确切问题的故障排除,这里提供的解决方案都没有对我有用。想分享我的解决方案,以防其他人遇到类似问题的问题。

My ENTIRE issue stemmed from a bad Refactor->Rename operation. I recently purchased Resharper for my dev team and Resharper did not like the name of our service implementation name. We had named it "WCFAccess" and Resharper wanted the name "WcfAccess". I had just published an update, had the release safely isolated in its own release branch in git, and figured this was a good time to perform a rename on the develop branch and shut Resharper up about the naming. I used Refactor->Rename to change the name of the file to match the naming convention we had defined in the Resharper configuration. The rename operation completed, the solution compiled and ran, time goes on and the WCF rename was forgotten.

我的整个问题源于错误的重构->重命名操作。我最近为我的开发团队购买了 Resharper,但 Resharper 不喜欢我们的服务实现名称。我们将其命名为“WCFAccess”,而 Resharper 想要命名为“WcfAccess”。我刚刚发布了一个更新,将该版本安全地隔离在它自己的 git 发布分支中,并认为这是在开发分支上执行重命名并关闭 Resharper 关于命名的好时机。我使用 Refactor->Rename 来更改文件的名称以匹配我们在 Resharper 配置中定义的命名约定。重命名操作完成,解决方案编译并运行,随着时间的推移,WCF 重命名被遗忘了。

Fast forward a couple weeks, and its time to deploy out web services to the test environment for regression testing. The solution compiled successfully, published successfully, then gave me the EXACT error that the OP posted. What I ended up finding out is that the Rename operation from weeks ago ONLY UPDATED SOURCE CODE REFERENCES to the old name and did not rename MARKUP. When I navigated out to our web server where the service was published to and double clicked on the .svc file, it opened the markup in Visual Studio and I noticed that the character casing of the CodeBehind="ServiceNameHere.svc.vb" was inconstant with the new naming convention. Updating the markup and web.config files to reference the correct character casing resolved my issue.

快进几周,是时候将 Web 服务部署到测试环境进行回归测试了。解决方案编译成功,发布成功,然后给了我 OP 发布的 EXACT 错误。我最终发现的是,几周前的重命名操作仅将源代码引用更新为旧名称,并没有重命名标记。当我导航到发布服务的 Web 服务器并双击 .svc 文件时,它在 Visual Studio 中打开了标记,我注意到 CodeBehind="ServiceNameHere.svc.vb" 的字符大小写是不一致的使用新的命名约定。更新标记和 web.config 文件以引用正确的字符大小写解决了我的问题。

I hope this helps someone. It was incredibly frustrating to troubleshoot

我希望这可以帮助别人。进行故障排除非常令人沮丧

(Please don't hate me for using VB.Net, I inherited this application) :-)

(请不要因为我使用 VB.Net 而恨我,我继承了这个应用程序):-)

回答by Déric Dallaire

I had the same problem happen to me earlier today. The webservice was running fine on local host but for some reason, I was having a 400 when trying to add the service reference in another project.

今天早些时候我遇到了同样的问题。webservice 在本地主机上运行良好,但由于某种原因,当我尝试在另一个项目中添加服务引用时出现 400。

My error was caused by setting the [DataMember]annotation instead of the [EnumMember]annotation on an enum of the service. Changing it solved my issue.

我的错误是由在服务枚举上设置[DataMember]注释而不是[EnumMember]注释引起的。改变它解决了我的问题。