WCF: Configurable service invocation mode

Source: Internet
Author: User

When adding a WCF service reference, Vs.net would have helped us build a variety of configurations in the App.config/web.config, which is not a good study, but this is not the configuration discussed in this article. Let's look at the following diagram:

Typically, if you use a. NET WCF technology to architect SOA-style applications, we will do some basic layering of the project, such as:

Contract layer: typically defines the interface of the service (that is, the service contract ServiceContract, indicates which methods are available for external invocation), and the model definition that is transferred in the interface method (that is, the data contract DataContract, Specifies the class definition of the object parameter in the method)

Implementation layer: Implementation of the Service interface

The host layer: WCF ultimately needs a hosting environment, and if it is a Web application, the simplest way is to host it directly on IIS

Client layer: That is, the consumer of the service, if it is b/s application, is usually a Web application

When actually deployed, the WCF service tier and the client layer are typically deployed separately, such as:

If the number of concurrent transactions grows with the growth of the business, whether it is the client layer's website or service layer, plus other technologies such as clustering or load balancing, it can be easily expanded. The implementation logic of the service can also be easily replaced by a separate modification (provided the service contract is relatively stable)

However, if the size of the application is small, for cost reasons, it is possible that the service layer and the website client are deployed on a machine, although 1 IIS shelves 2 sites completely no problem, but a bit uncomfortable, since all on a machine, why also call themselves, To add unnecessary overhead?

It is better to change the original code without modifying the premise, through a simple configuration file modification, you can let the original remote call to WCF, the way to directly call the local DLL assembly, and vice versa, so it is more flexible. In fact, many of our projects are this way, small-scale applications, directly all deployed on a machine, such as the application scale up, and then separate deployment, the code is not moving, just modify the relevant configuration.

The principle is actually very simple, reflection can be, first in the client layer of the Web. config or app. Config, add similar to the following node:

1   <appsettings>2     <!--call mode: Remote Call, local call (note: When local calls, there must be a [service implementation class] DLL in the bin directory)-->3     < Add key= "CallType" value= "Remote"/>  4     <!--local call, the name of the assembly-->5 <add key=     "AssemblyName" value= " Sjtu.wcf.demo.implementation "/>6     <!--local invocation, [Service implementation class] name-->7     <add key=" Servicetypename "value=" Sjtu.wcf.demo.implementation.DemoService "/>8   </appSettings>

CallType determines the invocation method: "Remote Call" or "Local DLL call". Then write a call to the Client class locally: (Note: WCF's invocation method, reference to Dudu's article "Enjoy endless-improved version of WCF Client")

 1 using System; 2 using System.Linq.Expressions; 3 using System.Reflection; 4 using System.ServiceModel; 5 using Sjtu.wcf.demo.client.configs; 6 7 Namespace Sjtu.wcf.demo.client 8 {9//&LT;SUMMARY&GT;10//WCF client///&LT;/SUMMARY&GT;12//&L T;typeparam name= "T" >servicecontract interface </typeparam>13 public class wcfclient<t> where T:CLASS14 {1 5 Private readonly string assemblyname;17 private readonly string impltypename;18 private Reado nly string calltype;19 public wcfclient () {CallType = ConfigHelper.CallType.ToLower (); if (CallType = = CallType.Local.ToString (). ToLower ()) (assemblyname = confighelper.assemblyname;26 Impltypename = Co         nfighelper.servicetypename;27}28}29///&LT;SUMMARY&GT;31//For external call method 32 &LT;/SUMMARY&GT;33//<typeparam name= "R" ></typeparam>34//<param name= "expression" ></param>35//<returns></returns>3 6 Public R call<r> (expression<func<t, r>> Expression) PNs {if (CallType = = CallType.Local.ToString ().             ToLower ()) invokelocalmethod<r> (expression), 41}42         return invokeremotemethod<r> (expression);}44//<summary>46//Call local assembly method 47 </summary>48//<typeparam name= "R" ></typeparam>49//<param name= "Operati On "></param>50//<returns></returns>51 private R invokelocalmethod<r> (Express Ion<func<t, r>> operation), Assembly asm = Assembly.Load (New AssemblyName (Assemblynam e)); si t t = (t) asm. CreateInstance (impltypename); R result = Operation.compile (). Invoke (t); 56            return result;57}58///&LT;SUMMARY&GT;60//Call remote WCF Method///</summary >62//<typeparam name= "R" ></typeparam>63//<param name= "Operation" ></param> //<returns></returns>65 Private R invokeremotemethod<r> (expression<func<t, R& Gt;> operation) (channelfactory<t> ChannelFactory = new Channelfactory<t> ("*"); 68             T channel = Channelfactory.createchannel (); var client = (Iclientchannel) channel;71 Client. Open (); R result = Operation.compile (). Invoke (channel), try74 {(client). state = communicationstate.faulted). Close ();}79}80 catch81 {. Abort ();}84 return result;85         }86}87} 

When this is called, only one line of code is required:

1 var students = new wcfclient<istudent> (). Call (c = c.getstudents ("Jerry"));

Reprint website: http://www.cnblogs.com/yjmyzz/p/3372390.html

WCF: Configurable service invocation mode

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.