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