2016-07-08 78 views
4

我有了這個OperationContract錯誤415而測試我WCF服務

[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> 

以這種方式形成的請求對象被配置RESTful服務

[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和wor進行了測試很好。如果我嘗試通過提琴手訪問這個,雖然我得到一個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" } } } 

我也嘗試部署該服務,看看是否提琴手將能夠訪問它的話,但事情了,因爲現在不是得到一個415我更加混亂得到400抱怨'Reference to an object not set to an instance of the object.'和我創建的小命令行客戶端會抱怨我的服務的位置There was no endpoint listening at

我的問題是,我的配置文件有什麼問題,或者我用錯誤的方式編寫了提琴手請求?如果是,我該如何解決這個問題,以便我可以正常訪問我的服務?

回答

3

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

這意味着你需要與你的OperationContract預期變量的名稱來包裝的實際數據。總之這應該工作

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

如果你想保持主體內容是那麼我建議你使用Bare代替Wrapped的車身風格。

+1

@Tim感謝編輯,有時我的手會自己寫東西。 – ealiaj