Sun_Blue_Sky

菩提本无树,明镜亦非台,本来无一物,何处惹尘埃 寻求内心的平静
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

WCF使用ClientBase

Posted on 2013-02-28 15:42  Sun_Blue_Sky  阅读(622)  评论(0编辑  收藏  举报

本章说明:

1、经常使用SvcUtil的朋友可能清楚,每次在服务端做了更改,都可能需要使用SvcUtil重新生成代理文件,在开发期间这可能经常发生,如果很反感这样呢,你可以尝试使用

ClientBase,这只需要在对应的ClientBase实现类做下更改就行了。

2、这对于简洁代码风格的朋友,这可能更加符合你们的口味。

 

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>
Client Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="wsHttpBindingConfiguration" >
          <security mode="None">
          </security>
        </binding>
      </wsHttpBinding>
      <basicHttpBinding>
        <binding name="basicHttpBindingConfiguration">
          <security mode="None">
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost/WcfServer/RoleService.svc"
     binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration"
     contract="WcfServer.IRoleService" name="RoleService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost/WcfServer/UserService.svc"
        binding="wsHttpBinding" bindingConfiguration="wsHttpBindingConfiguration"
        contract="WcfServer.IUserService" name="UserService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
      <endpoint address="http://localhost/WcfServer/UserService.svc/Soap11"
        binding="basicHttpBinding" bindingConfiguration="basicHttpBindingConfiguration"
        contract="WcfServer.IUserService" name="UserServiceSoap11">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>
    </client>
  </system.serviceModel>
</configuration>
RoleClient
using System.ServiceModel;
using WcfServer;

namespace WcfClient1
{
    public class RoleClient : ClientBase<IRoleService>, IRoleService
    {
        public RoleClient()
            : base("RoleService")
        {
        }

        #region ICmService Members

        public void DoWork()
        {
            Channel.DoWork();
        }

        #endregion
    }
}
UserClient
using System.ServiceModel;
using WcfServer;

namespace WcfClient1
{
    public class UserClient : ClientBase<IUserService>, IUserService
    {
        public UserClient()
            : base("UserService")
        {
        }

        #region ICmService Members

        public void DoWork()
        {
            Channel.DoWork();
        }

        #endregion
    }
}
UserClientSoap11
using System.ServiceModel;
using WcfServer;

namespace WcfClient1
{
    public class UserClientSoap11 : ClientBase<IUserService>, IUserService
    {
        public UserClientSoap11()
            : base("UserServiceSoap11")
        {
        }

        #region ICmService Members

        public void DoWork()
        {
            Channel.DoWork();
        }

        #endregion
    }
}
Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfServer;

namespace WcfClient1
{
    class Program
    {
        static void Main(string[] args)
        {
            IRoleService client=new RoleClient();
            
            using (client as IDisposable)
            {
                client.DoWork();
            }

            IUserService client1 = new UserClient();

            using (client1 as IDisposable)
            {
                client1.DoWork();
            }

            IUserService client2 = new UserClientSoap11();

            using (client2 as IDisposable)
            {
                client2.DoWork();
            }

            Console.ReadLine();
        }
    }
}