WCF note1
Summary of WCF
Client to use service
- use ChannelFactory
to create proxy to use service. Client code show below.
using System;
using System.ServiceModel;
using Artech.WcfServices.Service.Interface;
using System.ServiceModel.Channels;
namespace Artech.WcfServices.Client
{
class Program
{
static void Main(string[] args)
{
using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>("calculatorservice"))
{
ICalculator calculator = channelFactory.CreateChannel();
using (OperationContextScope contextScope = new OperationContextScope(calculator as IClientChannel))
{
string sn = "{DDA095DA-93CA-49EF-BE01-EF5B47179FD0}";
string ns = "http://www.artech.com/";
AddressHeader addressHeader = AddressHeader.CreateAddressHeader("sn", ns, sn);
MessageHeader messageHeader = addressHeader.ToMessageHeader();
OperationContext.Current.OutgoingMessageHeaders.Add(messageHeader);
Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 1, 2, calculator.Add(1, 2));
}
}
Console.Read();
}
}
}
we put our endpoing in config file
<configuration>
<system.serviceModel>
<client>
<endpoint name="calculatorservice"
address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator"/>
</client>
</system.serviceModel>
</configuration>
- service Contract definition
using System.ServiceModel;
namespace Artech.WcfServices.Service.Interface
{
[ServiceContract(Name = "CalculatorService", Namespace ="http://www.artech.com/")]
public interface ICalculator
{
[OperationContract]
double Add(double x, double y);
}
}
- service definition and use host the service
using Artech.WcfServices.Service.Interface;
using System.ServiceModel;
namespace Artech.WcfServices.Service
{
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class CalculatorService : ICalculator
{
public double Add(double x, double y)
{
return x + y;
}
}
}
service host
using (ServiceHost host = new ServiceHost(typeof(CalculatorService)))
{
host.Open();
Console.Read();
}
we put our endpoint definition in the config file
service host config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Artech.WcfServices.Service.CalculatorService">
<endpoint address="http://127.0.0.1:3721/calculatorservice"
binding="ws2007HttpBinding"
contract="Artech.WcfServices.Service.Interface.ICalculator">
<headers>
<sn xmlns="http://www.artech.com/">{DDA095DA-93CA-49EF-BE01-EF5B47179FD0}</sn>
</headers>
</endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
Linstenuri vs uri
- if the listenUriMode of endpoint set to Unique, three answers: 1. if http, the listenUri will add a guid after the Uri; 2. if tcp,and portshare is disable, it will use another port to take as uri. 3. if tcp and portshare is enable, it will use the same uri but add guid after it.
【推荐】国内首个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应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构