WebInvoke发现WCF REST服务EndPoint未找到错误

时间:2012-02-24 00:00:33

标签: wcf

我有一个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>

这是我尝试过的网址。

http://localhost:65317/Service1.svc

3 个答案:

答案 0 :(得分: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>

我从小提琴手的原始请求如下:

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 :(得分: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/

答案 2 :(得分:0)

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

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

只要您对web.config的定义明确,那将起作用