gpt4 book ai didi

c# - 测试我的 wcf 服务时出现错误 415

转载 作者:太空狗 更新时间:2023-10-30 01:32:08 26 4
gpt4 key购买 nike

我有一个具有此 OperationContract

的 Restful 服务
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json")]
Response JSONData(Request request);

并且是这样配置的

<?xml version="1.0"?>
<configuration>

<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService" closeTimeout="01:01:00"
openTimeout="01:01:00" receiveTimeout="01:10:00" sendTimeout="01:01:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default"/>
</security>
</binding>
</basicHttpBinding>

<webHttpBinding>
<binding name="WebHttpBinding_IService" closeTimeout="01:01:00" openTimeout="01:01:00"
receiveTimeout="01:10:00" sendTimeout="01:01:00" allowCookies="false"
bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"
transferMode="Streamed" useDefaultWebProxy="true">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="None" />
</binding>
<binding name="webHttpBindingXML"/>
<binding name="webHttpBindingJSON"/>
</webHttpBinding>
</bindings>

<services>
<service name="SearchService.SearchService" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" bindingConfiguration="WebHttpBinding_IService" contract="SearchService.ISearchService" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</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="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>

<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint
automaticFormatSelectionEnabled="true"
helpEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>

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

</configuration>

请求对象是这样构成的

[DataContract]
public class Query
{
[DataMember]
public string name { get; set; }

[DataMember]
public string birthDate { get; set; }
}

[DataContract]
public class Input
{
[DataMember]
public Query query { get; set; }

[DataMember]
public string login_username { get; set; }

[DataMember]
public string login_password { get; set; }
}

[DataContract]
public class Request
{
[DataMember]
public Input input { get; set; }
}

我创建了一个小客户端来测试这个查询,我还使用 WCFTestClient 测试了它并且工作正常。如果我尝试通过 fiddler 访问它,尽管我收到 415 Unsupported Media Type 错误。

我试过下面的方法

POST, http://localhost:8080/HostDevServer/SearchService.svc/json, HTTP/1.1

User-Agent: Fiddler
Content-Type: application/json;charset=UTF-8
Host: localhost:8080
Content-Length: 368

{ "input": { "login_password": "pass", "login_username": "login name", "query": { "birthDate": "", "name": "robert" } } }

我还尝试部署此服务,看看 fiddler 是否能够访问它,但事情变得更加困惑,因为现在我得到的不是 415,而是 400 提示 'Reference to an object not set to an instance of the object.' 而我创建的小型命令行客户端会提示 There was no endpoint listening at 我的服务地点。

我的问题是,是我的配置文件有问题,还是我以错误的方式编写了 fiddler 请求?如果是,我该如何解决才能正常访问我的服务?

最佳答案

我可以看到您正在使用 BodyStyle = WebMessageBodyStyle.Wrapped

这意味着您需要使用 OperationContract 期望的变量名称来包装实际数据。简而言之,这应该有效

{ "response": { "input": { "login_password": "pass", "login_username": "login name", "query": { "birthDate": "", "name": "robert" } } } }

如果您想保持正文内容不变,那么我建议您使用 Bare 而不是 Wrapped 作为正文样式。

关于c# - 测试我的 wcf 服务时出现错误 415,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38263857/

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