【问题标题】:Can't Add Service Reference to self-hosted WCF Service无法将服务引用添加到自托管 WCF 服务
【发布时间】:2011-05-17 21:35:13
【问题描述】:

我创建了一个 REST Web 服务,我需要从一个 asp.net 应用程序中使用它。该服务从控制台窗口托管。我可以让它运行良好,当在网络浏览器中浏览它时,我也可以从中获得输出。问题是,当我尝试从我的 asp.net 应用程序中“添加服务引用”时,它会根据我指向的服务 URL 抱怨各种事情。最终结果是我无法弄清楚如何添加服务参考。

我的界面是这样定义的:

[ServiceContract]
public interface IWave {
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/devices")]
    List<Device> getDevices();

    ...
}

这是我托管服务的方式:

// Port is 1178
var endPoint = new EndpointAddress(string.Format("http://localhost:{0}/wave", port));
var waveServiceSingleton = new WaveSVC();

binding = new WebHttpBinding();

var behavior = new WebHttpBehavior();
behavior.FaultExceptionEnabled = true;

host = new WebServiceHost(waveServiceSingleton, endPoint.Uri);

// Get the service debug behavior and tell it to include details about errors
ServiceDebugBehavior sdb;
sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;

host.AddServiceEndpoint(typeof(IWave), binding, "");

// Add mex endpoint
ServiceMetadataBehavior mexBehavior = new ServiceMetadataBehavior();
mexBehavior.HttpGetEnabled = true;
host.Description.Behaviors.Add(mexBehavior);
host.AddServiceEndpoint(typeof(IWave), MetadataExchangeBindings.CreateMexHttpBinding(), endPoint.Uri.AbsoluteUri + "/mex");

host.Open();

当我浏览到 http://localhost:1180/wave/devices 时,我在浏览器的正文中看到一个 json 字符串。这是预期的,并且可以按需要工作。我无法将“添加服务引用”向导指向此 URL,因为它抱怨:

The document at the url http://localhost:1178/wave/devices was not recognized as a known     document type.
The error message from each known type may help you fix the problem:
- Report from 'XML Schema' is 'Data at the root level is invalid. Line 1, position 1.'.
- Report from 'DISCO Document' is 'Data at the root level is invalid. Line 1, position  1.'.
- Report from 'WSDL Document' is 'There is an error in XML document (1, 1).'.
  - Data at the root level is invalid. Line 1, position 1.
Metadata contains a reference that cannot be resolved: 'http://localhost:1178/wave/devices'.
The remote server returned an unexpected response: (405) Method Not Allowed.
The remote server returned an error: (405) Method Not Allowed.
If the service is defined in the current solution, try building the solution and adding the service reference again.

我怀疑我需要将“添加服务引用”指向 mex,但这也不起作用,事实上,当我浏览到 http://localhost:1178/wave/mex 的 mex 地址时,我得到一个空白页.

编辑 1

为了消除 JSON 是罪魁祸首,我将合同更改为输出 Xml 而不是 Json。结果是一样的:我无法使用此 URL 添加服务引用:http://localhost:1178/zwave/devices/xml(即使该 URL 生成 XML)。

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate= "/devices/xml")]

提前感谢您提供的任何帮助。

【问题讨论】:

  • BTW - 您是否尝试过使用 svcutil 而不是 VS 从 mex 数据创建代理?从 VS 命令提示符:svcutil.exe /o:client.cs /config:app.config localhost:1178/wave/mex

标签: c# c#-4.0 wcf wcf-client


【解决方案1】:

传统上,您在 Visual Studio 中使用的“添加 Web 引用”选项仅用于引用 XML Web 服务。您的服务是根据rest/json 定义的,而不是Visual Studio 期望的soap/xml/wsdl。

我怀疑 Visual Studio 只是 无法为您的 json/rest 服务生成代理。这在WCF rest starter kit for .NET 3.5 的文档中得到了证实(请参阅“使用 HttpClient 使用 RESTful 服务”部分,搜索“无法像使用 SOAP 那样生成强类型代理”)。

我不确定它在 VS2010 / .NET 4.0 中是否仍然不受支持,但我认为您必须寻找“添加网络引用”以外的其他选项。

【讨论】:

  • 我将输出更改为 XML,但仍然无法添加引用。这是大致相同的错误。有关我所做的更改,请参阅上面的编辑。
  • 现在返回xml格式的数据,但是Visual Studio还是找不到自动生成代理所需的wsdl文档。对于 REST 服务,这样的文档不存在,所以我认为这不会起作用。我认为错误消息试图告诉您 VS 无法从它收到的数据中推断出 wsdl。
【解决方案2】:

或许为空白页找到了:

替换

host = new WebServiceHost(waveServiceSingleton, endPoint.Uri);

host = new ServiceHost(waveServiceSingleton, endPoint.Uri);

这对我有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多