C# WCF 测试客户端无法添加服务,无法获取元数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9114870/
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 06:02:26  来源:igfitidea点击:

WCF Test Client cannot add service, cannot obtain metadata

c#wcfmetadata

提问by hyprsleepy

Can anyone tell me why I get this error when I try to add my service?

谁能告诉我为什么在尝试添加服务时会出现此错误?

Error: Cannot obtain Metadata from http://myserver/myapp. If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455. WS-Metadata Exchange Error URI: http://myserver/myappMetadata contains a reference that cannot be resolved: 'http://myserver/myapp'. The remote server returned an unexpected response: (405) Method Not Allowed. The remote server returned an error: (405) Method Not Allowed.HTTP GET Error URI: http://myserver/myappThere was an error downloading 'http://myserver/myapp'. The request failed with HTTP status 403: Forbidden.

错误:无法从http://myserver/myapp获取元数据。如果这是您有权访问的 Windows (R) Communication Foundation 服务,请检查您是否已在指定地址启用元数据发布。有关启用元数据发布的帮助,请参阅http://go.microsoft.com/fwlink/?LinkId=65455上的 MSDN 文档。WS-Metadata Exchange 错误 URI:http://myserver/myapp元数据包含无法解析的引用:“ http://myserver/myapp”。远程服务器返回了意外响应:(405) Method Not Allowed。远程服务器返回错误:(405) Method Not Allowed.HTTP GET Error URI: http://myserver/myapp下载时出错http://myserver/myapp'。请求失败,HTTP 状态为 403:禁止。

Update: I have the following endpoint already,

更新:我已经有以下端点,

<endpoint address="mex" 
          binding="mexHttpBinding"
          name="Metadata"
          contract="IMetadataExchange" />

I also have the service behaviors set:

我还设置了服务行为:

  <serviceBehaviors>
    <behavior name="myBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
  </serviceBehaviors>

采纳答案by hyprsleepy

I figured it out. I was using an absolute path on one of my endpoints and I had more than one. I changed it to a relative path and then everything worked.

我想到了。我在我的一个端点上使用了绝对路径,而且我有多个。我将其更改为相对路径,然后一切正常。

Also, this can happen if your service isn't running and may have nothing to do with metadata. If you have a ConfigurationErrorsExceptionthen you will see this result as well.

此外,如果您的服务未运行并且可能与元数据无关,则可能会发生这种情况。如果你有,ConfigurationErrorsException那么你也会看到这个结果。

I also got it when trying to save configuration changes to the project and then it told me to save/overwrite/ignore, etc. It ended up adding a project subtype of "designer" and once I removed that it went back to working properly.

我在尝试将配置更改保存到项目时也得到了它,然后它告诉我保存/覆盖/忽略等。它最终添加了一个“设计师”的项目子类型,一旦我删除它,它就会恢复正常工作。

回答by Eric Schneider

If adding the metadata endpoints is not your issue. Check further in the error message details. I had a missing library; "Could not load file or assembly *".

如果添加元数据端点不是您的问题。在错误消息详细信息中进一步检查。我有一个丢失的图书馆;“无法加载文件或程序集 *”。

I needed to change the reference to Copy Local = True

我需要将引用更改为 Copy Local = True

回答by CW30Meters

I'm new to WCF and just got a project handed to me with WCF services inside. I was having similar issues with using WCF Test Client in VS2010 on Win7. After trying all the config changes in this article and others I was still getting similar errors.

我是 WCF 的新手,刚刚收到一个包含 WCF 服务的项目。我在 Win7 上的 VS2010 中使用 WCF 测试客户端时遇到了类似的问题。在尝试了本文和其他文章中的所有配置更改后,我仍然遇到类似的错误。

So I gave up and started reading up on WCF. Here I found that VS should be launched as administrator. Here's the article: Getting Started Tutorial WCFTurns out I have to launch VS2010 with 'Run as Administrator' checked. This fixed the issue and I was able to test WCF services in VS2010. I'm posting this answer incase others over look the Run as Administrator.

所以我放弃了并开始阅读 WCF。在这里我发现应该以管理员身份启动VS。这是文章: WCF 入门教程原来我必须在选中“以管理员身份运行”的情况下启动 VS2010。这解决了这个问题,我能够在 VS2010 中测试 WCF 服务。我发布了这个答案,以防其他人以管理员身份运行。

回答by Kevbo

This can also happen when you try to return a custom class object from one of the functions and that function doesn't have the [DataContract]attribute on the class declaration. WCF cant figure out what type your returning so it throws the standard message.

当您尝试从其中一个函数返回自定义类对象并且该函数[DataContract]在类声明中没有该属性时,也会发生这种情况。WCF 无法确定您返回的类型,因此它会抛出标准消息。

[Serializable]
[DataContract]
public class ServiceResult
{
  ....
}

回答by Dani Mathew

Some times there isn't anything to be done with metadata or the settings; instead we may simply use 127.0.0.1 instead of http:\localhost\

有时不需要对元数据或设置做任何事情;相反,我们可以简单地使用 127.0.0.1 而不是 http:\localhost\

回答by GJKH

I had this issue, turns out that when I renamed the .svc file a reference to it hadn't been renamed from Service1.svc. Run a project wide search and replace .Service1with your new name.

我遇到了这个问题,结果是当我重命名 .svc 文件时,对它的引用没有从 Service1.svc 重命名。运行项目范围的搜索并替换.Service1为您的新名称。

<%@ ServiceHost Language="C#" Debug="true" Service="MyNamespace.Service1" CodeBehind="MyRenamedService.svc.cs" %>

Service="MyNamespace.Service1"should read Service="MyNamespace.MyRenamedService"

Service="MyNamespace.Service1"应该读 Service="MyNamespace.MyRenamedService"

回答by Andrew

I discovered that I had the same error because of a DataContract that I was using that inherited from Dictionary. The proper setup of the DataContract should be as follows:

我发现我有同样的错误,因为我使用的是从字典继承的 DataContract。DataContract 的正确设置应如下所示:

[CollectionDataContract(Name = "AdditionalProperties",
                            ItemName = "Property",
                            KeyName = "Key",
                            ValueName = "Value")]
public class AdditionalProperties : Dictionary<string, string>
{
}

回答by user2211290

This is my experience on this error message,might be useful to others. My WCF service is working fine on windows 2008 server.I copied the same solution to Dev machine, selected .svc file from solution so that WCF test client can be popped-up.

这是我对此错误消息的经验,可能对其他人有用。我的 WCF 服务在 Windows 2008 服务器上运行良好。我将相同的解决方案复制到开发机器,从解决方案中选择 .svc 文件,以便可以弹出 WCF 测试客户端。

WCF test client opened with errors cannot obtain metadata,assembly can't be loaded etc.

错误打开的 WCF 测试客户端无法获取元数据,无法加载程序集等。

Key is, first kick up the service (http://localhost:port/Service) then only try WCF test client.

关键是,首先启动服务(http://localhost:port/Service)然后才尝试 WCF 测试客户端。

回答by user007

In my case, I had not enabled BindingParams.AllowHttpGet. When that was set to true, it worked...

就我而言,我没有启用 BindingParams.AllowHttpGet。当它设置为 true 时,它​​起作用了......

回答by Ali Rasouli

please check and read all text of error.

请检查并阅读所有错误文本。

in my case another dll has error but show same error in above text of error.

在我的情况下,另一个 dll 有错误,但在上面的错误文本中显示相同的错误。