wcf

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace ProjectFrame.Web
{
    public class ServiceClient<TChannel> : IDisposable
    {
       private TChannel channel;

       public TChannel Channel
       {
           get
           {
               return this.channel;
           }
       }
       private bool disposed;

        public ServiceClient()
        {
            this.disposed = false;
            string text1 = typeof(TChannel).Name;
            this.Inite(text1);
        }

        public ServiceClient(string endpointName)
        {
            this.disposed = false;
            this.Inite(endpointName);
        }
        private void Inite(string endpointName)
        {
            this.channelFactory = new ChannelFactory<TChannel>(endpointName);
            this.channel = this.channelFactory.CreateChannel();
        }
        public void Dispose()
        {
            if (!this.disposed)
            {
                this.channelFactory.Close();
                this.disposed = true;
            }
        }

        private ChannelFactory<TChannel> channelFactory;

 


     
    }
}

 

类的说明

1.一个wcf的类根据TChannel (wcf的接口)的不同返回的wcf的对象不同

 

比如:ITestService是一个wcf的接口那么它返回的就是TestService的对象

    private static string m_productServicePointName = "WSHttpBinding_ITestService";//webconfig中节点endpoint中Name
        public static string TestWork()
        {
            using (ServiceClient<ITestService> client =
                 new ServiceClient<ITestService>(m_productServicePointName))
            {
                return client.Channel.StrWork();
            }
        }

 

webconfig的配置

  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_Default" closeTimeout="00:10:00"
       openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
       bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
       maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text"
       textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
          <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647" maxArrayLength="2147483647"
        maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647"  />
          <reliableSession ordered="true" inactivityTimeout="00:10:00"
          enabled="false" />
          <security mode="Message">
            <transport clientCredentialType="Windows" proxyCredentialType="None"
          realm="" />
            <message clientCredentialType="Windows" negotiateServiceCredential="true"
          algorithmSuite="Default" establishSecurityContext="true" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="http://localhost:9059/TestService.svc" binding="wsHttpBinding"  behaviorConfiguration="DefaultClientBehavior" bindingConfiguration="WSHttpBinding_Default"   contract="TestService.ITestService" name="WSHttpBinding_ITestService">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>
    </client>
    <behaviors>
      <endpointBehaviors>
        <behavior name="DefaultClientBehavior">
          <!--<clientInterceptors/>
          <dataContractSerializer maxItemsInObjectGraph="2147483647"/>-->
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>

 

///这句话是在配置wcf运行的地址name

<endpoint address="http://localhost:9059/TestService.svc" binding="wsHttpBinding"  behaviorConfiguration="DefaultClientBehavior" bindingConfiguration="WSHttpBinding_Default"   contract="TestService.ITestService" name="WSHttpBinding_ITestService">
        <identity>
          <dns value="localhost"/>
        </identity>
      </endpoint>

posted on 2010-05-27 15:19  yangyueming  阅读(524)  评论(0编辑  收藏  举报