Home>

how to use

1. The first method is relatively simple,And everyone likes it,Because no configuration file is needed to solve it,You only need to know the service contract interface and service address to call it.

2. Use the invoke method, but you need to configure wcf on the calling client. After the configuration, you can encapsulate the service contract interface in the invoke class.

Client calls demo

//First way
string url="http://localhost:3000/doubleservice.svc";
idoubleservice proxy=wcfinvokefactory.createservicebyurl<idoubleservice>(url);
int result=proxy.add (1, 3);
//The second way<br><br>int result1=wcfinvoke.invoke (t =>t.add (1, 3));<br><br>
<system.servicemodel>
  <behaviors>
   <endpointbehaviors>
    <behavior name="newbehavior">
     <datacontractserializer maxitemsinobjectgraph="65536000" />
    </behavior>
   </endpointbehaviors>
  </behaviors>
  <bindings>
   <basichttpbinding>
    <binding name="basichttpbinding_idoubleservice"
        closetimeout="01:00:00"
        opentimeout="01:00:00"
        sendtimeout="01:00:00"
        receivetimeout="01:00:00"
        maxbuffersize="2147483647"
        maxbufferpoolsize="524288"
        maxreceivedmessagesize="2147483647">
     <readerquotas maxdepth="128" maxstringcontentlength="2147483647" maxarraylength="16384" maxbytesperread="4096" maxnametablecharcount="16384" />
    </binding>
   </basichttpbinding>
   <netmsmqbinding>
    <binding name="netmsmqbinding_iasyncsender">
     <security mode="none" />
    </binding>
   </netmsmqbinding>
  </bindings>
  <client>
   <endpoint address="http://localhost:3000/doubleservice.svc"
        binding="basichttpbinding"
        bindingconfiguration="basichttpbinding_idoubleservice"
        contract="doublestone.webhost.idoubleservice"
        name="basichttpbinding_idoubleservice" />
  </client>
 </system.servicemodel>

The first call

public class wcfinvokefactory
  {
    #region wcf service factory
    public static t createservicebyurl<t>(string url)
    {
      return createservicebyurl<t>(url, "basichttpbinding");
    }
    public static t createservicebyurl<t>(string url, string bing)
    {
      try
      {
        if (string.isnullorempty (url)) throw new notsupportedexception ("this url is not null or empty!");
        endpointaddress address=new endpointaddress (url);
        binding binding=createbinding (bing);
        channelfactory<t>factory=new channelfactory<t>(binding, address);
        return factory.createchannel ();
      }
      catch (exception ex)
      {
        throw new exception ("An exception occurred while creating the service factory.
");
      }
    }
    #endregion
    #region Creating a Transport Protocol
    ///<summary>
    ///Create a transmission protocol
    ///</summary>
    ///<param name="binding">transfer protocol name</param>
    ///<returns></returns>
    private static binding createbinding (string binding)
    {
      binding bindinginstance=null;
      if (binding.tolower () == "basichttpbinding")
      {
        basichttpbinding ws=new basichttpbinding ();
        ws.maxbuffersize=2147483647;
        ws.maxbufferpoolsize=2147483647;
        ws.maxreceivedmessagesize=2147483647;
        ws.readerquotas.maxstringcontentlength=2147483647;
        ws.closetimeout=new timespan (0, 30, 0);
        ws.opentimeout=new timespan (0, 30, 0);
        ws.receivetimeout=new timespan (0, 30, 0);
        ws.sendtimeout=new timespan (0, 30, 0);
        bindinginstance=ws;
      }
      else if (binding.tolower () == "nettcpbinding")
      {
        nettcpbinding ws=new nettcpbinding ();
        ws.maxreceivedmessagesize=65535000;
        ws.security.mode=securitymode.none;
        bindinginstance=ws;
      }
      else if (binding.tolower () == "wshttpbinding")
      {
        wshttpbinding ws=new wshttpbinding (securitymode.none);
        ws.maxreceivedmessagesize=65535000;
        ws.security.message.clientcredentialtype=system.servicemodel.messagecredentialtype.windows;
        ws.security.transport.clientcredentialtype=system.servicemodel.httpclientcredentialtype.windows;
        bindinginstance=ws;
      }
      return bindinginstance;
    }
    #endregion
  }

The second calling method

public class wcfinvoke
  {
    ///<summary>
    ///service contract you need to call
    ///</summary>
    ///<typeparam name="t"></typeparam>
    ///<param name="func"></param>
    ///<returns></returns>
    public static t invoke<t>(func<idoubleservice, t>func)
    {
      iserviceinvoker serviceinvoker=new wcfserviceinvoker ();
      return serviceinvoker.invokeservice (func);
    }
  }
public interface iserviceinvoker
  {
    void invokeservice<t>(action<t>invokehandler) where t:class;
    treslt invokeservice<t, treslt>(func<t, treslt>invokehandler) where t:class;
  }
public class wcfserviceinvoker:iserviceinvoker
  {
    private static readonly channelfactorymanager factorymanager=new channelfactorymanager ();
    private static readonly clientsection clientsection =
      configurationmanager.getsection ("system.servicemodel/client") as clientsection;
    public void invokeservice<t>(action<t>invokehandler) where t:class
    {
      keyvaluepair<string, string>endpointnameaddresspair=getendpointnameaddresspair (typeof (t));
      var arg=factorymanager.createchannel<t>(endpointnameaddresspair.key, endpointnameaddresspair.value);
      var obj2=(icommunicationobject) arg;
      try
      {
        invokehandler (arg);
      }
      finally
      {
        try
        {
          if (obj2.state!=communicationstate.faulted)
          {
            obj2.close ();
          }
        }
        catch
        {
          obj2.abort ();
        }
      }
    }
    public treslt invokeservice<t, treslt>(func<t, treslt>invokehandler) where t:class
    {
      keyvaluepair<string, string>endpointnameaddresspair=getendpointnameaddresspair (typeof (t));
      var arg=factorymanager.createchannel<t>(endpointnameaddresspair.key, endpointnameaddresspair.value);
      var obj2=(icommunicationobject) arg;
      try
      {
        return invokehandler (arg);
      }
      finally
      {
        try
        {
          if (obj2.state!=communicationstate.closed || obj2.state!=communicationstate.faulted)
          {
            obj2.close ();
          }
        }
        catch
        {
          obj2.abort ();
        }
      }
    }
    private keyvaluepair<string, string>getendpointnameaddresspair (type servicecontracttype)
    {
      var configexception =
        new configurationerrorsexception (
          string.format (
            "no client endpoint found for type {0}. please add the section<client><endpoint name=\" myservice \ "address=\" http://address/\ "binding=\" basichttpbinding \ "contract=\ "{0} \" /></client>in the config file. ",            servicecontracttype));
      if (((clientsection == null) || (clientsection.endpoints == null)) || (clientsection.endpoints.count<1))
      {
        throw configexception;
      }
      foreach (channelendpointelement element in clientsection.endpoints)
      {
        if (element.contract == servicecontracttype.tostring ())
        {
          return new keyvaluepair<string, string>(element.name, element.address.absoluteuri);
        }
      }
      throw configexception;
    }
  }
public class channelfactorymanager:idisposable
  {
    private static readonly dictionary<type, channelfactory>factories=new dictionary<type, channelfactory>();
    private static readonly object syncroot=new object ();
    public void dispose ()
    {
      dispose (true);
    }
    public virtual t createchannel<t>() where t:class
    {
      return createchannel<t>("*", null);
    }
    public virtual t createchannel<t>(string endpointconfigurationname) where t:class
    {
      return createchannel<t>(endpointconfigurationname, null);
    }
    public virtual t createchannel<t>(string endpointconfigurationname, string endpointaddress) where t:class
    {
      t local=getfactory<t>(endpointconfigurationname, endpointaddress) .createchannel ();
      ((iclientchannel) local) .faulted +=channelfaulted;
      return local;
    }
    protected virtual channelfactory<t>getfactory<t>(string endpointconfigurationname, string endpointaddress)
      where t:class
    {
      lock (syncroot)
      {
        channelfactory factory;
        if (! factories.trygetvalue (typeof (t), out factory))
        {
          factory=createfactoryinstance<t>(endpointconfigurationname, endpointaddress);
          factories.add (typeof (t), factory);
        }
        return (factory as channelfactory<t>);
      }
    }
    private channelfactory createfactoryinstance<t>(string endpointconfigurationname, string endpointaddress)
    {
      channelfactory factory=null;
      factory =! string.isnullorempty (endpointaddress)?new channelfactory<t>(endpointconfigurationname, new endpointaddress (endpointaddress)):new channelfactory<t>(endpointconfigurationname);
      factory.faulted +=factoryfaulted;
      factory.open ();
      return factory;
    }
    private void channelfaulted (object sender, eventargs e)
    {
      var channel=(iclientchannel) sender;
      try
      {
        channel.close ();
      }
      catch
      {
        channel.abort ();
      }
    }
    private void factoryfaulted (object sender, eventargs args)
    {
      var factory=(channelfactory) sender;
      try
      {
        factory.close ();
      }
      catch
      {
        factory.abort ();
      }
      type [] genericarguments=factory.gettype (). getgenericarguments ();
      if ((genericarguments.length == 1))
      {
        type key=genericarguments [0];
        if (factories.containskey (key))
        {
          factories.remove (key);
        }
      }
    }
    protected virtual void dispose (bool disposing)
    {
      if (disposing)
      {
        lock (syncroot)
        {
          foreach (type type in factories.keys)
          {
            channelfactory factory=factories [type];
            try
            {
              factory.close ();
            }
            catch
            {
              factory.abort ();
            }
          }
          factories.clear ();
        }
      }
    }
  }

to sum up

c
  • Previous Detailed types and advantages and disadvantages of MySQL indexes
  • Next Angular uses ui router to implement system permission control and development encountered problems