无法将字符串POST到WCF服务

时间:2014-12-28 15:30:09

标签: c# wcf post

我有一个简单的WCF服务,有几种方法。我使用POSTMAN来测试它们。所有的GET工作正常,但由于某种原因,我无法发布。

当我尝试发布时:

{"participants":"BLA"}

到本地主机(以ParticipantsStatusService.svc / init为后缀,也从不同的机器到部署了服务的机器) 我明白了:

"400 Bad Request. The request cannot be fulfilled due to bad syntax" and the in the preview:
"

请求错误

The server encountered an error processing the request. The exception message is '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.'. See server logs for more details. The exception stack trace is:

at System.ServiceModel.Dispatcher.DemultiplexingDispatchMessageFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) at System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
"

我设置了一个断点,看看我能否达到这个功能,但我从未到过那里。它应该是WCF之前的东西。不幸的是,我没有找到任何相关的问题参考(虽然它应该是非常基本的)。感谢您的帮助。谢谢 !

(附加界面和实现)

namespace ParticipantsService
{
    [ServiceContract]
    public interface IParticipantsStatusService
    {
        [WebInvoke(Method = "GET", UriTemplate = "ping", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        PingResponse Ping();

        /// <summary>
        ///     Get implementation. Returns a list of all the missing participants in the ParticipantsDB.
        /// </summary>
        [WebInvoke(Method = "GET", UriTemplate = "all", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        GetAllParticipantsResponse GetAllMissingParticipants();

        /// <summary>
        ///     Init implementation. Recieves a dictionary of required patticipants and atedance status (false).
        /// </summary>
        /// <param name="participants"></param>
        //[WebInvoke(Method = "POST", UriTemplate = "init", ResponseFormat = WebMessageFormat.Json,
        //    RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
        [WebInvoke(Method = "POST", UriTemplate = "init", BodyStyle = WebMessageBodyStyle.Bare,
           ResponseFormat = WebMessageFormat.Json)]
        void InitializeMeetingList(string participants);

        /// <summary>
        ///     Update implementation. Returns whether succeeded.
        /// </summary>
        /// <param name="participants"></param>
        [WebInvoke(Method = "POST", UriTemplate = "update", ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
        void UpdateParticipantsStatus(List<Participant> participants);

    }
}


namespace ParticipantsService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    public class ParticipantsStatusService : IParticipantsStatusService
    {
        //private IParticipantsDb participantsDb = new InMemoryParticipantsDb(new List<Participant>());
        private IParticipantsDb participantsDb =
            new InMemoryParticipantsDb(new List<Participant>()
            {
                new Participant("par1", true),
                new Participant("par2", false)
            });

        public PingResponse Ping()
        {
            return new PingResponse("ParticipantsStatusService is alive !");
        }

        public GetAllParticipantsResponse GetAllMissingParticipants()
        {
            if (participantsDb == null) return new GetAllParticipantsResponse(new List<Participant>());
            var missingList = participantsDb.GetMissingParticipants();

            return new GetAllParticipantsResponse(missingList);
        }

        public void InitializeMeetingList(string participants)
        {
            participantsDb = new InMemoryParticipantsDb(new List<Participant>());
        }
    }
}

1 个答案:

答案 0 :(得分:8)

查看错误消息(“传入消息具有意外的消息格式'Raw'。操作的预期消息格式为'Xml','Json'。”)我假设您忘记设置{{1邮件请求中的标题。

尝试使用其他帖子并添加该标题:

Content-Type

查看底部“raw”:https://www.getpostman.com/docs/requests