WCF service can be accessed by browser

//D:\C\WcfService2\WcfService2\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 WcfService2
{
    // 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(ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetBooksList?cnt={cnt}")]
        List<Book> GetBooksList(int cnt = 1000000);

        [OperationContract]
        [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetIncrementIdx")]
        long GetIncrementIdx();
    }
}

//D:\C\WcfService2\WcfService2\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 WcfService2
{
    // 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.

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class BookService : IBookService
    {
        private static long id = 0; 
        public List<Book> GetBooksList(int cnt = 1000000)
        {
            List<Book> booksList = new List<Book>();
            for (int i = 1; i < cnt + 1; i++)
            {
                long a = GetIncrementIdx();
                booksList.Add(new Book()
                {
                    Id = a,
                    Name = $"Name_{a}",
                    ISBN = $"ISBN_{a}",
                    Abstract = $"Abstract_{a}",
                    Author = $"Author_{a}",
                    Comment = $"Comment_{a}",
                    Content = $"Content_{a}",
                    Summary = $"Summary_{a}",
                    Title = $"Title_{a}",
                    Topic = $"Topic_{a}"
                });
            }
            return booksList;
        }

        public long GetIncrementIdx()
        {
            return Interlocked.Increment(ref id);
        }
    }
}


//D:\C\WcfService2\WcfService2\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"/>
  </system.web>
  <system.serviceModel>
      <services>
          <service name="WcfService2.BookService"
                   behaviorConfiguration="ServiceBehavior">
              <endpoint address=""
                        binding="webHttpBinding"
                        contract="WcfService2.IBookService"
                        behaviorConfiguration="webBehavior"/>
          </service>
      </services>
    <behaviors>
        <serviceBehaviors>
            <behavior name="ServiceBehavior">
                <!-- 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"/>
            </behavior>
        </serviceBehaviors>
        <endpointBehaviors>
            <behavior name="webBehavior">
                <webHttp/>
            </behavior>
        </endpointBehaviors>     
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <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\WcfService2\WcfService2\Book.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace WcfService2
{
    [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\WcfService2\WcfService2/BookService.svc
<%@ ServiceHost Language="C#" Debug="true" Service="WcfService2.BookService" CodeBehind="BookService.svc.cs" %>

 

 

 

http://localhost:56207/BookService.svc/GetBooksList?cnt=10000

 

image

 

image

 

 

image

 

 

image

 

 

http://localhost:56207/BookService.svc/GetIncrementIdx

image

 

image

 

posted @ 2026-04-23 19:52  FredGrit  阅读(12)  评论(0)    收藏  举报