日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

编程实现WCF服务

發布時間:2025/5/22 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 编程实现WCF服务 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先當然是編寫契約啦,為了實現契約代碼的復用,這里單獨將契約寫在一個類庫里面Wcf.Contract

1 using System; 2 using System.ServiceModel; 3 4 namespace Wcf.Contract 5 { 6 [ServiceContract(Name="operation",Namespace="urlns://little.org")] 7 public interface IOperation 8 { 9 Guid InstanceId{ get; } 10 [OperationContract] 11 int Add(int a,int b); 12 [OperationContract] 13 int Sub(int a,int b); 14 [OperationContract] 15 int Muti(int a,int b); 16 [OperationContract] 17 int Devide(int a,int b); 18 } 19 }

然后就是服務實現啦,將服務實現單獨寫在一個類庫里面 新建一個類庫項目Wcf.Service,并添加Wcf.Contract 引用

using System; using Wcf.Contract;namespace Wcf.Service {public class Operation:IOperation{public Guid InstanceId{get{return Guid.NewGuid();}}public int Add(int a,int b){return a + b;}public int Sub(int a,int b){return a - b;}public int Muti(int a,int b){return a * b;}public int Devide(int a,int b){return a / b;}} }

然后就是服務的承載了,我們新建一個Wcf.Host 的控制臺項目,在控制臺中承載服務

1 using System; 2 using System.ServiceModel; 3 using Wcf.Service; 4 5 namespace Wcf.Host 6 { 7 class MainClass 8 { 9 public static void Main (string[] args) 10 { 11 Uri[] baseAddresses = new Uri[] { 12 new Uri ("http://localhost:8081/operation", UriKind.Absolute) 13 }; 14 ServiceHost host = new ServiceHost (typeof(Wcf.Service.Operation), baseAddresses); 15 16 host.AddServiceEndpoint ("Wcf.Contract.IOperation", new BasicHttpBinding (), ""); 17 host.Opened += OnServiceHostOpened; 18 try 19 { 20 host.Open(); 21 } 22 catch(Exception ex) { 23 //Console.BackgroundColor = ConsoleColor.Red; 24 Console.WriteLine (ex.Message); 25 //Console.ResetColor (); 26 } 27 Console.WriteLine ("press any key to exit..."); 28 Console.ReadKey (); 29 30 31 } 32 33 public static void OnServiceHostOpened(object sender,EventArgs e) 34 { 35 Console.WriteLine ("ServiceHost has opened."); 36 } 37 } 38 }

這樣就簡單實現了一個Wcf服務,沒有使用配置文件

轉載于:https://www.cnblogs.com/yayaxxww/p/4282749.html

《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀

總結

以上是生活随笔為你收集整理的编程实现WCF服务的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。