我正在尝试将一些JSON数据发布到WCF服务库。
我创建了一个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);
});
我的网络预览会返回错误:
未找到端点
服务类的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 :(得分: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>