[英]Error 415 while testing my wcf service
I have a Restful service that has this OperationContract
我有一个具有此
OperationContract
的Restful服务
[OperationContract]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "json")]
Response JSONData(Request request);
and is configured in this way 并以这种方式配置
<?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>
The request object is formed in this way 以这种方式形成请求对象
[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; }
}
I created a small client to test this query and I've also tested it using WCFTestClient
and works fine. 我创建了一个小型客户端来测试此查询,并且还使用
WCFTestClient
对其进行了测试,并且工作正常。 If I try to access this through fiddler though I get a 415 Unsupported Media Type
error. 如果我尝试通过提琴手访问此文件,但出现
415 Unsupported Media Type
错误。
I tried this in the following manner 我以以下方式尝试了
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" } } }
I also tried deploying this service and see if fiddler would be able to access it then, but things got even more confusing because now instead of getting a 415
I was getting a 400
complaining about 'Reference to an object not set to an instance of the object.'
我还尝试部署了该服务,然后看提琴手是否能够访问它,但是事情变得更加令人困惑,因为现在不是得到
415
而是得到了400
抱怨: 'Reference to an object not set to an instance of the object.'
and the small command line client I created would complain about There was no endpoint listening at
the location of my service. 并且我创建的小型命令行客户端会抱怨
There was no endpoint listening at
我的服务位置进行There was no endpoint listening at
。
My question is, is there something wrong with my configuration file, or did I compose the fiddler request in the wrong way? 我的问题是,我的配置文件有问题吗,还是我以错误的方式编写了提琴手请求? If yes how can I fix this so I can access my service normally?
如果是,如何解决此问题,以便可以正常访问我的服务?
I can see that you are using BodyStyle = WebMessageBodyStyle.Wrapped
我可以看到您正在使用
BodyStyle = WebMessageBodyStyle.Wrapped
This means that you need to wrap the actual data with the name of the variable expected by your OperationContract
. 这意味着您需要使用
OperationContract
期望的变量名称包装实际数据。 In short this should work 简而言之,这应该工作
{ "response": { "input": { "login_password": "pass", "login_username": "login name", "query": { "birthDate": "", "name": "robert" } } } }
If you want to keep the body content as is then I suggest you use Bare
instead of Wrapped
for the body style. 如果您希望保持主体内容不变,那么建议您使用
Bare
而不是Wrapped
作为主体样式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.