【问题标题】:Getting null values in post wcf service在 post wcf 服务中获取空值
【发布时间】:2013-06-05 06:24:05
【问题描述】:

我正在编写 WCF 服务。这是服务端实现

    [OperationContract(Name="GetMediaFile")]
    [Description(ServiceDescConstants.GetMediaFile)]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = UriTemplateConstants.GetMediaFile)]        
    Stream GetMediaFile(string type, string mediaId);

在哪里

UriTemplateConstants.GetMediaFile = "GetMediaFile?type={type}&mediaId={mediaId}";

这里是接口方法实现

  public Stream GetMediaFile(string type, string mediaId)
    {
            CustomerBL customerBl = new CustomerBL();
            return customerBl.getMediaFile(Convert.ToInt32(type), Convert.ToInt32(mediaId));            
    }

在客户端,我使用 RestClient 插件来测试服务。 这是我要发送的数据

网址:customersite/GetMediaFile 标题:内容类型 = x-www-form-urlencoded 正文:type=0&mediaId=1

任何帮助!

现在的问题是我得到空值

【问题讨论】:

    标签: wcf web-services rest wcf-data-services


    【解决方案1】:

    修改接口方法:

    [OperationContract(Name = "GetMediaFile")]
    [Description(ServiceDescConstants.GetMediaFile)]
    [WebInvoke(Method = "POST", 
        ResponseFormat = WebMessageFormat.Json, 
        BodyStyle = WebMessageBodyStyle.Wrapped, 
        UriTemplate = "/GetMediaFile")]
    Stream GetMediaFile(Stream input);
    

    并修改它的实现:

    public Stream GetMediaFile(Stream input)
    {
        StreamReader sr = new StreamReader(input);
        string s = sr.ReadToEnd();
        sr.Dispose();
        NameValueCollection qs = HttpUtility.ParseQueryString(s);
        string type = qs["type"];
        string mediaId = qs["mediaId"];
    
        CustomerBL customerBl = new CustomerBL();
        return customerBl.getMediaFile(Convert.ToInt32(type), Convert.ToInt32(mediaId));
    }
    

    web.config中,使用如下配置(确保使用自己的命名空间/类/接口名称):

    <system.serviceModel>
      <services>
        <service name="WcfServices.MyService">
          <endpoint address="" 
                    name="webEndPoint" 
                    behaviorConfiguration="webBehavior"
                    binding="webHttpBinding" 
                    contract="WcfServices.IMyService" />
        </service>
      </services>
      <behaviors>
        <endpointBehaviors>
          <behavior name="webBehavior">
            <webHttp />
          </behavior>
        </endpointBehaviors>
      </behaviors>
    </system.serviceModel>
    

    这是一个示例请求:

    POST /MyService.svc/GetMediaFile HTTP/1.1
    Host: localhost:64531
    Cache-Control: no-cache
    Content-Type: application/x-www-form-urlencoded
    
    type=0&mediaId=1
    

    解决方案改编自 Edgardo Rossetto 的博客文章 Raw HTTP POST with WCF

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多