WCF应用:宿主与调用纯代码示例(Host &Client code only sample)
There are different ways to host a WCF appliaction. You can host it in:
- IIS
- WAS
- Windows service
- console application
The last one is very simple to do and I’ll will showYou how to do it.
First, create a new WCF Service Appliaction project. Then open the IService1.cs interface and add a custom method:
1
2
|
[OperationContract] int GetSum( int a, int b); |
After that, implement this method in the Service1.svc.cs file. I did it like this:
1
2
3
4
|
public int GetSum( int a, int b) { return a + b; } |
Next create a new Console Application project and fill the Main function with this content:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
//Creating a new ServiceHost instance with two base addresses: one for the http and second for the net.tcp using (ServiceHost sh = new ServiceHost( typeof (WCFAppTest.Service.Service1), new Uri( "http://localhost:72/Service" ), new Uri( "net.tcp://localhost:71/Service" ))) { //Adding the endpoints to the ServiceHost sh.AddServiceEndpoint( typeof (WCFAppTest.Service.IService1), new NetTcpBinding(), "" ); sh.AddServiceEndpoint( typeof (WCFAppTest.Service.IService1), new WSHttpBinding(), "" ); //Creating a new Service Behavior ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true ; sh.Description.Behaviors.Add(smb); //Opening the ServiceHost for incoming connections sh.Open(); System.Console.WriteLine( "Waiting for connections..." ); System.Console.ReadLine(); //Close opened ServiceHost object sh.Close(); } |
That’s it. Now You have a fully functional WCF Service. TO test it You can create second console project and fill it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
using (ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client()) { proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress( new Uri( "net.tcp://localhost:71/Service" )); proxy.Endpoint.Binding = new NetTcpBinding(); proxy.Endpoint.Contract.ContractType = typeof (WCFAppTest.Service.IService1); Console.WriteLine( "Net.TCP: 1 + 1 is " + proxy.GetSum(1, 1)); proxy.Close(); } using (ServiceReference1.Service1Client proxy = new ServiceReference1.Service1Client()) { proxy.Endpoint.Address = new System.ServiceModel.EndpointAddress( new Uri( "http://localhost:72/Service" )); proxy.Endpoint.Binding = new WSHttpBinding(); proxy.Endpoint.Contract.ContractType = typeof (WCFAppTest.Service.IService1); Console.WriteLine( "HTTP: 2 + 2 is " + proxy.GetSum(2, 2)); proxy.Close(); } Console.ReadLine(); |
ServiceReference1 is a reference You have to add to Your project. Simply do it by clicking RMB on the “Service Reference” node and the “Add Service Reference…”. Next step is to click “Discover” and when Your svc file is found click OK.
OK, now You can run first the service console app and then the test console app and You should see the results like this:
作者:today4king
出处:https://www.cnblogs.com/jinzhao/archive/2012/11/27/2790665.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架