One, create a new WCF Service reference program
1. Delete the. svc file, delete all.
2, New IService class
namespace testwcf{ [ServiceContract] publicinterface iservice { [ OperationContract] string DoWork ();} }
3, Implement Interface class service class
namespace testwcf{ publicclass service:iservice { public String DoWork () { return" your sister!" "; } }}
4, writing the configuration file
<system.serviceModel> <!--Add this node, otherwise 405 error is present--<bindings> <wsHttpBinding> <bind ing Name="nonesecurity"maxbufferpoolsize="12000000"Maxreceivedmessagesize="12000000"usedefaultwebproxy="false"> <readerquotas maxstringcontentlength="12000000"Maxarraylength="12000000"/> <security mode="None"/> </binding> </wsHttpBinding> </bindings> <behaviors> <servicebeha Viors> <behavior name="Metadatabehavior"> <!--to avoid leaking metadata information, set the following values before deployment tofalse-<servicemetadata httpgetenabled="true"Httpsgetenabled="true"/> <!--to receive the fault exception details for debugging, set the following values totrue。 Before deployment, set tofalseTo avoid leaking exception information--<servicedebug includeexceptiondetailinfaults="false"/> </behavior> </serviceBehaviors> </behaviors> <protocolMapping> &L T;add binding="Wshttpbinding"Scheme="http"/> </protocolMapping> <services> <service name="Testwcf.service"behaviorconfiguration="Metadatabehavior"> <endpoint address=""binding="Wshttpbinding"contract="Testwcf.iservice"bindingconfiguration="nonesecurity"> </endpoint> <endpoint address="Mex"binding="mexhttpbinding"contract="IMetadataExchange"/> </service> </services> <!--No svc file WCF Service activation--<serviceHostingEnvironment> & Lt;serviceactivations> <add relativeaddress="service.svc"Service="Testwcf.service"/> </serviceActivations> </serviceHostingEnvironment> </system.serviceModel>
5. Precautions
The following code is none. svc, the key to activating the WCF service, the new features of WCF4.0
From the point of view of message exchange, the client's call to the Iis/was hosted service essentially manifests itself in the access to the. svc physical file that is actually present. If the service has not been activated, WCF eventually activates the corresponding service based on the physical file that reads the request. Specifically, the type of servicehostfactory that is used to create the ServiceHost (if not explicitly set by factory of the <% @ServiceHost%> directive, The default type of servicehostfactory used is System.ServiceModel.Activation.ServiceHostFactory). After the ServiceHostFactory type is parsed correctly, the ServiceHost object for the homestay service is created through reflection .
If the service side of WCF can correctly create ServiceHost based on the request, the service activation problem can be resolved. Further, if the server can maintain a mapping between a service/servicehostfactory and a request address, we can no longer need the. svc file because. svc is the role of such a mapping for service activation. In the latest WCF, such a mapping relationship can be set in the configuration file. In other words, if the configuration has been set appropriately for this mapping relationship, we will no longer need to define the. svc file for the service.
Under the <system.serviceModel>/<serviceHostingEnvironment> configuration section, there is a <serviceActivations> child node. The mapping between the service/servicehostfactory and the request address is defined under this configuration node. Specifically, the configuration element in the,<serviceactivations> configuration section has three basic properties, where service and factory are used to define the <% @ServiceHost in the. svc File > The service and factory properties of the directive, while relativeaddress represents the address of the server relative to the service-hosted IIS site, which must be suffixed with. svc. The following configuration has the same effect as the. svc file given above, and with this configuration, the. SVC is no longer needed.
<!--without SVC file WCF Service activation-- <serviceHostingEnvironment> <serviceActivations> <add relativeaddress= "service.svc " service="testwcf.service"/ > </serviceActivations> </serviceHostingEnvironment>
Two, do not directly reference the WCF service, using the proxy class to implement the call to WCF
1, new console program, new client proxy class, myclient
namespacewcfclient{/// <summary> ///class used to invoke the service/// </summary> Public classMyclient:clientbase<iservice>, IService { PublicMyClient (binding binding, EndpointAddress edpaddress):Base(binding, edpaddress) {}/// <summary> ///invoking a service-side function/// </summary> /// <returns></returns> Public stringDoWork () {return Base. Channel.dowork (); } }}
2, call test
namespacewcfclient{classProgram {StaticEndpointAddress edphttp =NewEndpointAddress ("http://10.11.109.7:8001/Service.svc"); Static voidMain (string[] args) {myclient Client=NewMyClient (NewWshttpbinding (Securitymode.none), edphttp); Console.WriteLine (client. DoWork ()); Console.readkey (); } }}
3, running results
This makes it possible to manually implement a call to WCF without adding a service reference.
WCF does not have the. svc file service activated, and the service reference is not added to call WCF