• WCF使用小结:(1)WCF接收HTTP POST数据的处理方法


    在WCF 4.0中,为我们创建Restful API有了更好的支持。通过定义UriTemplate,WebInvoke就可以快速开发API接口。

    这里我记录一下HTTP POST数据时要如何接收POST过来的数据。

    1,方法一:Stream inputStream 输入流方法(注意看方法

    例如我的代码

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare,
     UriTemplate = "json/ui/{projectName}/{entityName}?q={queryString}&id={indentity}")]
     UpdateOrInsertEntityResponse UpdateOrInsertEntityResponse (String projectName, String entityName, String queryString,String indentity,Stream pstream);

    UriTemplate定义了参数匹配关系。

    "json/ui/{projectName}/{entityName}?q={queryString}&id={indentity}"

    对应的参数

    String projectName, String entityName, String queryString,String indentity

    名称必须相同,否则不能匹配。所有字段必须是String类型。

    如何获取POST过来的数据信息。

    定义Stream pstream参数。

    如果你现在运行应用程序的话,会在页面爆出一个错误信息:

    System.InvalidOperationException: For request in operation UpdateOrInsertEntityResponse to be a stream the operation
    
     must have a single parameter whose type is Stream.

    如何解决。

    第一步,修改你自己的Service.svc文件。

    将原始的

    <%@ ServiceHost Language="C#" Debug="true" Service="Vine.DataMan.RestfulService.EntityService"

    CodeBehind="EntityService.svc.cs" %>

     

    增加新的属性:

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"

    最后结果是:

    <%@ ServiceHost Language="C#" Debug="true" Service="Vine.DataMan.RestfulService.EntityService"

    CodeBehind="EntityService.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>

    修改配置文件Web.config

    <system.serviceModel>

    <services>

    <service name="Vine.DataMan.RestfulService.EntityService">

    <endpoint binding="webHttpBinding"

    contract="Vine.DataMan.RestfulService.ServiceContracts.IEntityCommonService"

    behaviorConfiguration="webHttp"/>

    </service>

    </services>

    <behaviors>

    <endpointBehaviors>

    <behavior name="webHttp">

    <webHttp/>

    </behavior>

    </endpointBehaviors>

    <serviceBehaviors>

    <behavior>

    <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->

    <serviceMetadata httpGetEnabled="true"/>

    <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->

    <serviceDebug includeExceptionDetailInFaults="true"/>

    </behavior>

    </serviceBehaviors>

    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>

    </system.serviceModel>

    注意加粗的文字。必须定义webHttp的行为。

  • 相关阅读:
    大胆决定+细致用功, 才可能改变命运
    采用用同步编程的方式实现跨进程异步获取数据
    关于DataGridViewComboBoxCell修改后提交数据源
    学习:如何具有坚韧的性格
    汉语-成语:坚韧不拔
    汉语-词语:坚韧
    阿里云-OSS-Help-SKD参考-.NET:前言
    阿里云-OSS-OSS管理控制台:Bucket列表
    阿里云-OSS-OSS管理控制台:概览
    汉语-词语:推理
  • 原文地址:https://www.cnblogs.com/stragon/p/4718353.html
Copyright © 2020-2023  润新知