Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将字符串发布到WCF服务_C#_Wcf_Post - Fatal编程技术网

C# 无法将字符串发布到WCF服务

C# 无法将字符串发布到WCF服务,c#,wcf,post,C#,Wcf,Post,我有一个简单的WCF服务,有几种方法。我用邮递员来测试它们。所有得到的作品都很好,但由于某种原因,我无法张贴 当我尝试发布时: {"participants":"BLA"} 到本地主机(后缀为ParticipantsStatusService.svc/init,也可以从不同的计算机到部署服务的计算机) 我得到: 请求错误 The server encountered an error processing the request. The exception message is 'The i

我有一个简单的WCF服务,有几种方法。我用邮递员来测试它们。所有得到的作品都很好,但由于某种原因,我无法张贴

当我尝试发布时:

{"participants":"BLA"}
到本地主机(后缀为ParticipantsStatusService.svc/init,也可以从不同的计算机到部署服务的计算机) 我得到:

请求错误

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之前。不幸的是,我没有找到任何与我的问题相关的参考资料(尽管它应该是非常基本的)。谢谢你的帮助。谢谢

(附上接口和实现)

命名空间参与者服务
{
[服务合同]
公共接口IParticipantsStatusService
{
[WebInvoke(Method=“GET”,UriTemplate=“ping”,ResponseFormat=WebMessageFormat.Json,
RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]
Ping响应Ping();
/// 
///获取实现。返回ParticipantsDB中缺少的所有参与者的列表。
/// 
[WebInvoke(Method=“GET”,UriTemplate=“all”,ResponseFormat=WebMessageFormat.Json,
RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Bare)]
GetAllParticipants响应GetAllMissingParticipants();
/// 
///初始化实现。接收所需patticipants和Atedess状态的字典(false)。
/// 
/// 
//[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(字符串参与者);
/// 
///更新实现。返回是否成功。
/// 
/// 
[WebInvoke(Method=“POST”,UriTemplate=“update”,ResponseFormat=WebMessageFormat.Json,
RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest)]
void UpdateParticipantsStatus(列出参与者);
}
}
命名空间参与者服务
{
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
公共类参与者StatusService:IParticipantsStatusService
{
//私有IParticipantsDb participantsDb=新的InMemoryParticipantsDb(新列表());
私人参与者参与者参与者=
新InMemoryParticipantsDb(新列表()
{
新参与者(“par1”,正确),
新参与者(“par2”,假)
});
公共Ping响应Ping()
{
返回新的PingResponse(“ParticipantsStatusService处于活动状态!”);
}
公共GetAllParticipants响应GetAllMissingParticipants()
{
if(participantsDb==null)返回新的GetAllParticipantsResponse(new List());
var missingList=participantsDb.getmissingampicipants();
返回新的GetAllParticipants响应(缺失列表);
}
public void InitializeMeetingList(字符串参与者)
{
participantsDb=新的InMemoryParticipantsDb(新列表());
}
}
}

查看错误消息(“传入消息具有意外的消息格式“Raw”。操作的预期消息格式为“Xml”、“Json”)。我假设您忘记在post请求中设置
内容类型

尝试另一篇文章并添加标题:

Content-Type: application/json

看看底部的“raw”:

你能给我们看一下post请求吗?我看不到在这里添加aprint屏幕的任何选项,但确切的url是,在邮递员中,我选择“raw”+“JSON”,并发布以下内容:{“参与者”:“someString”}这正是问题所在:)谢谢!当然可以又是Tnx
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>());
        }
    }
}
Content-Type: application/json