WCF服务GET、POST请求接口的配置

一、接口

1,IwcfService

 public interface IwcfService {

        [OperationContract]
        [WebGet(UriTemplate = "Get")]
        string Get();

        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "POST")
            ]
        string Post(Stream pstream);

        [OperationContract]
        [WebInvoke(Method = "PUT", UriTemplate = "PUT")]
        string Put(Stream pstream);

        [OperationContract]
        [WebInvoke(BodyStyle = WebMessageBodyStyle.WrappedRequest,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            Method = "POST")]
        string Post1(string value, string content);

    }

2,wcfService

public class wcfService : IwcfService {

        public string Get() { return "1"; }

        public string Post(Stream pstream) {

            using (var sr = new StreamReader(pstream)) {
                var data = sr.ReadToEnd();
                Logs("WCF", LogType.RECV, 0, data);
                return data;
            }

        }

        public string Post1(string value, string content) {


            var m = new {
                value = value,
                content = content
            };

            var data = m.Json();
            Logs("WCF", LogType.RECV, 0, data);
            return data;

        }

        public string Put(Stream pstream) {

            using (var sr = new StreamReader(pstream)) {
                var data = sr.ReadToEnd();
                Logs("WCF", LogType.RECV, 0, data);
                return data;
            }

        }

    }

 

 

二、配置文件

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceConfig" name="Con_Servicewcf.wcfService">
        <endpoint address="http://localhost:9001/wcfService/" behaviorConfiguration="webHttp"
          binding="webHttpBinding" bindingConfiguration="MyServiceBinding"
          contract="Con_Servicewcf.IwcfService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:9001/wcfService/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="WebService1Soap" closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxReceivedMessageSize="2147483647"/>
      </basicHttpBinding>
      <customBinding>
        <binding name="WebService1Soap12">
          <textMessageEncoding messageVersion="Soap12"/>
          <httpTransport/>
        </binding>
        <binding name="customBinding">
          <binaryMessageEncoding>
          </binaryMessageEncoding>
          <httpTransport maxReceivedMessageSize="2147483647">
          </httpTransport>
        </binding>
      </customBinding>
      <webHttpBinding>
        <binding name="MyServiceBinding" closeTimeout="00:10:00" receiveTimeout="00:20:00" sendTimeout="00:20:00" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" crossDomainScriptAccessEnabled="true">
          <security mode="None">
            <transport clientCredentialType="None"></transport>
          </security>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="webHttp"><!--必须设置-->
          <webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="ServiceConfig">
          <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:9001/wcfService/"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
  </system.serviceModel>
</configuration>

 

 

 

三、启动服务

            using (var host = new ServiceHost(typeof(wcfService))) {

                Logs("WCF", LogType.OPEN, 0, "WCF Servise Starting");

                host.Open();

                var url = host.BaseAddresses[0].ToString();
                Logs("WCF", LogType.OPEN, 0, "WCF Servise URL." + url);

                Logs("WCF", LogType.OPEN, 0, "WCF Servise Open Success");

                Console.Read();

            }

四、参考资料

1.https://www.cnblogs.com/adingfirstlove/p/9835724.html

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值