2017-08-24 19 views
0

一部のJSONデータをWCFサービスライブラリにPOSTしようとしています。WCFサービスライブラリのエンドポイントがPOSTで見つかりません

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クラス:

私は GettingStartedLibサービス・インターフェースを作成しました
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

はい。確かに。私はそれをトップに投稿します –

関連する問題