本章说明:
开发中,可能会出现需要有水平扩展或需分布式部署等需求,遇到这种问题你该怎么解决呢,您可以考虑使用一个中间层负责调度,多个服务层进行处理。
而这可能使用代码绑定比较好。

<?xml version="1.0" encoding="utf-8"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <bindings> <wsHttpBinding> <binding name="wsHttpBindingConfiguration" maxReceivedMessageSize="2147483647"> <security mode="None"> </security> </binding> </wsHttpBinding> <basicHttpBinding> <binding name="basicHttpBindingConfiguration" maxReceivedMessageSize="2147483647"> <security mode="None"> </security> </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name="behaviorConfiguration"> <serviceDebug includeExceptionDetailInFaults="true" /> <serviceMetadata httpGetBinding="webHttpBinding" httpGetBindingConfiguration="" httpGetEnabled="true" /> <serviceThrottling maxConcurrentSessions="10000" maxConcurrentInstances="10000" maxConcurrentCalls="10000" /> </behavior> </serviceBehaviors> </behaviors> <services> <service behaviorConfiguration="behaviorConfiguration" name="WcfServer.RoleService"> <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration" contract="WcfServer.IRoleService" /> <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> <service behaviorConfiguration="behaviorConfiguration" name="WcfServer.UserService" > <endpoint address="" binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration" contract="WcfServer.IUserService" /> <endpoint address="soap11" binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration" contract="WcfServer.IUserService" /> <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration="" contract="IMetadataExchange" /> </service> </services> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> </system.webServer> </configuration>
使用ScvUtil生成RoleClient

//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:2.0.50727.5466 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [System.ServiceModel.ServiceContractAttribute(ConfigurationName="IRoleService")] public interface IRoleService { [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IRoleService/DoWork", ReplyAction="http://tempuri.org/IRoleService/DoWorkResponse")] void DoWork(); } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface IRoleServiceChannel : IRoleService, System.ServiceModel.IClientChannel { } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class RoleServiceClient : System.ServiceModel.ClientBase<IRoleService>, IRoleService { public RoleServiceClient() { } public RoleServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) { } public RoleServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public RoleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public RoleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { } public void DoWork() { base.Channel.DoWork(); } }
使用SvcUtil生成UserClient

//------------------------------------------------------------------------------ // <auto-generated> // 此代码由工具生成。 // 运行时版本:2.0.50727.5466 // // 对此文件的更改可能会导致不正确的行为,并且如果 // 重新生成代码,这些更改将会丢失。 // </auto-generated> //------------------------------------------------------------------------------ [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] [System.ServiceModel.ServiceContractAttribute(ConfigurationName="IUserService")] public interface IUserService { [System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/IUserService/DoWork", ReplyAction="http://tempuri.org/IUserService/DoWorkResponse")] void DoWork(); } [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public interface IUserServiceChannel : IUserService, System.ServiceModel.IClientChannel { } [System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")] public partial class UserServiceClient : System.ServiceModel.ClientBase<IUserService>, IUserService { public UserServiceClient() { } public UserServiceClient(string endpointConfigurationName) : base(endpointConfigurationName) { } public UserServiceClient(string endpointConfigurationName, string remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public UserServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) : base(endpointConfigurationName, remoteAddress) { } public UserServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) : base(binding, remoteAddress) { } public void DoWork() { base.Channel.DoWork(); } }
Program

using System; using System.Collections.Generic; using System.Linq; using System.ServiceModel; using System.Text; namespace WcfClient2 { class Program { static void Main(string[] args) { EndpointAddress address = new EndpointAddress(new Uri("http://localhost/WcfServer/RoleService.svc")); WSHttpBinding binding = new WSHttpContextBinding(); binding.Security.Mode = SecurityMode.None; using (ChannelFactory<IRoleService> channel = new ChannelFactory<IRoleService>(binding, address)) { IRoleService role = channel.CreateChannel(); using (role as IDisposable) { role.DoWork(); } } EndpointAddress address1 = new EndpointAddress(new Uri("http://localhost/WcfServer/UserService.svc")); WSHttpBinding binding1 = new WSHttpContextBinding(); binding1.Security.Mode = SecurityMode.None; using (ChannelFactory<IUserService> channel = new ChannelFactory<IUserService>(binding1, address1)) { IUserService user = channel.CreateChannel(); using (user as IDisposable) { user.DoWork(); } } EndpointAddress address2 = new EndpointAddress(new Uri("http://localhost/WcfServer/UserService.svc/Soap11")); BasicHttpBinding binding2 = new BasicHttpBinding(); binding2.Security.Mode = BasicHttpSecurityMode.None; using (ChannelFactory<IUserService> channel = new ChannelFactory<IUserService>(binding2, address2)) { IUserService user = channel.CreateChannel(); using (user as IDisposable) { user.DoWork(); } } Console.ReadLine(); } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?