【发布时间】: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