Record: Web No Reference no configuration method dynamically invoking WCF service

Source: Internet
Author: User

These years have been used more WEBAPI, the recent project has a need to use WCF, previously also used jquery directly call WCF, but honestly really forgot ...

So it's still a few minutes to take a minute to finish the deal.

WCF server: Host in the existing win service, add code when the service starts, the service code does not have to write, the interface and implementation according to the contract implementation can

            
   Private ServiceHost ServiceHost = null; Service Host

// start the WCF service if NULL ) { servicehost.close (); } New ServiceHost (typeof(ControlService)); Servicehost.open (); Toolkit.Log.Warn ("WCF service startup ");

Server-side ServiceModel configuration: Personal feeling is also the most troublesome place, WCF itself supports a variety of communication methods, so the configuration is more, but the basic understanding of the same

For more information, refer to: http://www.cnblogs.com/ingstyle/p/5711249.html

<?xml version="1.0"encoding="Utf-8"?><configuration> <system.serviceModel> <services> <!--this section isOptional with theNewConfiguration model introducedinch. NET Framework4. -<service Name="namespace + class name"behaviorconfiguration="Controlservicebehavior"> "http://localhost:8000/ServiceModel/service.svc"/> </baseAddresses>  ThisEndpoint isExposed at theBaseAddress provided by Host:http://Localhost:8000/servicemodel/service --<endpoint address=""binding="Wshttpbinding" Contract="namespace + interface name"/> <!--the MEX endpoint isExposed at http://Localhost:8000/servicemodel/service/mex --<endpoint address="Mex"binding="mexhttpbinding"Contract="IMetadataExchange"/> </service> </services> <behaviors> <serviceBehaviors> <behavior na Me="Controlservicebehavior"> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="False"/> </behavior> </serviceBehaviors> </behaviors> </system.servicemodel></conf Iguration>

When you are finished, you can access: Http://localhost:8000/ServiceModel/service.svc view such as:

Client WCF Service call encapsulation:

 Public classwcfhelper{ PublicWcfhelper () {} Public Static ObjectExecutemethod<t> (Binding Bind,stringPURL,stringPmethodname,params Object[] pparams) {EndpointAddress address=Newendpointaddress (PURL); Binding bindinginstance=NULL; Bindinginstance=bind; using(Channelfactory<t> channel =NewChannelfactory<t>(bindinginstance, address)) {T instance=Channel.            CreateChannel (); using(Instance asIDisposable) {                Try{Type type=typeof(T); MethodInfo mi=type.                    GetMethod (Pmethodname); returnmi.                Invoke (instance, pparams); }                Catch(timeoutexception) {(Instance asicommunicationobject).                    Abort (); Throw; }                Catch(communicationexception) {(Instance asicommunicationobject).                    Abort (); Throw; }                Catch(Exception verr) {(Instance asicommunicationobject).                    Abort (); Throw; }            }        }    }}

Because it is called dynamically, there is no configuration and can be used directly to the bottom:

        New Wshttpbinding (); // Focus here, you can pass different bind, and the corresponding server configuration parameters (if the parameters and server inconsistencies will be error)        Wcfhelper.executemethod<icontrolservice> (Bind,"http://localhost:8000/ServiceModel/ Service.svc""resetterminal"newobject"parameter });

Note that the first parameter needs to correspond to the corresponding WCF server configuration

For example, the server is WebHttpBinding, here is also replaced WebHttpBinding bind=new webhttpbinding (), here I use the Wshttpbinding

If you want to know how jquery calls WCF, please refer to the webmaster's blog: http://www.cnblogs.com/dudu/archive/2009/07/14/1523082.html

Record: Web No Reference no configuration method dynamically invoking WCF service

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.