如何创建restful wcf项目

1.首先新建一个wcf的project

选择新建wcf项目。


2.在IService接口中定义并配置方法

  [ServiceContract]
    public interface IDataService
    {
        [OperationContract]
        [WebGet(UriTemplate = "Metrics?page={page}&pagesize={pagesize}",
        ResponseFormat = WebMessageFormat.Json)]
        [Description("Returns a list of all Metrics.")]
        ICollection<MetricNodes> GetAllMetrics(int page, int pagesize);

        [OperationContract]
        [WebGet(UriTemplate = "Nodes?userName={userName}&userDomain={userDomain}", ResponseFormat = WebMessageFormat.Json)]
        [Description("Returns a list of all MetricNodes.")]
        ICollection<MetricNodes> GetRootMetricNodes(string userName, string userDomain);

3在Svc文件中配置

 [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
    public class EmiteDataService : IEmiteDataService
    {
        
        public ICollection<MetricNodes> GetAllMetrics(int page, int pagesize)
        {
            ........}
       }


4.最后配置web.config(在原有配置中添加下列配置)
  <system.serviceModel>
      <services>
          <service behaviorConfiguration="CfxixiDataServiceBehavior" name="CfxixiDataService.EmiteDataService">
              <host>
                  <baseAddresses>
                      <add baseAddress="http://localhost:6340/CfxixiDataService/"/>
                  </baseAddresses>
              </host>
              <endpoint address="" binding="webHttpBinding" contract="CfxixiDataService.Service.IEmiteDataService" behaviorConfiguration="webBehavior" bindingConfiguration="webBinding"></endpoint>
              <!--<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="EmiteDataService.Service.IEmiteDataService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>-->
              <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
          </service>
      </services>
      <bindings>
          <basicHttpBinding>
              <binding name="basicHttp" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" closeTimeout="00:10:00" openTimeout="00:10:00" sendTimeout="00:10:00" receiveTimeout="00:10:00">
                  <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"/>
              </binding>
              <binding name="BasicTransportSecurity">
                  <security mode="Transport">
                      <transport clientCredentialType="None"/>
                  </security>
              </binding>
          </basicHttpBinding>
          <webHttpBinding>
              <binding name="webBinding" crossDomainScriptAccessEnabled="true"></binding>
          </webHttpBinding>
      </bindings>







      <behaviors>
          <endpointBehaviors>
              <behavior name="webBehavior">
                  <webHttp/>
              </behavior>
          </endpointBehaviors>
          <serviceBehaviors>

              <behavior name="CfxixiDataServiceBehavior">
                  <!-- 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>

              <behavior name="">
                  <serviceMetadata httpGetEnabled="true" />
                  <serviceDebug includeExceptionDetailInFaults="false" />
              </behavior>
          </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

如此便大功告成,发布访问svc文件即可。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值