WCF服务接收空请求

时间:2015-01-23 07:10:17

标签: wcf

enter image description here

var dataToSend = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(mi));

        var req = HttpWebRequest.Create("http://localhost/Service1.svc/json/MethodName");

        req.ContentType = "application/json";
        req.ContentLength = dataToSend.Length;
        req.Method = "POST";
        req.GetRequestStream().Write(dataToSend, 0, dataToSend.Length);

        var response = req.GetResponse();

这里“/ json”是我的端点地址,我的服务配置了多个端点。根据此处的图像,我发送的请求在服务器上接收为空。

如果我的请求格式不正确,请建议正确的方式来调用此服务。

//服务接口

[ServiceContract]
public interface IService
{


    [OperationContract]
    [WebInvoke(Method="POST")]
    Response MethodName(Request request);
}

// Service1

  public class Service1 : IService
  {
      public Response MethodName(Request request)
      {
          some logical operation....
      }
  }

//端点配置(Web配置)

<endpoint address="json" behaviorConfiguration="jsonBehavior"
              binding="webHttpBinding" bindingConfiguration="webHttpBindingJson"
              name="jsonn" contract="Service1.IService" />

<endpoint address="xml" behaviorConfiguration="poxBehavior" binding="webHttpBinding"
              bindingConfiguration="webHttpBindingXml" name="xmll" contract="Service1.IService" />

<endpointBehaviors>
    <behavior name="jsonBehavior">          
      <enableWebScript />
    </behavior>
<behavior name="poxBehavior">
      <enableWebScript />
    </behavior>
  </endpointBehaviors>

<webHttpBinding>
    <binding name="webHttpBindingJson">
      <security mode="None" />
    </binding>
    <binding name="webHttpBindingXml">
      <security mode="None" />
    </binding>
  </webHttpBinding>

//请求类

 [DataContract] 
public class Request 
{ 
   string userMobile; 
   string otp; 

   [DataMember] 
   public string UserMobile 
   { 
        get { return userMobile; } 
        set { userMobile = value; } 
   } 
   [DataMember] 
   public string OTP 
   { 
        get { return otp; } 
        set { otp = value; } 
   }
}

1 个答案:

答案 0 :(得分:0)

最后我找到了这个。 我将json行为配置的端点修改为

<behavior name="jsonBehavior">          
      <webHttp defaultBodyStyle ="Bare"/>
      <!--<enableWebScript />-->
    </behavior>

并删除了enableWebScript。最后我的代码工作。