gpt4 book ai didi

WCF REST 服务端点未找到 WebInvoke 错误

转载 作者:行者123 更新时间:2023-12-04 14:12:54 26 4
gpt4 key购买 nike

我有一个 WCF REST 服务。我验证了 Webconfig 文件,所有配置项都很好。

我在使用以下代码时遇到 EndPoint not found 错误。我将向 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

最佳答案

我有以下代码:

[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 格式传递的参数时,这是您需要传递它的方式,如原始请求格式所示(我使用的值是在我执行期间的当前日期时间,使用相同的值应该返回如下所示的值)

关于WCF REST 服务端点未找到 WebInvoke 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9423420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com