2017-08-24 151 views
0

我想將一些JSON數據發佈到WCF服務庫。WCF服務庫端點未找到POST

我創建了一個GettingStartedLib服務接口:

namespace GettingStartedLib 
{ 
    [ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")] 
    public interface IService 
    { 
     //[OperationContract] 
     [WebGet] 
     string EchoWithGet(string s); 

     //[OperationContract] 
     [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "EchoWithPost")] 
     string EchoWithPost(Person person); 
    } 
} 

和一個服務實現類:

public class Service : IService 
{ 
    public string EchoWithGet(string s) 
    { 
     return "You said " + s; 
    } 

    public string EchoWithPost(Person p) 
    { 
     return "You said " + p.ToString(); 
    } 
} 

和主機

class Program 
{ 
    static void Main(string[] args) 
    { 
     String home = "http://localhost/services/"; 
     WebServiceHost host = new WebServiceHost(typeof(Service), new Uri(home)); 

     try 
     { 
      ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService), new WebHttpBinding(), ""); 
      host.Open(); 

      using (ChannelFactory<IService> cf = new ChannelFactory<IService>(new WebHttpBinding(), home)) 
      { 
       cf.Endpoint.Behaviors.Add(new WebHttpBehavior()); 

       IService channel = cf.CreateChannel(); 

       // Get works fine 
       s = channel.EchoWithGet("Hello, world. GETGET"); 

       // Discarded. Changing to HTTP 
       // s = channel.EchoWithPost({"FirstName": "Anthony"}); 
      } 

      Console.WriteLine("Press <ENTER> to terminate"); 
      Console.ReadLine(); 

      host.Close(); 
    } 
    catch (CommunicationException cex) 
    { 
     Console.WriteLine("An exception occurred: {0}", cex.Message); 
     host.Abort(); 
    } 
} 

並有Person類:

namespace GettingStartedLib 
{ 
    [DataContract] 
    public class Person 
    { 
     [DataMember(Order = 0)] 
     public string FirstName { get; set; } 
    } 
} 

而一個HTTP網頁客戶端

$.post("localhost/services/EchoWithPost", 
{ 
    type: "POST", 
    url: "http://localhost/services/EchoWithPost", 
    contentType: "application/json; charset=utf-8", 
    processData: false, 
    dataType: "json", 
    data: { "FirstName": "Anthony" } 
}, function (data) { 
    console.log(data); 
}); 

我的網絡預覽返回一個錯誤:

Endpoint Not found

App.config爲服務類

<?xml version="1.0" encoding="utf-8" ?> 
<configuration> 
    <appSettings> 
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> 
    </appSettings> 
    <system.web> 
    <compilation debug="true" /> 
    </system.web> 
    <system.serviceModel> 
    <services> 
     <service name="GettingStartedLib.Service"> 
     <host> 
      <baseAddresses> 
      <add baseAddress = "http://localhost/services/" /> 
      </baseAddresses> 
     </host> 
     <endpoint address="" binding="basicHttpBinding" contract="GettingStartedLib.IService"> 
      <identity> 
      <dns value="localhost"/> 
      </identity> 
     </endpoint> 
     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
     </service> 
    </services> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior> 
     </behavior> 
     </serviceBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 

我搜索TraceSVCViewer和爲它添加了代碼。有一個過程錯誤日誌

The incoming message has an unexpected message format 'Raw'. 
The expected message formats for the operation are 'Xml', 'Json'. 
This can be because a WebContentTypeMapper has not been configured on the binding. 
See the documentation of WebContentTypeMapper for more details. 

難道我做錯了什麼?

回答

0

什麼:

[OperationContract] 
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "/EchoWithPost")] 
string EchoWithPost(Person person); 

<system.serviceModel> 
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />  
<services> 
    <service behaviorConfiguration="Default" name="GettingStartedLib.Service"> 
    <host> 
     <baseAddresses> 
     <add baseAddress = "http://localhost/services/" /> 
     </baseAddresses> 
    </host> 
    <endpoint address="" binding="webHttpBinding" contract="GettingStartedLib.IService"> 
     <identity> 
     <dns value="localhost"/> 
     </identity> 
    </endpoint> 
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 
    </service> 
</services> 
<behaviors> 
    <endpointBehaviors> 
     <behavior name="webBehavior"> 
      <webHttp helpEnabled="true" /> 
     </behavior> 
    </endpointBehaviors> 
    <serviceBehaviors> 
     <behavior name="Default"> 
      <serviceMetadata httpGetEnabled="true" /> 
     </behavior> 
     <behavior name=""> 
      <serviceMetadata httpGetEnabled="true" /> 
      <serviceDebug includeExceptionDetailInFaults="false" /> 
     </behavior> 
    </serviceBehaviors> 
</behaviors> 

+0

相同的結果。接收到的數據是'RAW',預計'Json/XML' –

+0

您可以發佈您的端點配置嗎? – Isma

+0

是的。當然。我發佈到頂端 –