WCF set fixed port as http://localhost:8888/ via Project /Properties/web/project url to create virtual directory

image

 

 

http://localhost:8888/BookService.svc/getbookslist?cnt=10000

 

 

 

 

 

image

 

 

 

image

 

 

image

 

 

//D:\C\WcfService7\WcfService7\Web.config
<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.8" />
    <httpRuntime targetFramework="4.8" maxRequestLength="2147483647" executionTimeout="1800" />
  </system.web>
  <system.serviceModel>
      <bindings>
          <webHttpBinding>
              <binding name="webHttpBindingConfig"                       
                       sendTimeout="00:05:00"
                       receiveTimeout="00:05:00"
                       openTimeout="00:01:00"
                       closeTimeout="01:00:00"
                       maxReceivedMessageSize="2147483647"
                       maxBufferPoolSize="2147483647"
                       maxBufferSize="2147483647"
                       
                       transferMode="Buffered"
                       bypassProxyOnLocal="true"
                       useDefaultWebProxy="true"
                       hostNameComparisonMode="StrongWildcard"
                       crossDomainScriptAccessEnabled="true">
                  <readerQuotas maxArrayLength="2147483647" 
                                maxBytesPerRead="2147483647" 
                                maxDepth="2147483647"
                                maxNameTableCharCount="2147483647"
                                maxStringContentLength="2147483647"/>
                  <security mode="None">
                      
                  </security>
              </binding>                        
          </webHttpBinding>
      </bindings>
    <behaviors>
        <endpointBehaviors>
            <behavior name="webHttpEndpointBehavior">
                <webHttp
                    helpEnabled="true"
                    defaultBodyStyle="Bare"
                    defaultOutgoingResponseFormat="Json"
                    automaticFormatSelectionEnabled="false"/>
            </behavior>
        </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="webHttpServiceBehavior">            
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="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="false"/>
            <serviceThrottling
                maxConcurrentCalls="1000"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>

      <services>
          <service name="WcfService7.BookService" behaviorConfiguration="webHttpServiceBehavior">
              <endpoint address=""
                        binding="webHttpBinding"
                        bindingConfiguration="webHttpBindingConfig"
                        behaviorConfiguration="webHttpEndpointBehavior"
                        contract="WcfService7.IBookService"/>
              <host>
                  <baseAddresses>
                      <add baseAddress="http://localhost:8888/BookService"/>
                  </baseAddresses>
              </host>
          </service>
      </services>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
    
  <system.webServer>
      <security>
          <requestFiltering>
              <requestLimits maxAllowedContentLength="2147483647"/>
          </requestFiltering>
      </security>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>


//D:\C\WcfService7\WcfService7\IBookService.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService7
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IBookService" in both code and config file together.
    [ServiceContract]
    public interface IBookService
    {
        [OperationContract]
        [WebGet(UriTemplate = "GetBooksList?cnt={cnt}", RequestFormat =WebMessageFormat.Json, ResponseFormat =WebMessageFormat.Json)]
        List<Book> GetBooksList(int cnt);
    }

    [DataContract]
    public class Book
    {
        [DataMember]
        public long Id { get; set;  }
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string ISBN { get; set; }
        [DataMember]
        public string Author { get; set; }
        [DataMember]
        public string Abstract { get; set; }
        [DataMember]
        public string Comment { get; set; }
        [DataMember]
        public string Content { get; set; }
        [DataMember]
        public string Summary { get; set; }
        [DataMember]
        public string Title { get; set; }
        [DataMember]
        public string Topic { get; set; }
    }
}

//D:\C\WcfService7\WcfService7\BookService.svc.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Threading;

namespace WcfService7
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "BookService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select BookService.svc or BookService.svc.cs at the Solution Explorer and start debugging.
    public class BookService : IBookService
    {
        private static long idx = 0;
        public static long GetIncrementIdx()
        {
            return Interlocked.Increment(ref idx);
        }

        public List<Book> GetBooksList(int cnt)
        {
            List<Book> bksList = new List<Book>();
            for (int i = 0; i < cnt; i++)
            {
                var a = GetIncrementIdx();
                bksList.Add(new Book()
                {
                    Id = a,
                    Name = $"Name_{a}",
                    ISBN = $"ISBN_{a}_{Guid.NewGuid():N}",
                    Author = $"Author_{a}",
                    Abstract = $"Abstract_{a}",
                    Comment = $"Comment_{a}",
                    Content = $"Content_{a}",
                    Summary = $"Summary_{a}",
                    Title = $"Title_{a}",
                    Topic = $"Topic_{a}"
                });
            }
            return bksList;
        }
    }
}

 

posted @ 2026-05-21 11:52  FredGrit  阅读(11)  评论(0)    收藏  举报