为什么当我为Wcf服务键入“POST”方法它给“端点未找到”错误?

时间:2013-06-27 22:09:46

标签: c# .net wcf web-config wcf-endpoint

如果我使用Method = "POST",wcf服务会给出“Endpoint not found”错误(... / SpellCheckerWcf.svc)。但是,GET方法有效。我在stackoverflow中搜索了“Endpoint not found”主题,但没有一个没有帮助。如果您知道解决方案,请帮助我。

接口:

[ServiceContract]
public interface ISpellCheckerWcf
{
    [OperationContract]
    [WebInvoke(UriTemplate = "DoWork?params[document]={document}", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
    Stream DoWork(string document);
}

类别:

public class SpellCheckerWcf :ISpellCheckerWcf
{
   public Stream DoWork(string document)
   {
       JsonFormat json = new JsonFormat();
       json.document = document;
   return WriteJson(json);
   }
   private Stream WriteJson(object value)
   {
       var javaScriptSerializer = new JavaScriptSerializer();
       var json =
           Encoding.UTF8.GetBytes(javaScriptSerializer.Serialize(value));
       var memoryStream = new MemoryStream(json);
       WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
       return memoryStream;
   }

Web配置:

enter image description here

服务加价:

<%@ ServiceHost Language="C#" Debug="true"
Service="SpellCheckerWeb.SpellCheckerWcf" 
Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

2 个答案:

答案 0 :(得分:0)

我认为你的UriTemplate不再需要查询字符串信息了:

UriTemplate = "DoWork?params[document]={document}"

尝试使用:

UriTemplate = "DoWork"

答案 1 :(得分:0)

我认为你需要'WebGet'装饰

<WebGet(UriTemplate:="DoWork?params[document]={document}", BodyStyle:=WebMessageBodyStyle.Wrapped,
                RequestFormat:=WebMessageFormat.Json, ResponseFormat:=WebMessageFormat.Xml)>