Sun_Blue_Sky

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃 寻求内心的平静
随笔 - 21, 文章 - 92, 评论 - 75, 阅读 - 10万
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

WCF使用代码绑定

Posted on   Sun_Blue_Sky  阅读(348)  评论(0编辑  收藏  举报

本章说明:

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

而这可能使用代码绑定比较好。

 

复制代码
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();
        }
    }
}
复制代码

 

 

 

 

 

编辑推荐:
· 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,谁才是开发者新宠?
点击右上角即可分享
微信分享提示