WCF REST服务终结点未找到错误与WebInvoke

3

我有一个WCF REST服务。我已经验证了Webconfig文件,并且所有的配置项都是正确的。

我使用以下代码,但是出现了“EndPoint未找到”的错误。我将向GetDateTime方法添加参数。

[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)] 
    string GetDateTime();    

使用以下代码出现了“无终端点”错误。

[OperationContract]
[WebGet(UriTemplate = "/")]
string GetDateTime();    

我希望能够通过WebInvoke使其运行起来。非常感谢您的帮助!

以下是Web.config文件的配置细节。

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <services>
      <service name="SampleRest.Service1" behaviorConfiguration ="ServiceBehaviour">
        <endpoint address="" binding ="webHttpBinding" contract="SampleRest.IService1" behaviorConfiguration ="web" >
        </endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior  name="ServiceBehaviour">
          <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

这是我尝试的URL。

http://localhost:65317/Service1.svc


(注:该内容已经是中文,无需翻译)

你能否发布你的配置和你尝试访问WebGet方法的URL? - Amir
3个回答

2
我有以下代码:
[WebInvoke(ResponseFormat=WebMessageFormat.Json, RequestFormat= WebMessageFormat.Json)]
string PostDateTimeAndReturnAsString(DateTime dateTime);

上述方法的实现如下所示:

public string PostDateTimeAndReturnAsString(DateTime dateTime)
{
       return dateTime.ToString();
}

我的web.config如下所示:

<services>
    <service name="XMLService.Service1" behaviorConfiguration="default">
            <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="RestBinding" name="Service1" contract="XMLService.IService1" />
    </service>
</services>
<behaviors>
      <endpointBehaviors>
    <behavior name="web">
         <webHttp />
    </behavior>
</endpointBehaviors>
</behaviors>

我的Fiddler原始请求如下:

POST http://localhost/XMLService/Service1.svc/postdatetimeandreturnasstring HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost
Content-Length: 30

"\/Date(1330081170513+0000)\/"

我收到了HTTP 200 OK代码,并返回以下响应:

"24\/02\/2012 11:07:59"

注意:当日期时间是需要以JSON格式传递的参数时,您需要按照原始请求格式进行传递(我在执行过程中使用的值是当前日期时间,使用相同的值应返回如下所示的值)。


1

从配置文件中删除整个system.serviceModel,如下所示:

<?xml version="1.0"?>
<configuration>
<system.web>
  <compilation debug="true" targetFramework="4.0" />
</system.web>

<system.webServer>
  <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>

我假设您的SVC文件已经长成这样:

<%@ ServiceHost 
                Language="C#" Service="SampleRest.Service1" 
                Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

svc 会自动在 system.serviceModel 中为您进行配置。

最后尝试 http://localhost:65317/Service1.svc/


0

你应该调用操作本身。在你的例子中,你的客户端必须指向:

http://localhost:65317/Service1.svc/GetDateTime

只要你的 web.config 定义良好,那就可以工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接