I. Tạo project WCF bằng visual studio
Mình sử dụng visual sutio 2017 để tạo
B1. Vào File -> New -> Project chọn đến WCF và chọn WCF Service Application

B2. Tạo một project test đơn giản sửa file IService1.cs
using System.ServiceModel; using System.ServiceModel.Web; using static WcfService1.Service1; namespace WcfService1 { [ServiceContract] public interface IService1 { [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/sum", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] Dataresponse sum(Datarequest data); } }
B3. Sửa file Service1.svc.cs
using System; using System.Runtime.Serialization; namespace WcfService1 { public class Service1 : IService1 { Dataresponse IService1.sum(Datarequest data) { return new Dataresponse() { sum = data.v1 + data.v2, }; } [DataContract] public class Datarequest { [DataMember(Name = "v1")] public int v1 { get; set; } [DataMember(Name = "v2")] public int v2 { get; set; } } [DataContract] public class Dataresponse { [DataMember(Order = 0)] public int sum { get; set; } } } }
B4 Sửa file Web.config để có thể request .
<?xml version="1.0"?> <configuration> <appSettings> <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /> </appSettings> <system.web> <compilation debug="true" targetFramework="4.5.2" /> <httpRuntime targetFramework="4.5.2"/> </system.web> <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="web"> <webHttp /> </behavior> </endpointBehaviors> <serviceBehaviors> <behavior name="sum"> <!-- 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> </behaviors> <services> <service behaviorConfiguration="sum" name="WcfService1.Service1"> <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="WcfService1.IService1" /> </service> </services> <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>
B5. Test hoạt động

II Config IIS
B1 Mở IIS trên máy tính
Vào Control Panel -> Programs and Features -> Turn Windows features on or off . Hiển ra của sổ Windows Features.

B2. Chọn .NET Feamework 4.8 Advanced Services -> WCF Services -> Tích vào HTTP Activation

Tích vào Internet Information Services để mở IIS

Chọn Internet Infomation Services -> World Wide Web Services -> Tích vào ASP.NET 4.8, ISAPI Filters, ISAPI Extensions

III Deploy project bằng IIS
B1. Trong visual studio chuột phải vào Object -> Publish..

B2 Chọn IIS, FTP, etc -> Publish

B3 Cài đặt thông số cho publish -> next -> chọn configuration là Release

Cuối cùng là save

B4 Test kết nối
