我创建了以下svc服务 -
public class NewsRest : INewsRest
{
public string getHomePageNews1()
{
DataTable dt = new DataTable();
try
{
dt = new ManageNews().getHomePageNews1();
string json = new Utills().dataTableToJsonString(dt);
json = json.Replace("task-photo-846b6edc-3b2e-4ff6-ba10-734d64143c1d.png", "task-photo-8d5feed4-d6a1-4ae9-9a72-387ccfda1dc5.jpg");
return json;
}
catch (Exception ex)
{
return "{}";
}
}
}
[ServiceContract]
public interface INewsRest
{
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
string getHomePageNews1();
}
和web配置配置是 -
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewsRestBehavior">
<serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="UPNews.WebServices.NewsRest" behaviorConfiguration="NewsRestBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="UPNews.WebServices.INewsRest" />
<endpoint address="mex" binding="mexHttpBinding" name="MetadataEndpoint"
contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
但是当我在WCF测试客户端上测试此服务时,它会出现以下错误.-
错误:无法从http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1获取元数据 如果这是您可以访问的Windows(R)Communication Foundation服务, 请检查您是否已在指定地址启用元数据发布。 有关启用元数据发布的帮助 请参阅http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange错误中的MSDN文档 URI:http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1元数据包含无法解析的引用: &#39; http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1&#39 ;.内容类型application / soap + xml; 服务http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1不支持charset = utf-8。 客户端和服务绑定可能不匹配。远程服务器返回错误: (415)不支持的媒体类型.HTTP GET错误URI:http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1下载时出错 &#39; http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1&#39 ;.请求失败,HTTP状态为400:错误请求。
请告诉我们这里发生了什么错误。
答案 0 :(得分:0)
您必须添加到服务器配置以允许MEX(元数据交换),以便客户端可以检索生成的WSDL。像这样:
<service name="UPNews.WebServices.NewsRest" behaviorConfiguration="NewsRestBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="UPNews.WebServices.INewsRest" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>